module Hashie::Extensions::Coercion::ClassMethods
Attributes
Public Instance Methods
# File lib/hashie/extensions/coercion.rb, line 160 def build_coercion(type) if type.is_a? Enumerable if type.class <= ::Hash type, key_type, value_type = type.class, *type.first build_hash_coercion(type, key_type, value_type) else # Enumerable but not Hash: Array, Set value_type = type.first type = type.class build_container_coercion(type, value_type) end elsif CORE_TYPES.key? type build_core_type_coercion(type) elsif type.respond_to? :coerce lambda do |value| return value if value.is_a? type type.coerce(value) end elsif type.respond_to? :new lambda do |value| return value if value.is_a? type type.new(value) end else raise TypeError, "#{type} is not a coercable type" end end
# File lib/hashie/extensions/coercion.rb, line 195 def build_container_coercion(type, value_type) value_coerce = fetch_coercion(value_type) lambda do |value| type.new(value.map { |v| value_coerce.call(v) }) end end
# File lib/hashie/extensions/coercion.rb, line 202 def build_core_type_coercion(type) name = CORE_TYPES[type] lambda do |value| return value if value.is_a? type return value.send(name) end end
# File lib/hashie/extensions/coercion.rb, line 187 def build_hash_coercion(type, key_type, value_type) key_coerce = fetch_coercion(key_type) value_coerce = fetch_coercion(value_type) lambda do |value| type[value.map { |k, v| [key_coerce.call(k), value_coerce.call(v)] }] end end
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.
@param [Object] key the key or array of keys you would like to be coerced. @param [Class] into the class into which you want the key(s) coerced.
@example Coerce a “user” subhash into a User object
class Tweet < Hash include Hashie::Extensions::Coercion coerce_key :user, User end
# File lib/hashie/extensions/coercion.rb, line 76 def coerce_key(*attrs) into = attrs.pop attrs.each { |key| key_coercions[key] = into } end
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.
@param [Class] from the type you would like coerced. @param [Class] into the class into which you would like the value coerced. @option options [Boolean] :strict (true) whether use exact source class only or include ancestors
@example Coerce all hashes into this special type of hash
class SpecialHash < Hash include Hashie::Extensions::Coercion coerce_value Hash, SpecialHash def initialize(hash = {}) super hash.each_pair do |k,v| self[k] = v end end end
# File lib/hashie/extensions/coercion.rb, line 114 def coerce_value(from, into, options = {}) options = { strict: true }.merge(options) if ABSTRACT_CORE_TYPES.key? from ABSTRACT_CORE_TYPES[from].each do |type| coerce_value type, into, options end end if options[:strict] strict_value_coercions[from] = into else while from.superclass && from.superclass != Object lenient_value_coercions[from] = into from = from.superclass end end end
# File lib/hashie/extensions/coercion.rb, line 154 def coercion_cache @coercion_cache ||= ::Hash.new do |hash, type| hash[type] = build_coercion(type) end end
# File lib/hashie/extensions/coercion.rb, line 149 def fetch_coercion(type) return type if type.is_a? Proc coercion_cache[type] end
# File lib/hashie/extensions/coercion.rb, line 210 def inherited(klass) super klass.key_coercions = key_coercions.dup end
Returns the specific key coercion for the specified key, if one exists.
# File lib/hashie/extensions/coercion.rb, line 90 def key_coercion(key) key_coercions[key.to_sym] end
Returns a hash of any existing key coercions.
# File lib/hashie/extensions/coercion.rb, line 84 def key_coercions @key_coercions ||= {} end
Return all value coercions that have the :strict rule as false.
# File lib/hashie/extensions/coercion.rb, line 139 def lenient_value_coercions @lenient_value_coercions ||= {} end
Return all value coercions that have the :strict rule as true.
# File lib/hashie/extensions/coercion.rb, line 134 def strict_value_coercions @strict_value_coercions ||= {} end
Fetch the value coercion, if any, for the specified object.
# File lib/hashie/extensions/coercion.rb, line 144 def value_coercion(value) from = value.class strict_value_coercions[from] || lenient_value_coercions[from] end