Module: Hashie::Extensions::Coercion::ClassMethods
- Defined in:
- lib/hashie/extensions/coercion.rb
Instance Attribute Summary collapse
-
#key_coercions ⇒ Object
readonly
Returns a hash of any existing key coercions.
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.
- #inherited(klass) ⇒ Object
-
#key_coercion(key) ⇒ Object
Returns the specific key coercion for the specified key, if one exists.
-
#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 Attribute Details
#key_coercions ⇒ Object
Returns a hash of any existing key coercions.
114 115 116 |
# File 'lib/hashie/extensions/coercion.rb', line 114 def key_coercions @key_coercions ||= {} end |
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.
106 107 108 109 |
# File 'lib/hashie/extensions/coercion.rb', line 106 def coerce_key(*attrs) 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/hashie/extensions/coercion.rb', line 144 def coerce_value(from, into, = {}) = { strict: true }.merge() if ABSTRACT_CORE_TYPES.key? from ABSTRACT_CORE_TYPES[from].each do | type | coerce_value type, into, end end 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 |
#inherited(klass) ⇒ Object
178 179 180 181 182 |
# File 'lib/hashie/extensions/coercion.rb', line 178 def inherited(klass) super klass.key_coercions = key_coercions.dup end |
#key_coercion(key) ⇒ Object
Returns the specific key coercion for the specified key, if one exists.
120 121 122 |
# File 'lib/hashie/extensions/coercion.rb', line 120 def key_coercion(key) key_coercions[key.to_sym] end |
#lenient_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as false.
168 169 170 |
# File 'lib/hashie/extensions/coercion.rb', line 168 def lenient_value_coercions @lenient_value_coercions ||= {} end |
#strict_value_coercions ⇒ Object
Return all value coercions that have the :strict rule as true.
164 165 166 |
# File 'lib/hashie/extensions/coercion.rb', line 164 def strict_value_coercions @strict_value_coercions ||= {} end |
#value_coercion(value) ⇒ Object
Fetch the value coercion, if any, for the specified object.
173 174 175 176 |
# File 'lib/hashie/extensions/coercion.rb', line 173 def value_coercion(value) from = value.class strict_value_coercions[from] || lenient_value_coercions[from] end |