Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parrent matching of GlobalID & GlobalID::URI #140

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ task :default => :test

Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/cases/**/*_test.rb']
t.test_files = FileList.new('test/cases/**/*_test.rb') do |fl|
fl.exclude('test/cases/pattern_matching_test.rb') if RUBY_VERSION < '2.7'
end
t.verbose = true
t.warning = true
end
2 changes: 1 addition & 1 deletion lib/global_id/global_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def repad_gid(gid)
end

attr_reader :uri
delegate :app, :model_name, :model_id, :params, :to_s, to: :uri
delegate :app, :model_name, :model_id, :params, :to_s, :deconstruct_keys, to: :uri

def initialize(gid, options = {})
@uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)
Expand Down
4 changes: 4 additions & 0 deletions lib/global_id/uri/gid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def to_s
"gid://#{app}#{path}#{'?' + query if query}"
end

def deconstruct_keys(_keys)
{app: app, model_name: model_name, model_id: model_id, params: params}
end

protected
def set_path(path)
set_model_components(path) unless defined?(@model_name) && @model_id
Expand Down
31 changes: 31 additions & 0 deletions test/cases/pattern_matching_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'helper'

class URI::PatternMatchingTest < ActiveSupport::TestCase
setup do
@gid = URI::GID.parse('gid://bcx/Person/5?hello=worlds&param=value')
end

test 'URI::GID pattern matching' do
case @gid
in app: 'bcx', model_name: 'Person', model_id: '5', params: { hello: _ => world, param: }
assert_equal world, 'worlds'
else
raise
end
end
end

class GlobalIDPatternMatchingTest < ActiveSupport::TestCase
setup do
@gid = GlobalID.parse('gid://bcx/Person/5?hello=worlds&param=value')
end

test 'GlobalID pattern matching' do
case @gid
in app: 'bcx', model_name: 'Person', model_id: '5', params: { hello: _ => world, param: }
assert_equal world, 'worlds'
else
raise
end
end
end