Skip to content

Commit

Permalink
respect other Hashie::Hash subclasses when assigning a value
Browse files Browse the repository at this point in the history
When assigning a raw ruby Hash, the hash should be converted
to the current subclass. But when assigning an existing
subclass, we should keep the existing subclass's type.

Example:

h = {:foo => 'bar'}
m = Hashie::Mash.new
m.value = h
m.value.class  # Hashie::Mash

class SubMash < Hashie::Mash
end

h = SubMash.new(:foo => 'bar')
m = Hashie::Mash.new
m.value = h
m.value.class  # Hashie::SubMash  <- should not be converted to a Mash
  • Loading branch information
Jerry Cheung committed Jun 4, 2012
1 parent 6d21c68 commit 3fa6126
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/hashie/mash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def convert_value(val, duping=false) #:nodoc:
case val
when self.class
val.dup
when Hash
duping ? val.dup : val
when ::Hash
val = val.dup if duping
self.class.new(val)
Expand Down
10 changes: 7 additions & 3 deletions spec/hashie/mash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,17 @@ class SubMash < Hashie::Mash
record = Hashie::Mash.new
record.details = Hashie::Mash.new({:email => "randy@asf.com"})
record.details.should be_kind_of(Hashie::Mash)
end

it "should respect another subclass when converting the value" do
record = Hashie::Mash.new

class SubMash < Hashie::Mash
end

son = SubMash.new
son.details = Hashie::Mash.new({:email => "randyjr@asf.com"})
son.details.should be_kind_of(SubMash)
son = SubMash.new({:email => "foo@bar.com"})
record.details = son
record.details.should be_kind_of(SubMash)
end

describe '#respond_to?' do
Expand Down

0 comments on commit 3fa6126

Please sign in to comment.