Skip to content

Commit 6f20be9

Browse files
authored
Merge pull request rails#45553 from gmcgibbon/revert_model_pattern_matching
Revert "Provide pattern matching for ActiveModel"
2 parents d44e786 + 17b4b8f commit 6f20be9

File tree

3 files changed

+0
-62
lines changed

3 files changed

+0
-62
lines changed

activemodel/CHANGELOG.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828

2929
* Define `deconstruct_keys` in `ActiveModel::AttributeMethods`
3030

31-
This provides the Ruby 2.7+ pattern matching interface for hash patterns,
32-
which allows the user to pattern match against anything that includes the
33-
`ActiveModel::AttributeMethods` module (e.g., `ActiveRecord::Base`). As an
34-
example, you can now:
35-
36-
```ruby
37-
class Person < ActiveRecord::Base
38-
end
39-
40-
person = Person.new(name: "Mary")
41-
person => { name: }
42-
name # => "Mary"
43-
```
44-
45-
*Kevin Newton*
46-
4731
* Fix casting long strings to `Date`, `Time` or `DateTime`
4832

4933
*fatkodima*

activemodel/lib/active_model/attribute_methods.rb

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -489,43 +489,6 @@ def respond_to?(method, include_private_methods = false)
489489
end
490490
end
491491

492-
# Returns a hash of attributes for the given keys. Provides the pattern
493-
# matching interface for matching against hash patterns. For example:
494-
#
495-
# class Person
496-
# include ActiveModel::AttributeMethods
497-
#
498-
# attr_accessor :name
499-
# define_attribute_method :name
500-
# end
501-
#
502-
# def greeting_for(person)
503-
# case person
504-
# in { name: "Mary" }
505-
# "Welcome back, Mary!"
506-
# in { name: }
507-
# "Welcome, stranger!"
508-
# end
509-
# end
510-
#
511-
# person = Person.new
512-
# person.name = "Mary"
513-
# greeting_for(person) # => "Welcome back, Mary!"
514-
#
515-
# person = Person.new
516-
# person.name = "Bob"
517-
# greeting_for(person) # => "Welcome, stranger!"
518-
def deconstruct_keys(keys)
519-
deconstructed = {}
520-
521-
keys.each do |key|
522-
string_key = key.to_s
523-
deconstructed[key] = _read_attribute(string_key) if attribute_method?(string_key)
524-
end
525-
526-
deconstructed
527-
end
528-
529492
private
530493
def attribute_method?(attr_name)
531494
respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)

activemodel/test/cases/attributes_test.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,5 @@ def attribute=(_, _)
175175
ModelForAttributesTest.attribute :foo, :unknown
176176
end
177177
end
178-
179-
test "pattern matching against keys" do
180-
case ModelForAttributesTest.new(integer_field: 1)
181-
in { integer_field: 1 }
182-
assert(true)
183-
else
184-
assert(false, "Failed to pattern match")
185-
end
186-
end
187178
end
188179
end

0 commit comments

Comments
 (0)