Module: Humanoid::Extensions::Hash::Accessors
- Included in:
- Hash
- Defined in:
- lib/humanoid/extensions/hash/accessors.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#insert(key, attrs) ⇒ Object
Inserts new attributes into the hash.
-
#klass ⇒ Object
If a _type key exists in the hash, return the class for the value.
-
#remove(key, attrs) ⇒ Object
Remove a set of attributes from a hash.
Instance Method Details
#insert(key, attrs) ⇒ Object
Inserts new attributes into the hash. If the elements are present in the hash it will update them, otherwise it will add the new attributes into the hash as either a child hash or child array of hashes.
21 22 23 24 25 26 27 28 |
# File 'lib/humanoid/extensions/hash/accessors.rb', line 21 def insert(key, attrs) elements = self[key] if elements elements.merge!(attrs) else self[key] = key.singular? ? attrs : [attrs] end end |
#klass ⇒ Object
If a _type key exists in the hash, return the class for the value.
31 32 33 34 |
# File 'lib/humanoid/extensions/hash/accessors.rb', line 31 def klass class_name = self["_type"] class_name ? class_name.constantize : nil end |
#remove(key, attrs) ⇒ Object
Remove a set of attributes from a hash. If the attributes are contained in an array it will remove it from the array, otherwise it will delete the child attribute completely.
10 11 12 13 14 15 |
# File 'lib/humanoid/extensions/hash/accessors.rb', line 10 def remove(key, attrs) elements = self[key] if elements key.singular? ? self[key] = nil : elements.delete(attrs) end end |