diff --git a/lib/rubygems/dependency.rb b/lib/rubygems/dependency.rb index 1e91f493a64e..81987163d83d 100644 --- a/lib/rubygems/dependency.rb +++ b/lib/rubygems/dependency.rb @@ -161,6 +161,10 @@ def type @type ||= :runtime end + def deconstruct_keys(keys) + { name: @name, type: type, requirement: requirement, prerelease: prerelease? } + end + def runtime? @type == :runtime || !@type end diff --git a/test/rubygems/test_gem_dependency.rb b/test/rubygems/test_gem_dependency.rb index a0bfee023936..ab671822c0aa 100644 --- a/test/rubygems/test_gem_dependency.rb +++ b/test/rubygems/test_gem_dependency.rb @@ -410,4 +410,37 @@ def test_identity assert_equal dep("a", " >= 1.a").identity, :abs_latest assert_equal dep("a").identity, :latest end + + def test_deconstruct_keys + dependency = dep("rails", "~> 7.0", :runtime) + keys = dependency.deconstruct_keys(nil) + assert_equal "rails", keys[:name] + assert_equal :runtime, keys[:type] + assert_equal Gem::Requirement.new("~> 7.0"), keys[:requirement] + assert_equal false, keys[:prerelease] + end + + def test_pattern_matching_runtime + dependency = dep("rails", ">= 6.0", :runtime) + result = + case dependency + in type: :runtime, name: "rails" + "matched" + else + "no match" + end + assert_equal "matched", result + end + + def test_pattern_matching_development + dependency = dep("rspec", "~> 3.0", :development) + result = + case dependency + in type: :development, name: + name + else + "no match" + end + assert_equal "rspec", result + end end