diff options
author | Michael Herold <opensource@michaeljherold.com> | 2019-11-17 11:16:10 -0600 |
---|---|---|
committer | Michael Herold <opensource@michaeljherold.com> | 2019-11-17 11:36:31 -0600 |
commit | 15ea67ef0667546627f9a3cd4fef4457512f5880 (patch) | |
tree | 2cf6345d16a063cd0dca51eb6a0dfc8c90ea2d80 /spec | |
parent | 2846ea63a90a594ed67e3eb8ba7c5fd125909089 (diff) | |
download | hashie-15ea67ef0667546627f9a3cd4fef4457512f5880.tar.gz |
Add a PermissiveRespondTo extension for Mashes
By default, Mashes don't state that they respond to unset keys. This
causes unexpected behavior when you try to use a Mash with a
SimpleDelegator.
This new extension allows you create a permissive subclass of Mash that
will be fully compatible with SimpleDelegator and allow you to fully do
thunk-oriented programming with Mashes.
This comes with the trade-off of a ~19KB cache for each of these
subclasses and a ~20% performance penalty on any of those subclasses.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/hashie/extensions/mash/permissive_respond_to_spec.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/spec/hashie/extensions/mash/permissive_respond_to_spec.rb b/spec/hashie/extensions/mash/permissive_respond_to_spec.rb new file mode 100644 index 0000000..189f94a --- /dev/null +++ b/spec/hashie/extensions/mash/permissive_respond_to_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +RSpec.describe Hashie::Extensions::Mash::PermissiveRespondTo do + class PermissiveMash < Hashie::Mash + include Hashie::Extensions::Mash::PermissiveRespondTo + end + + it 'allows you to bind to unset getters' do + mash = PermissiveMash.new(a: 1) + other_mash = PermissiveMash.new(b: 2) + + expect { mash.method(:b) }.not_to raise_error + expect(mash.method(:b).unbind.bind(other_mash).call).to eq 2 + end + + it 'works properly with SimpleDelegator' do + delegator = Class.new(SimpleDelegator) do + def initialize(hash) + super(PermissiveMash.new(hash)) + end + end + + foo = delegator.new(a: 1) + + expect(foo.a).to eq 1 + expect { foo.b }.not_to raise_error + end + + context 'warnings' do + include_context 'with a logger' + + it 'does not log a collision when setting normal keys' do + PermissiveMash.new(a: 1) + + expect(logger_output).to be_empty + end + + it 'logs a collision with a built-in method' do + PermissiveMash.new(zip: 1) + + expect(logger_output).to match('PermissiveMash#zip defined in Enumerable') + end + end +end |