Skip to content

Commit

Permalink
Merge pull request #27 from mattfawcett/master
Browse files Browse the repository at this point in the history
Coercion does not work with MergeInitializer
  • Loading branch information
jch committed Feb 12, 2013
2 parents 60dacaa + 68ed0a3 commit c2815fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/hashie/extensions/merge_initializer.rb
Expand Up @@ -17,7 +17,9 @@ module Extensions
module MergeInitializer
def initialize(hash = {}, default = nil, &block)
default ? super(default) : super(&block)
update(hash)
hash.each do |key, value|
self[key] = value
end
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion spec/hashie/extensions/coercion_spec.rb
Expand Up @@ -17,7 +17,10 @@ def self.coerce(obj)
end

before(:each) do
class ExampleCoercableHash < Hash; include Hashie::Extensions::Coercion end
class ExampleCoercableHash < Hash
include Hashie::Extensions::Coercion
include Hashie::Extensions::MergeInitializer
end
end
subject { ExampleCoercableHash }
let(:instance){ subject.new }
Expand All @@ -39,6 +42,13 @@ class ExampleCoercableHash < Hash; include Hashie::Extensions::Coercion end
instance[:foo].value.should == "String"
instance[:foo].should_not be_coerced
end

it "should coerce when the merge initializer is used" do
subject.coerce_key :foo, Coercable
instance = subject.new(:foo => "bar")

instance[:foo].should be_coerced
end
end

describe '.coerce_value' do
Expand Down

0 comments on commit c2815fa

Please sign in to comment.