summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJerry Cheung <jollyjerry@gmail.com>2013-05-10 11:56:46 -0700
committerJerry Cheung <jollyjerry@gmail.com>2013-05-10 11:56:46 -0700
commit961f71872f57ddb53818b2b322f70692461957c2 (patch)
treec415ddb8c2daaa6b675f36089c371d7161c9c61e
parent0c71bb2043fea64372aefb92a53936627f5964e5 (diff)
parent478371614d6ad84f0e04774bc66e14c31aa2c55b (diff)
downloadhashie-961f71872f57ddb53818b2b322f70692461957c2.tar.gz
Merge pull request #96 from wapcaplet/coerce-improvements
Coerce improvements
-rw-r--r--lib/hashie/extensions/coercion.rb12
-rw-r--r--lib/hashie/mash.rb11
-rw-r--r--spec/hashie/extensions/coercion_spec.rb32
3 files changed, 47 insertions, 8 deletions
diff --git a/lib/hashie/extensions/coercion.rb b/lib/hashie/extensions/coercion.rb
index 9d0c160..c89f20d 100644
--- a/lib/hashie/extensions/coercion.rb
+++ b/lib/hashie/extensions/coercion.rb
@@ -21,6 +21,10 @@ module Hashie
super(key, value)
end
+ def custom_writer(key, value)
+ self[key] = value
+ end
+
def replace(other_hash)
(keys - other_hash.keys).each { |key| delete(key) }
other_hash.each { |key, value| self[key] = value }
@@ -31,7 +35,7 @@ module Hashie
module ClassMethods
# 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
+ # 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.
#
@@ -59,7 +63,7 @@ module Hashie
# Returns the specific key coercion for the specified key,
# if one exists.
def key_coercion(key)
- key_coercions[key]
+ key_coercions[key.to_sym]
end
# Set up a coercion rule such that any time a value of the
@@ -94,7 +98,7 @@ module Hashie
end
end
end
-
+
# Return all value coercions that have the :strict rule as true.
def strict_value_coercions; @strict_value_coercions || {} end
# Return all value coercions that have the :strict rule as false.
@@ -103,7 +107,7 @@ module Hashie
# Fetch the value coercion, if any, for the specified object.
def value_coercion(value)
from = value.class
- strict_value_coercions[from] || lenient_value_coercions[from]
+ strict_value_coercions[from] || lenient_value_coercions[from]
end
end
end
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 6bd1ca4..880225e 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -82,7 +82,7 @@ module Hashie
# Retrieves an attribute set in the Mash. Will convert
# any key passed in to a string before retrieving.
- def [](key)
+ def custom_reader(key)
value = regular_reader(convert_key(key))
yield value if block_given?
value
@@ -91,10 +91,13 @@ module Hashie
# Sets an attribute in the Mash. Key will be converted to
# a string before it is set, and Hashes will be converted
# into Mashes for nesting purposes.
- def []=(key,value) #:nodoc:
+ def custom_writer(key,value) #:nodoc:
regular_writer(convert_key(key), convert_value(value))
end
+ alias_method :[], :custom_reader
+ alias_method :[]=, :custom_writer
+
# This is the bang method reader, it will return a new Mash
# if there isn't a value already assigned to the key requested.
def initializing_reader(key)
@@ -148,11 +151,11 @@ module Hashie
other_hash.each_pair do |k,v|
key = convert_key(k)
if regular_reader(key).is_a?(Mash) and v.is_a?(::Hash)
- regular_reader(key).deep_update(v, &blk)
+ custom_reader(key).deep_update(v, &blk)
else
value = convert_value(v, true)
value = blk.call(key, self[k], value) if blk
- regular_writer(key, value)
+ custom_writer(key, value)
end
end
self
diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb
index 2ad5181..106255a 100644
--- a/spec/hashie/extensions/coercion_spec.rb
+++ b/spec/hashie/extensions/coercion_spec.rb
@@ -77,6 +77,38 @@ describe Hashie::Extensions::Coercion do
instance[:hi].should == "bye"
end
end
+
+ context "when used with a Mash" do
+ class UserMash < Hashie::Mash
+ end
+ class TweetMash < Hashie::Mash
+ include Hashie::Extensions::Coercion
+ coerce_key :user, UserMash
+ end
+
+ it "should coerce with instance initialization" do
+ tweet = TweetMash.new(:user => {:email => 'foo@bar.com'})
+ tweet[:user].should be_a(UserMash)
+ end
+
+ it "should coerce when setting with attribute style" do
+ tweet = TweetMash.new
+ tweet.user = {:email => 'foo@bar.com'}
+ tweet[:user].should be_a(UserMash)
+ end
+
+ it "should coerce when setting with string index" do
+ tweet = TweetMash.new
+ tweet['user'] = {:email => 'foo@bar.com'}
+ tweet[:user].should be_a(UserMash)
+ end
+
+ it "should coerce when setting with symbol index" do
+ tweet = TweetMash.new
+ tweet[:user] = {:email => 'foo@bar.com'}
+ tweet[:user].should be_a(UserMash)
+ end
+ end
end
describe '.coerce_value' do