diff options
author | Max Lincoln <max@devopsy.com> | 2014-08-14 19:21:16 -0400 |
---|---|---|
committer | Max Lincoln <max@devopsy.com> | 2014-08-14 19:25:24 -0400 |
commit | c4957c6ff9754b88499633bf2cdcfdea19320670 (patch) | |
tree | 990c3c932680dd6c5dba85b54d253c134d5ac8ff /lib/hashie/extensions/coercion.rb | |
parent | 68b83a8e283079d702fb9cf3f1bea9a6b7af2b16 (diff) | |
download | hashie-c4957c6ff9754b88499633bf2cdcfdea19320670.tar.gz |
core type coercion may be done!
Diffstat (limited to 'lib/hashie/extensions/coercion.rb')
-rw-r--r-- | lib/hashie/extensions/coercion.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/hashie/extensions/coercion.rb b/lib/hashie/extensions/coercion.rb index 332953f..cc96dd0 100644 --- a/lib/hashie/extensions/coercion.rb +++ b/lib/hashie/extensions/coercion.rb @@ -13,6 +13,11 @@ module Hashie Symbol => :to_sym } + ABSTRACT_CORE_TYPES = { + Integer => [Fixnum, Bignum], + Numeric => [Fixnum, Bignum, Float, Complex, Rational] + } + def self.included(base) base.send :include, InstanceMethods base.extend ClassMethods # NOTE: we wanna make sure we first define set_value_with_coercion before extending @@ -25,7 +30,8 @@ module Hashie def set_value_with_coercion(key, value) into = self.class.key_coercion(key) || self.class.value_coercion(value) - return set_value_without_coercion(key, value) unless value && into + return set_value_without_coercion(key, value) if value.nil? || into.nil? + begin return set_value_without_coercion(key, coerce_or_init(into).call(value)) unless into.is_a?(Enumerable) @@ -137,6 +143,12 @@ module Hashie 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 |