Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5ba4bea

Browse files
committedMar 30, 2022
Support parrent matching of GlobalID & GlobalID::URI
1 parent 3ddb0f8 commit 5ba4bea

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed
 

‎Rakefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ task :default => :test
55

66
Rake::TestTask.new do |t|
77
t.libs << 'test'
8-
t.test_files = FileList['test/cases/**/*_test.rb']
8+
t.test_files = FileList.new('test/cases/**/*_test.rb') do |fl|
9+
fl.exclude('test/cases/pattern_matching_test.rb') if RUBY_VERSION < '2.7'
10+
end
911
t.verbose = true
1012
t.warning = true
1113
end

‎lib/global_id/global_id.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def repad_gid(gid)
4646
end
4747

4848
attr_reader :uri
49-
delegate :app, :model_name, :model_id, :params, :to_s, to: :uri
49+
delegate :app, :model_name, :model_id, :params, :to_s, :deconstruct_keys, to: :uri
5050

5151
def initialize(gid, options = {})
5252
@uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)

‎lib/global_id/uri/gid.rb

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def to_s
9898
"gid://#{app}#{path}#{'?' + query if query}"
9999
end
100100

101+
def deconstruct_keys(_keys)
102+
{app: app, model_name: model_name, model_id: model_id, params: params}
103+
end
104+
101105
protected
102106
def set_path(path)
103107
set_model_components(path) unless defined?(@model_name) && @model_id

‎test/cases/pattern_matching_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'helper'
2+
3+
class URI::PatternMatchingTest < ActiveSupport::TestCase
4+
setup do
5+
@gid = URI::GID.parse('gid://bcx/Person/5?hello=worlds&param=value')
6+
end
7+
8+
test 'full match' do
9+
case @gid
10+
in app: 'bcx', model_name: 'Person', model_id: '5', params: { hello: _ => world, param: }
11+
assert_equal world, 'worlds'
12+
else
13+
raise
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)
Please sign in to comment.