Module: Hashie::Extensions::Coercion::ClassMethods
- Defined in:
- lib/hashie/extensions/coercion.rb
Instance Method Summary collapse
-
#coerce_key(*attrs) ⇒ Object
(also: #coerce_keys)
Set up a coercion rule such that any time the specified key is set it will be coerced into the specified class.
-
#coerce_value(from, into, options = {}) ⇒ Object
Set up a coercion rule such that any time a value of the specified type is set it will be coerced into the specified class.
-
#key_coercion(key) ⇒ Object
Returns the specific key coercion for the specified key, if one exists.
-
#key_coercions ⇒ Object
Returns a hash of any existing key coercions.
-
#lenient_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as false.
-
#strict_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as true.
-
#value_coercion(value) ⇒ Object
Fetch the value coercion, if any, for the specified object.
Instance Method Details
#coerce_key(*attrs) ⇒ Object Also known as: coerce_keys
Set up a coercion rule such that any time the specified key is set it will be coerced into the specified class. Coercion will occur by first attempting to call Class.coerce and then by calling Class.new with the value as an argument in either case.
46 47 48 49 50 |
# File 'lib/hashie/extensions/coercion.rb', line 46 def coerce_key(*attrs) @key_coercions ||= {} into = attrs.pop attrs.each { |key| @key_coercions[key] = into } end |
#coerce_value(from, into, options = {}) ⇒ Object
Set up a coercion rule such that any time a value of the specified type is set it will be coerced into the specified class.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hashie/extensions/coercion.rb', line 85 def coerce_value(from, into, = {}) = {:strict => true}.merge() if [:strict] (@strict_value_coercions ||= {})[from] = into else while from.superclass && from.superclass != Object (@lenient_value_coercions ||= {})[from] = into from = from.superclass end end end |
#key_coercion(key) ⇒ Object
Returns the specific key coercion for the specified key, if one exists.
61 62 63 |
# File 'lib/hashie/extensions/coercion.rb', line 61 def key_coercion(key) key_coercions[key] end |
#key_coercions ⇒ Object
Returns a hash of any existing key coercions.
55 56 57 |
# File 'lib/hashie/extensions/coercion.rb', line 55 def key_coercions @key_coercions || {} end |
#lenient_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as false.
101 |
# File 'lib/hashie/extensions/coercion.rb', line 101 def lenient_value_coercions; @value_coercions || {} end |
#strict_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as true.
99 |
# File 'lib/hashie/extensions/coercion.rb', line 99 def strict_value_coercions; @strict_value_coercions || {} end |
#value_coercion(value) ⇒ Object
Fetch the value coercion, if any, for the specified object.
104 105 106 107 |
# File 'lib/hashie/extensions/coercion.rb', line 104 def value_coercion(value) from = value.class strict_value_coercions[from] || lenient_value_coercions[from] end |