summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladimir Kochnev <hashtable@yandex.ru>2015-09-02 19:04:07 +0300
committerVladimir Kochnev <hashtable@yandex.ru>2015-09-02 19:15:09 +0300
commitadd20277a389e97b3f9c0a2005a3d79b967bb4dd (patch)
tree9d5fe3be001539ff631fee670c8be569a1b5561d
parent11c0ac707f908c19dda72e6e863b06fb069f06b5 (diff)
downloadhashie-add20277a389e97b3f9c0a2005a3d79b967bb4dd.tar.gz
Speed up SafeAssignment and fix private methods.
-rw-r--r--CHANGELOG.md3
-rw-r--r--lib/hashie/extensions/mash/safe_assignment.rb2
-rw-r--r--spec/hashie/extensions/mash/safe_assignment_spec.rb27
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)