From add20277a389e97b3f9c0a2005a3d79b967bb4dd Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Wed, 2 Sep 2015 19:04:07 +0300 Subject: Speed up SafeAssignment and fix private methods. --- CHANGELOG.md | 3 ++- lib/hashie/extensions/mash/safe_assignment.rb | 2 +- .../hashie/extensions/mash/safe_assignment_spec.rb | 27 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e5b7bb..f8b48ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ * Your contribution here. * [#304](https://github.com/intridea/hashie/pull/304): Ensured compatibility of `Hash` extensions with singleton objects - [@regexident](https://github.com/regexident). -* [#306](https://github.com/intridea/hashie/pull/306): Added Hashie::Extensions::Dash::Coercion - [@marshall-lee](https://github.com/marshall-lee). +* [#306](https://github.com/intridea/hashie/pull/306): Added `Hashie::Extensions::Dash::Coercion` - [@marshall-lee](https://github.com/marshall-lee). +* [#310](https://github.com/intridea/hashie/pull/310): Fixed `Hashie::Extensions::SafeAssignment` bug with private methods - [@marshall-lee](https://github.com/marshall-lee). ## 3.4.2 (6/2/2015) diff --git a/lib/hashie/extensions/mash/safe_assignment.rb b/lib/hashie/extensions/mash/safe_assignment.rb index dd41a01..10a57dd 100644 --- a/lib/hashie/extensions/mash/safe_assignment.rb +++ b/lib/hashie/extensions/mash/safe_assignment.rb @@ -3,7 +3,7 @@ module Hashie module Mash module SafeAssignment def custom_writer(key, *args) #:nodoc: - fail ArgumentError, "The property #{key} clashes with an existing method." if methods.include?(key.to_sym) + fail ArgumentError, "The property #{key} clashes with an existing method." if !key?(key) && respond_to?(key, true) super end diff --git a/spec/hashie/extensions/mash/safe_assignment_spec.rb b/spec/hashie/extensions/mash/safe_assignment_spec.rb index a4cd3b1..34d22b1 100644 --- a/spec/hashie/extensions/mash/safe_assignment_spec.rb +++ b/spec/hashie/extensions/mash/safe_assignment_spec.rb @@ -3,17 +3,44 @@ require 'spec_helper' describe Hashie::Extensions::Mash::SafeAssignment do class MashWithSafeAssignment < Hashie::Mash include Hashie::Extensions::Mash::SafeAssignment + + private + + def my_own_private + :hello! + end end context 'when included in Mash' do subject { MashWithSafeAssignment.new } + context 'when not attempting to override a method' do + it 'assigns just fine' do + expect do + subject.blabla = 'Test' + subject.blabla = 'Test' + end.to_not raise_error + end + end + context 'when attempting to override a method' do it 'raises an error' do expect { subject.zip = 'Test' }.to raise_error(ArgumentError) end end + context 'when attempting to override a private method' do + it 'raises an error' do + expect { subject.my_own_private = 'Test' }.to raise_error(ArgumentError) + end + end + + context 'when attempting to initialize with predefined method' do + it 'raises an error' do + expect { MashWithSafeAssignment.new(zip: true) }.to raise_error(ArgumentError) + end + end + context 'when setting as a hash key' do it 'still raises if conflicts with a method' do expect { subject[:zip] = 'Test' }.to raise_error(ArgumentError) -- cgit v1.2.1