diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | lib/hashie/extensions/mash/safe_assignment.rb | 2 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 65 | ||||
-rw-r--r-- | lib/hashie/version.rb | 2 | ||||
-rw-r--r-- | spec/hashie/extensions/coercion_spec.rb | 2 | ||||
-rw-r--r-- | spec/hashie/mash_spec.rb | 28 |
7 files changed, 59 insertions, 45 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f832113..3266618 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,10 @@ * Your contribution here. -## 3.3.0 (8/26/2014) +## 3.3.1 (8/26/2014) * [#183](https://github.com/intridea/hashie/pull/183): Added Mash#load with YAML file support - [@gregory](https://github.com/gregory). * [#195](https://github.com/intridea/hashie/pull/195): Ensure that the same object is returned after injecting IndifferentAccess - [@michaelherold](https://github.com/michaelherold). -* [#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). * [#200](https://github.com/intridea/hashie/pull/200): Improved coercion: primitives and error handling - [@maxlinc](https://github.com/maxlinc). @@ -12,7 +12,7 @@ $ gem install hashie ## Upgrading -You're reading the documentation for the next release of Hashie, which should be 3.3.1. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version. The current stable release is [3.3.0](https://github.com/intridea/hashie/blob/v3.3.0/README.md). +You're reading the documentation for the next release of Hashie, which should be 3.3.2. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version. The current stable release is [3.3.1](https://github.com/intridea/hashie/blob/v3.3.1/README.md). ## Hash Extensions diff --git a/lib/hashie/extensions/mash/safe_assignment.rb b/lib/hashie/extensions/mash/safe_assignment.rb index 2afedc5..4fa58c7 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 assign_property(name, value) - fail ArgumentError, "The property #{name} clashes with an existing method." if methods.include?(name) + fail ArgumentError, "The property #{name} clashes with an existing method." if methods.include?(name.to_sym) self[name] = value end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 62c0564..182fddb 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -91,13 +91,21 @@ module Hashie class << self; alias_method :[], :new; end + def id #:nodoc: + self['id'] + end + + def type #:nodoc: + self['type'] + end + alias_method :regular_reader, :[] alias_method :regular_writer, :[]= # Retrieves an attribute set in the Mash. Will convert # any key passed in to a string before retrieving. def custom_reader(key) - value = regular_reader(key) + value = regular_reader(convert_key(key)) yield value if block_given? value end @@ -106,7 +114,7 @@ module Hashie # a string before it is set, and Hashes will be converted # into Mashes for nesting purposes. def custom_writer(key, value, convert = true) #:nodoc: - regular_writer(key, convert ? convert_value(value) : value) + regular_writer(convert_key(key), convert ? convert_value(value) : value) end alias_method :[], :custom_reader @@ -115,14 +123,32 @@ module Hashie # This is the bang method reader, it will return a new Mash # if there isn't a value already assigned to the key requested. def initializing_reader(key) - regular_writer(key, self.class.new) unless key?(key) - regular_reader(key) + ck = convert_key(key) + regular_writer(ck, self.class.new) unless key?(ck) + regular_reader(ck) end # This is the under bang method reader, it will return a temporary new Mash # if there isn't a value already assigned to the key requested. def underbang_reader(key) - key?(key) ? regular_reader(key) : self.class.new + ck = convert_key(key) + if key?(ck) + regular_reader(ck) + else + self.class.new + end + end + + def fetch(key, *args) + super(convert_key(key), *args) + end + + def delete(key) + super(convert_key(key)) + end + + def values_at(*keys) + super(*keys.map { |key| convert_key(key) }) end alias_method :regular_dup, :dup @@ -131,6 +157,9 @@ module Hashie self.class.new(self, default) end + def key?(key) + super(convert_key(key)) + end alias_method :has_key?, :key? alias_method :include?, :key? alias_method :member?, :key? @@ -146,12 +175,13 @@ module Hashie # in hash, merging each hash in the hierarchy. def deep_update(other_hash, &blk) other_hash.each_pair do |k, v| - if regular_reader(k).is_a?(Mash) && v.is_a?(::Hash) - custom_reader(k).deep_update(v, &blk) + key = convert_key(k) + if regular_reader(key).is_a?(Mash) && v.is_a?(::Hash) + custom_reader(key).deep_update(v, &blk) else value = convert_value(v, true) - value = convert_value(blk.call(k, self[k], value), true) if blk - custom_writer(k, value, false) + value = convert_value(blk.call(key, self[k], value), true) if blk + custom_writer(key, value, false) end end self @@ -173,7 +203,9 @@ module Hashie # Merges (non-recursively) the hash from the argument, # changing the receiving hash def shallow_update(other_hash) - other_hash.each_pair { |k, v| regular_writer(k, convert_value(v, true)) } + other_hash.each_pair do |k, v| + regular_writer(convert_key(k), convert_value(v, true)) + end self end @@ -201,8 +233,6 @@ module Hashie def method_missing(method_name, *args, &blk) return self.[](method_name, &blk) if key?(method_name) - return self.[](method_name.to_s, &blk) if key?(method_name.to_s) - name, suffix = method_suffix(method_name) case suffix when '=' @@ -223,16 +253,11 @@ module Hashie def method_suffix(method_name) suffixes_regex = ALLOWED_SUFFIXES.join match = method_name.to_s.match(/(.*?)([#{suffixes_regex}]?)$/) - [convert_key(match[1], method_name), match[2]] + [match[1], match[2]] end - def convert_key(transformed_value, original_value) - case original_value - when Symbol - transformed_value.to_sym - when String - transformed_value.to_s - end + def convert_key(key) #:nodoc: + key.to_s end def convert_value(val, duping = false) #:nodoc: diff --git a/lib/hashie/version.rb b/lib/hashie/version.rb index 6fb004f..9ae315a 100644 --- a/lib/hashie/version.rb +++ b/lib/hashie/version.rb @@ -1,3 +1,3 @@ module Hashie - VERSION = '3.3.0' + VERSION = '3.3.1' end diff --git a/spec/hashie/extensions/coercion_spec.rb b/spec/hashie/extensions/coercion_spec.rb index 20cd706..17261ed 100644 --- a/spec/hashie/extensions/coercion_spec.rb +++ b/spec/hashie/extensions/coercion_spec.rb @@ -346,7 +346,7 @@ describe Hashie::Extensions::Coercion do it 'coerces when setting with string index' do tweet = TweetMash.new tweet['user'] = { email: 'foo@bar.com' } - expect(tweet['user']).to be_a(UserMash) + expect(tweet[:user]).to be_a(UserMash) end it 'coerces when setting with symbol index' do diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb index fa0f04f..6579d00 100644 --- a/spec/hashie/mash_spec.rb +++ b/spec/hashie/mash_spec.rb @@ -10,11 +10,11 @@ describe Hashie::Mash do it 'sets hash values through method= calls' do subject.test = 'abc' - expect(subject[:test]).to eq 'abc' + expect(subject['test']).to eq 'abc' end it 'retrieves set values through method calls' do - subject[:test] = 'abc' + subject['test'] = 'abc' expect(subject.test).to eq 'abc' end @@ -26,7 +26,7 @@ describe Hashie::Mash do end it 'retrieves set values through blocks with method calls' do - subject[:test] = 'abc' + subject['test'] = 'abc' value = nil subject.test { |v| value = v } expect(value).to eq 'abc' @@ -214,7 +214,7 @@ describe Hashie::Mash do end it 'returns self' do - expect(subject.replace(foo: 'bar').to_hash).to eq(foo: 'bar') + expect(subject.replace(foo: 'bar').to_hash).to eq('foo' => 'bar') end it 'sets all specified keys to their corresponding values' do @@ -226,7 +226,7 @@ describe Hashie::Mash do end it 'leaves only specified keys' do - expect(subject.keys.sort).to eq [:details, :middle_name] + expect(subject.keys.sort).to eq %w(details middle_name) expect(subject.first_name?).to be_falsy expect(subject).not_to respond_to(:first_name) expect(subject.last_name?).to be_falsy @@ -236,7 +236,7 @@ describe Hashie::Mash do describe 'delete' do it 'deletes with String key' do - subject.delete(:details) + subject.delete('details') expect(subject.details).to be_nil expect(subject).not_to be_respond_to :details end @@ -354,14 +354,6 @@ describe Hashie::Mash do expect(converted.name).to eq 'Bob' end - it 'does not force the key type to string' do - h = { :abc => 123, 'name' => 'Bob', 123 => 'foo', true => 'false', /foo/ => 'bar' } - converted = Hashie::Mash.new h - expect(converted.to_hash).to eq h - expect(converted.abc).to eq h[:abc] - expect(converted.name).to eq h['name'] - end - it 'converts hashes recursively into Hashie::Mashes' do converted = Hashie::Mash.new(a: { b: 1, c: { d: 23 } }) expect(converted.a.is_a?(Hashie::Mash)).to be_truthy @@ -418,8 +410,7 @@ describe Hashie::Mash do context 'when key has other than original but acceptable type' do it 'returns the value' do - expect { mash.fetch('one') }.to raise_exception(KeyError) - expect(mash.fetch(:one)).to eql(1) + expect(mash.fetch('one')).to eql(1) end end end @@ -497,9 +488,8 @@ describe Hashie::Mash do end context 'when a different, but acceptable type is given' do - it 'returns the existing values' do - expect(mash.values_at(:key_one, 'key_two')).to eq([nil, nil]) - expect(mash.values_at('key_one', :key_two)).to eq([1, 2]) + it 'returns the values' do + expect(mash.values_at(:key_one, 'key_two')).to eq([1, 2]) end end |