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

SafeAssignment protects hash-style assignment too #249

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

* [#247](https://github.com/intridea/hashie/pull/247): Fixed #stringify_keys and #symbolize_keys collision with ActiveSupport - [@bartoszkopinski](https://github.com/bartoszkopinski).
* [#249](https://github.com/intridea/hashie/pull/249): Make SafeAssignment protect hash-style assignment too.
* Your contribution here.

## 3.3.2 (11/26/2014)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ class SafeMash < ::Hashie::Mash
end

safe_mash = SafeMash.new
safe_mash.zip = 'Test' # => ArgumentError
safe_mash.zip = 'Test' # => ArgumentError
safe_mash[:zip] = 'test' # => still ArgumentError
```

## Dash
Expand Down
9 changes: 6 additions & 3 deletions lib/hashie/extensions/mash/safe_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ module Hashie
module Extensions
module Mash
module SafeAssignment
def assign_property(name, value)
fail ArgumentError, "The property #{name} clashes with an existing method." if methods.include?(name.to_sym)
def custom_writer(key, *args) #:nodoc:
fail ArgumentError, "The property #{key} clashes with an existing method." if methods.include?(key.to_sym)
super
end

self[name] = value
def []=(*args)
custom_writer(*args)
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/hashie/extensions/mash/safe_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ class MashWithSafeAssignment < Hashie::Mash
expect { subject.zip = 'Test' }.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)
end
end

end
end