hashie 3.5.4 → 3.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b244f4fdc55d9650174134efecbb99d32e75ef14
4
- data.tar.gz: 5a00da5d9da33a9a7bdc3f821d16e83dfca65ced
3
+ metadata.gz: ec77fc445035152bbc8ff4d4ce152250d2cbe448
4
+ data.tar.gz: 5ce003a19f12e4f3a058713ef02fbe1e179b6dcc
5
5
  SHA512:
6
- metadata.gz: f948ef11d034b29d9b6a6d9473be7528ba2e3b2e79e9a164fd622194ed39ab29a7b6d37ea1a4f4eeb0232a28fb3ff8d6b19cba3b059a13b33e5894180d76c7c7
7
- data.tar.gz: e3ac5f5715323c230b2beedd38a4e47a9a2d199f17a4535072d75c645364ac14b2b4f95e4985de39db270b17f10b92989266eb5d4bb2c4a834a386a7807a04dd
6
+ metadata.gz: f7fd9f5988b7e928f526d0bc739c754d1792af74a2ba93a328775686940d7b769e06c760cb38b9482418e397cffb7a6f4479925769537394c73b489b039f29f6
7
+ data.tar.gz: d31ba3a374545f33e9c8cbf551f29aa74a27f3caed1af505c72c082bc68c6d8475a790a40721ddb6a5b6fa838a0effa47440661b0f155cf3613cae9ac481bedb
@@ -6,6 +6,18 @@ scheme are considered to be bugs.
6
6
 
7
7
  [semver]: http://semver.org/spec/v2.0.0.html
8
8
 
9
+ ## [3.5.5] - 2017-02-24
10
+
11
+ [3.5.5]: https://github.com/intridea/hashie/compare/v3.5.4...v3.5.5
12
+
13
+ ### Added
14
+
15
+ * [#326](https://github.com/intridea/hashie/pull/326): Added `Hashie::Extensions::Mash::KeepOriginalKeys` to give Mashes the ability to keep the original structure given to it - [@michaelherold](https://github.com/michaelherold).
16
+
17
+ ### Fixed
18
+
19
+ * [#415](https://github.com/intridea/hashie/pull/415): Fixed Mash logging keys multiple times which lead to a bad user experience or, in some cases, errors - [@michaelherold](https://github.com/michaelherold).
20
+
9
21
  ## [3.5.4] - 2017-02-22
10
22
 
11
23
  [3.5.4]: https://github.com/intridea/hashie/compare/v3.5.3...v3.5.4
data/README.md CHANGED
@@ -20,7 +20,7 @@ $ gem install hashie
20
20
 
21
21
  ## Upgrading
22
22
 
23
- You're reading the documentation for the stable release of Hashie, 3.5.4. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
23
+ You're reading the documentation for the stable release of Hashie, 3.5.5. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
24
24
 
25
25
  ## Hash Extensions
26
26
 
@@ -545,6 +545,25 @@ class Response < Hashie::Mash
545
545
  end
546
546
  ```
547
547
 
548
+ ### Mash Extension: KeepOriginalKeys
549
+
550
+ This extension can be mixed into a Mash to keep the form of any keys passed directly into the Mash. By default, Mash converts keys to strings to give indifferent access. This extension still allows indifferent access, but keeps the form of the keys to eliminate confusion when you're not expecting the keys to change.
551
+
552
+ ```ruby
553
+ class KeepingMash < ::Hashie::Mash
554
+ include Hashie::Extensions::Mash::KeepOriginalKeys
555
+ end
556
+
557
+ mash = KeepingMash.new(:symbol_key => :symbol, 'string_key' => 'string')
558
+ mash.to_hash == { :symbol_key => :symbol, 'string_key' => 'string' } #=> true
559
+ mash.symbol_key #=> :symbol
560
+ mash[:symbol_key] #=> :symbol
561
+ mash['symbol_key'] #=> :symbol
562
+ mash.string_key #=> 'string'
563
+ mash['string_key'] #=> 'string'
564
+ mash[:string_key] #=> 'string'
565
+ ```
566
+
548
567
  ### Mash Extension: SafeAssignment
549
568
 
550
569
  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.
@@ -44,6 +44,7 @@ module Hashie
44
44
  end
45
45
 
46
46
  module Mash
47
+ autoload :KeepOriginalKeys, 'hashie/extensions/mash/keep_original_keys'
47
48
  autoload :SafeAssignment, 'hashie/extensions/mash/safe_assignment'
48
49
  autoload :SymbolizeKeys, 'hashie/extensions/mash/symbolize_keys'
49
50
  end
@@ -0,0 +1,54 @@
1
+ module Hashie
2
+ module Extensions
3
+ module Mash
4
+ # Overrides the indifferent access of a Mash to keep keys in the
5
+ # original format given to the Mash.
6
+ #
7
+ # @example
8
+ # class KeepingMash < Hashie::Mash
9
+ # include Hashie::Extensions::Mash::KeepOriginalKeys
10
+ # end
11
+ #
12
+ # mash = KeepingMash.new(:symbol_key => :symbol, 'string_key' => 'string')
13
+ # mash.to_hash #=> { :symbol_key => :symbol, 'string_key' => 'string' }
14
+ # mash['string_key'] == mash[:string_key] #=> true
15
+ # mash[:symbol_key] == mash['symbol_key'] #=> true
16
+ module KeepOriginalKeys
17
+ private
18
+
19
+ def self.included(descendant)
20
+ unless descendant <= Hashie::Mash
21
+ fail ArgumentError, "#{descendant} is not a kind of Hashie::Mash"
22
+ end
23
+ end
24
+
25
+ # Converts the key when necessary to access the correct Mash key.
26
+ #
27
+ # @param [Object, String, Symbol] key the key to access.
28
+ # @return [Object] the value assigned to the key.
29
+ def convert_key(key)
30
+ if regular_key?(key)
31
+ key
32
+ elsif (converted_key = __convert(key)) && regular_key?(converted_key)
33
+ converted_key
34
+ else
35
+ key
36
+ end
37
+ end
38
+
39
+ # Converts symbol/string keys to their alternative formats, but leaves
40
+ # other keys alone.
41
+ #
42
+ # @param [Object, String, Symbol] key the key to convert.
43
+ # @return [Object, String, Symbol] the converted key.
44
+ def __convert(key)
45
+ case key
46
+ when Symbol then key.to_s
47
+ when String then key.to_sym
48
+ else key
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -145,7 +145,7 @@ module Hashie
145
145
  def custom_writer(key, value, convert = true) #:nodoc:
146
146
  key_as_symbol = (key = convert_key(key)).to_sym
147
147
 
148
- log_built_in_message(key_as_symbol) if respond_to?(key_as_symbol)
148
+ log_built_in_message(key_as_symbol) if log_collision?(key_as_symbol)
149
149
  regular_writer(key, convert ? convert_value(value) : value)
150
150
  end
151
151
 
@@ -189,6 +189,7 @@ module Hashie
189
189
  self.class.new(self, default)
190
190
  end
191
191
 
192
+ alias_method :regular_key?, :key?
192
193
  def key?(key)
193
194
  super(convert_key(key))
194
195
  end
@@ -347,5 +348,10 @@ module Hashie
347
348
  'property. You can still access the key via the #[] method.'
348
349
  )
349
350
  end
351
+
352
+ def log_collision?(method_key)
353
+ respond_to?(method_key) && !self.class.disable_warnings? &&
354
+ !(regular_key?(method_key) || regular_key?(method_key.to_s))
355
+ end
350
356
  end
351
357
  end
@@ -1,3 +1,3 @@
1
1
  module Hashie
2
- VERSION = '3.5.4'
2
+ VERSION = '3.5.5'
3
3
  end
@@ -192,6 +192,32 @@ describe DashTest do
192
192
  end
193
193
  end
194
194
 
195
+ context 'converting from a Mash' do
196
+ class ConvertingFromMash < Hashie::Dash
197
+ property :property, required: true
198
+ end
199
+
200
+ context 'without keeping the original keys' do
201
+ let(:mash) { Hashie::Mash.new(property: 'test') }
202
+
203
+ it 'does not pick up the property from the stringified key' do
204
+ expect { ConvertingFromMash.new(mash) }.to raise_error(NoMethodError)
205
+ end
206
+ end
207
+
208
+ context 'when keeping the original keys' do
209
+ class KeepingMash < Hashie::Mash
210
+ include Hashie::Extensions::Mash::KeepOriginalKeys
211
+ end
212
+
213
+ let(:mash) { KeepingMash.new(property: 'test') }
214
+
215
+ it 'picks up the property from the original key' do
216
+ expect { ConvertingFromMash.new(mash) }.not_to raise_error
217
+ end
218
+ end
219
+ end
220
+
195
221
  describe '#new' do
196
222
  it 'fails with non-existent properties' do
197
223
  expect { described_class.new(bork: '') }.to raise_error(*no_property_error('bork'))
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Hashie::Extensions::Mash::KeepOriginalKeys do
4
+ let(:keeping_mash) do
5
+ Class.new(Hashie::Mash) do
6
+ include Hashie::Extensions::Mash::KeepOriginalKeys
7
+ end
8
+ end
9
+
10
+ it 'keeps the keys in the resulting hash identical to the original' do
11
+ original = { :a => 'apple', 'b' => 'bottle' }
12
+ mash = keeping_mash.new(original)
13
+
14
+ expect(mash.to_hash).to eq(original)
15
+ end
16
+
17
+ it 'indifferently responds to keys' do
18
+ original = { :a => 'apple', 'b' => 'bottle' }
19
+ mash = keeping_mash.new(original)
20
+
21
+ expect(mash['a']).to eq(mash[:a])
22
+ expect(mash['b']).to eq(mash[:b])
23
+ end
24
+
25
+ it 'responds to all method accessors like a Mash' do
26
+ original = { :a => 'apple', 'b' => 'bottle' }
27
+ mash = keeping_mash.new(original)
28
+
29
+ expect(mash.a).to eq('apple')
30
+ expect(mash.a?).to eq(true)
31
+ expect(mash.b).to eq('bottle')
32
+ expect(mash.b?).to eq(true)
33
+ expect(mash.underbang_).to be_a(keeping_mash)
34
+ expect(mash.bang!).to be_a(keeping_mash)
35
+ expect(mash.predicate?).to eq(false)
36
+ end
37
+
38
+ it 'keeps the keys that are directly passed without converting them' do
39
+ original = { :a => 'apple', 'b' => 'bottle' }
40
+ mash = keeping_mash.new(original)
41
+
42
+ mash[:c] = 'cat'
43
+ mash['d'] = 'dog'
44
+ expect(mash.to_hash).to eq(:a => 'apple', 'b' => 'bottle', :c => 'cat', 'd' => 'dog')
45
+ end
46
+ end
@@ -142,6 +142,14 @@ describe Hashie::Mash do
142
142
  expect(logger_output).to match('Hashie::Mash#trust')
143
143
  end
144
144
 
145
+ it 'can set keys more than once and does not warn when doing so' do
146
+ mash = Hashie::Mash.new
147
+ mash[:test_key] = 'Test value'
148
+
149
+ expect { mash[:test_key] = 'A new value' }.not_to raise_error
150
+ expect(logger_output).to be_blank
151
+ end
152
+
145
153
  it 'does not write to the logger when warnings are disabled' do
146
154
  mash_class = Class.new(Hashie::Mash) do
147
155
  disable_warnings
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashie
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.4
4
+ version: 3.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-22 00:00:00.000000000 Z
12
+ date: 2017-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -85,6 +85,7 @@ files:
85
85
  - lib/hashie/extensions/ignore_undeclared.rb
86
86
  - lib/hashie/extensions/indifferent_access.rb
87
87
  - lib/hashie/extensions/key_conversion.rb
88
+ - lib/hashie/extensions/mash/keep_original_keys.rb
88
89
  - lib/hashie/extensions/mash/safe_assignment.rb
89
90
  - lib/hashie/extensions/mash/symbolize_keys.rb
90
91
  - lib/hashie/extensions/merge_initializer.rb
@@ -119,6 +120,7 @@ files:
119
120
  - spec/hashie/extensions/indifferent_access_spec.rb
120
121
  - spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb
121
122
  - spec/hashie/extensions/key_conversion_spec.rb
123
+ - spec/hashie/extensions/mash/keep_original_keys_spec.rb
122
124
  - spec/hashie/extensions/mash/safe_assignment_spec.rb
123
125
  - spec/hashie/extensions/mash/symbolize_keys_spec.rb
124
126
  - spec/hashie/extensions/merge_initializer_spec.rb
@@ -187,6 +189,7 @@ test_files:
187
189
  - spec/hashie/extensions/indifferent_access_spec.rb
188
190
  - spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb
189
191
  - spec/hashie/extensions/key_conversion_spec.rb
192
+ - spec/hashie/extensions/mash/keep_original_keys_spec.rb
190
193
  - spec/hashie/extensions/mash/safe_assignment_spec.rb
191
194
  - spec/hashie/extensions/mash/symbolize_keys_spec.rb
192
195
  - spec/hashie/extensions/merge_initializer_spec.rb