Class: VariaModel::Attributes
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- VariaModel::Attributes
- Defined in:
- lib/varia_model/attributes.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Override setter to coerce the given value if a coercion is defined.
- #coercion(key) ⇒ Object
-
#container(path) ⇒ Hashie::Mash?
Return the containing Hashie::Mash of the given dotted path.
- #dup ⇒ Object
- #old_dup ⇒ Object
- #old_setter ⇒ Object
- #set_coercion(key, fun) ⇒ Object
Instance Attribute Details
#coercions ⇒ Hashie::Mash
12 13 14 |
# File 'lib/varia_model/attributes.rb', line 12 def coercions @coercions ||= Hashie::Mash.new end |
Instance Method Details
#[]=(key, value) ⇒ Object
Override setter to coerce the given value if a coercion is defined
25 26 27 28 |
# File 'lib/varia_model/attributes.rb', line 25 def []=(key, value) coerced_value = coercion(key).present? ? coercion(key).call(value) : value old_setter(key, coerced_value) end |
#coercion(key) ⇒ Object
16 17 18 |
# File 'lib/varia_model/attributes.rb', line 16 def coercion(key) self.coercions[key] end |
#container(path) ⇒ Hashie::Mash?
Return the containing Hashie::Mash of the given dotted path
35 36 37 38 39 40 41 42 43 |
# File 'lib/varia_model/attributes.rb', line 35 def container(path) parts = path.split('.', 2) match = (self[parts[0].to_s] || self[parts[0].to_sym]) if !parts[1] or match.nil? self else match.container(parts[1]) end end |
#dup ⇒ Object
45 46 47 48 49 |
# File 'lib/varia_model/attributes.rb', line 45 def dup mash = old_dup mash.coercions = self.coercions mash end |
#old_dup ⇒ Object
7 |
# File 'lib/varia_model/attributes.rb', line 7 alias_method :old_dup, :dup |
#old_setter ⇒ Object
6 |
# File 'lib/varia_model/attributes.rb', line 6 alias_method :old_setter, :[]= |
#set_coercion(key, fun) ⇒ Object
20 21 22 |
# File 'lib/varia_model/attributes.rb', line 20 def set_coercion(key, fun) self.coercions[key] = fun end |