diff options
author | tcocca <tom.cocca@gmail.com> | 2010-08-27 02:15:43 +0800 |
---|---|---|
committer | Michael Bleigh <michael@intridea.com> | 2010-08-31 13:22:32 +0800 |
commit | 54f9bdde2930deda2d223a00b9fb40a8d236e904 (patch) | |
tree | 16f6a6de0f0a8fd5676db07bec159708b2951840 | |
parent | bbb21e20afcc16abcb2abc569b6bb5d1fbbac917 (diff) | |
download | hashie-54f9bdde2930deda2d223a00b9fb40a8d236e904.tar.gz |
initialze_reader and convert_value should respect subclassing by using self.class instead of Hashie::Mash
-rw-r--r-- | lib/hashie/mash.rb | 4 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 24 |
2 files changed, 26 insertions, 2 deletions
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 218c341..3330fcf 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -79,7 +79,7 @@ module Hashie # if there isn't a value already assigned to the key requested. def initializing_reader(key) ck = convert_key(key) - regular_writer(ck, Hashie::Mash.new) unless key?(ck) + regular_writer(ck, self.class.new) unless key?(ck) regular_reader(ck) end @@ -141,7 +141,7 @@ module Hashie def convert_value(val, duping=false) #:nodoc: case val - when ::Hashie::Mash + when self.class val.dup when ::Hash val = val.dup if duping diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index fd1af79..df3d30e 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -100,6 +100,30 @@ describe Hashie::Mash do record['submash'].should be_kind_of(SubMash) end + it "should respect the class when passed a bang method for a non-existent key" do + record = Hashie::Mash.new + record.non_existent!.should be_kind_of(Hashie::Mash) + + class SubMash < Hashie::Mash + end + + son = SubMash.new + son.non_existent!.should be_kind_of(SubMash) + end + + it "should respect the class when converting the value" do + record = Hashie::Mash.new + record.details = Hashie::Mash.new({:email => "randy@asf.com"}) + record.details.should be_kind_of(Hashie::Mash) + + class SubMash < Hashie::Mash + end + + son = SubMash.new + son.details = Hashie::Mash.new({:email => "randyjr@asf.com"}) + son.details.should be_kind_of(SubMash) + end + describe '#respond_to?' do it 'should respond to a normal method' do Hashie::Mash.new.should be_respond_to(:key?) |