Skip to content

Commit

Permalink
initialze_reader and convert_value should respect subclassing by usin…
Browse files Browse the repository at this point in the history
…g self.class instead of Hashie::Mash
  • Loading branch information
tcocca authored and Michael Bleigh committed Aug 31, 2010
1 parent bbb21e2 commit 54f9bdd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hashie/mash.rb
Expand Up @@ -79,7 +79,7 @@ def []=(key,value) #:nodoc:
# 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

Expand Down Expand Up @@ -141,7 +141,7 @@ def convert_key(key) #:nodoc:

def convert_value(val, duping=false) #:nodoc:
case val
when ::Hashie::Mash
when self.class
val.dup
when ::Hash
val = val.dup if duping
Expand Down
24 changes: 24 additions & 0 deletions spec/hashie/mash_spec.rb
Expand Up @@ -100,6 +100,30 @@ class SubMash < Hashie::Mash
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?)
Expand Down

0 comments on commit 54f9bdd

Please sign in to comment.