diff options
author | Michael Herold <michael.j.herold@gmail.com> | 2014-08-18 13:00:05 -0500 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2014-08-20 16:55:51 -0400 |
commit | 6a2fac91e919b568a6d5cd164e26f55b255ab6e4 (patch) | |
tree | 5b160596fdf77b013969801012e7406483fbef13 | |
parent | 1dce1d08b369e0e073274335875d424d54ccba19 (diff) | |
download | hashie-6a2fac91e919b568a6d5cd164e26f55b255ab6e4.tar.gz |
Add Hashie::Extensions::Mash::SafeAssignment
This is part 3 of 3 of the to-do list determined in #198.
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | lib/hashie.rb | 4 | ||||
-rw-r--r-- | lib/hashie/extensions/mash/safe_assignment.rb | 13 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 7 | ||||
-rw-r--r-- | spec/hashie/extensions/mash/safe_assignment_spec.rb | 17 |
6 files changed, 57 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2213263..0915810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,9 @@ * [#197](https://github.com/intridea/hashie/pull/197): Dont convert keys to string on initalization of mash - [@gregory](https://github.com/gregory). * [#201](https://github.com/intridea/hashie/pull/201): Hashie::Trash transforms can be inherited - [@fobocaster](https://github.com/fobocaster). * [#189](https://github.com/intridea/hashie/pull/189): Added Rash#fetch - [@medcat](https://github.com/medcat). -* [#204](https://github.com/intridea/hashie/pull/204): Added Hashie::Extensions::MethodOverridingWriter and Hashie::Extensions::MethodAccessWithOverride - [@michaelherold](https://github.com/michaelherold). * [#200](https://github.com/intridea/hashie/pull/200): Improved coercion: primitives and error handling - [@maxlinc](https://github.com/maxlinc). +* [#204](https://github.com/intridea/hashie/pull/204): Added Hashie::Extensions::MethodOverridingWriter and Hashie::Extensions::MethodAccessWithOverride - [@michaelherold](https://github.com/michaelherold). +* [#205](http://github.com/intridea/hashie/pull/205): Added Hashie::Extensions::Mash::SafeAssignment - [@michaelherold](https://github.com/michaelherold). * Your contribution here. ## 3.2.0 (7/10/2014) @@ -342,6 +342,21 @@ mash = Mash.load('data/user.csv', parser: MyCustomCsvParser) mash[1] #=> { name: 'John', lastname: 'Doe' } ``` +### Mash Extension: SafeAssignment + +This extension can be mixed into a Mash to guard the attempted overwriting of methods by property setters. When mixed in, the Mash will raise an `ArgumentError` if you attempt to write a property with the same name as an existing method. + +#### Example: + +```ruby +class SafeMash < ::Hashie::Mash + include Hashie::Extensions::Mash::SafeAssignment +end + +safe_mash = SafeMash.new +safe_mash.zip = 'Test' # => ArgumentError +``` + ## Dash Dash is an extended Hash that has a discrete set of defined properties and only those properties may be set on the hash. Additionally, you can set defaults for each property. You can also flag a property as required. Required properties will raise an exception if unset. diff --git a/lib/hashie.rb b/lib/hashie.rb index 40a1af1..2cc24dd 100644 --- a/lib/hashie.rb +++ b/lib/hashie.rb @@ -31,5 +31,9 @@ module Hashie module Dash autoload :IndifferentAccess, 'hashie/extensions/dash/indifferent_access' end + + module Mash + autoload :SafeAssignment, 'hashie/extensions/mash/safe_assignment' + end end end diff --git a/lib/hashie/extensions/mash/safe_assignment.rb b/lib/hashie/extensions/mash/safe_assignment.rb new file mode 100644 index 0000000..2afedc5 --- /dev/null +++ b/lib/hashie/extensions/mash/safe_assignment.rb @@ -0,0 +1,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) + + self[name] = value + end + end + end + end +end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 0206195..62c0564 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -160,6 +160,11 @@ module Hashie alias_method :update, :deep_update alias_method :merge!, :update + # Assigns a value to a key + def assign_property(name, value) + self[name] = value + end + # Performs a shallow_update on a duplicate of the current mash def shallow_merge(other_hash) dup.shallow_update(other_hash) @@ -201,7 +206,7 @@ module Hashie name, suffix = method_suffix(method_name) case suffix when '=' - self[name] = args.first + assign_property(name, args.first) when '?' !!self[name] when '!' diff --git a/spec/hashie/extensions/mash/safe_assignment_spec.rb b/spec/hashie/extensions/mash/safe_assignment_spec.rb new file mode 100644 index 0000000..1ac8678 --- /dev/null +++ b/spec/hashie/extensions/mash/safe_assignment_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Hashie::Extensions::Mash::SafeAssignment do + class MashWithSafeAssignment < Hashie::Mash + include Hashie::Extensions::Mash::SafeAssignment + end + + context 'when included in Mash' do + subject { MashWithSafeAssignment.new } + + context 'when attempting to override a method' do + it 'raises an error' do + expect { subject.zip = 'Test' }.to raise_error(ArgumentError) + end + end + end +end |