Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coercion scope bug #282

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/hashie/extensions/coercion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def value_coercion(value)
def inherited(klass)
super

klass.key_coercions = key_coercions
klass.key_coercions = key_coercions.dup
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/hashie/extensions/coercion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ class NestedCoercableHash < BaseCoercableHash
coerce_key :bar, Integer
end

class OtherNestedCoercableHash < BaseCoercableHash
coerce_key :foo, Symbol
end

class RootCoercableHash < BaseCoercableHash
coerce_key :nested, NestedCoercableHash
coerce_key :other, OtherNestedCoercableHash
coerce_key :nested_list, Array[NestedCoercableHash]
coerce_key :nested_hash, Hash[String => NestedCoercableHash]
end
Expand All @@ -62,6 +67,13 @@ def test_nested_object(obj)
subject { RootCoercableHash }
let(:instance) { subject.new }

it 'does not add coercions to superclass' do
instance[:nested] = { foo: 'bar' }
instance[:other] = { foo: 'bar' }
expect(instance[:nested][:foo]).to be_a String
expect(instance[:other][:foo]).to be_a Symbol
end

it 'coerces nested objects' do
instance[:nested] = { foo: 123, bar: '456' }
test_nested_object(instance[:nested])
Expand Down