Revert "Provide pattern matching for ActiveModel"
This reverts commit 7e499b25acd5e1bfdd54ca2af66678b0ed05def1.
This commit is contained in:
parent
d44e786d21
commit
17b4b8fd63
@ -28,22 +28,6 @@
|
||||
|
||||
* Define `deconstruct_keys` in `ActiveModel::AttributeMethods`
|
||||
|
||||
This provides the Ruby 2.7+ pattern matching interface for hash patterns,
|
||||
which allows the user to pattern match against anything that includes the
|
||||
`ActiveModel::AttributeMethods` module (e.g., `ActiveRecord::Base`). As an
|
||||
example, you can now:
|
||||
|
||||
```ruby
|
||||
class Person < ActiveRecord::Base
|
||||
end
|
||||
|
||||
person = Person.new(name: "Mary")
|
||||
person => { name: }
|
||||
name # => "Mary"
|
||||
```
|
||||
|
||||
*Kevin Newton*
|
||||
|
||||
* Fix casting long strings to `Date`, `Time` or `DateTime`
|
||||
|
||||
*fatkodima*
|
||||
|
@ -489,43 +489,6 @@ def respond_to?(method, include_private_methods = false)
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a hash of attributes for the given keys. Provides the pattern
|
||||
# matching interface for matching against hash patterns. For example:
|
||||
#
|
||||
# class Person
|
||||
# include ActiveModel::AttributeMethods
|
||||
#
|
||||
# attr_accessor :name
|
||||
# define_attribute_method :name
|
||||
# end
|
||||
#
|
||||
# def greeting_for(person)
|
||||
# case person
|
||||
# in { name: "Mary" }
|
||||
# "Welcome back, Mary!"
|
||||
# in { name: }
|
||||
# "Welcome, stranger!"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# person = Person.new
|
||||
# person.name = "Mary"
|
||||
# greeting_for(person) # => "Welcome back, Mary!"
|
||||
#
|
||||
# person = Person.new
|
||||
# person.name = "Bob"
|
||||
# greeting_for(person) # => "Welcome, stranger!"
|
||||
def deconstruct_keys(keys)
|
||||
deconstructed = {}
|
||||
|
||||
keys.each do |key|
|
||||
string_key = key.to_s
|
||||
deconstructed[key] = _read_attribute(string_key) if attribute_method?(string_key)
|
||||
end
|
||||
|
||||
deconstructed
|
||||
end
|
||||
|
||||
private
|
||||
def attribute_method?(attr_name)
|
||||
respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)
|
||||
|
@ -175,14 +175,5 @@ def attribute=(_, _)
|
||||
ModelForAttributesTest.attribute :foo, :unknown
|
||||
end
|
||||
end
|
||||
|
||||
test "pattern matching against keys" do
|
||||
case ModelForAttributesTest.new(integer_field: 1)
|
||||
in { integer_field: 1 }
|
||||
assert(true)
|
||||
else
|
||||
assert(false, "Failed to pattern match")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user