hashie 2.1.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +3 -2
- data/CHANGELOG.md +11 -3
- data/Gemfile +1 -1
- data/README.md +72 -79
- data/UPGRADING.md +93 -0
- data/hashie.gemspec +8 -8
- data/lib/hashie.rb +18 -11
- data/lib/hashie/dash.rb +8 -9
- data/lib/hashie/extensions/coercion.rb +1 -1
- data/lib/hashie/extensions/dash/indifferent_access.rb +21 -0
- data/lib/hashie/extensions/deep_fetch.rb +1 -1
- data/lib/hashie/extensions/ignore_undeclared.rb +5 -1
- data/lib/hashie/extensions/key_conversion.rb +0 -82
- data/lib/hashie/extensions/pretty_inspect.rb +19 -0
- data/lib/hashie/extensions/stringify_keys.rb +44 -0
- data/lib/hashie/extensions/symbolize_keys.rb +44 -0
- data/lib/hashie/hash.rb +17 -5
- data/lib/hashie/mash.rb +24 -13
- data/lib/hashie/rash.rb +1 -1
- data/lib/hashie/trash.rb +9 -10
- data/lib/hashie/version.rb +1 -1
- data/spec/hashie/dash_spec.rb +69 -24
- data/spec/hashie/extensions/dash/indifferent_access_spec.rb +58 -0
- data/spec/hashie/extensions/deep_fetch_spec.rb +27 -0
- data/spec/hashie/extensions/ignore_undeclared_spec.rb +36 -13
- data/spec/hashie/extensions/indifferent_access_spec.rb +2 -2
- data/spec/hashie/hash_spec.rb +15 -23
- data/spec/hashie/mash_spec.rb +41 -29
- data/spec/hashie/trash_spec.rb +5 -2
- data/spec/spec_helper.rb +0 -1
- metadata +20 -28
- data/lib/hashie/hash_extensions.rb +0 -47
- data/spec/spec.opts +0 -3
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6033c13b11711b7a5951864fcec0108fc3f93acc
|
4
|
+
data.tar.gz: e8f54e062f1593b56cc1c0ecea1b7cb5d43dd218
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a039149a6d895259bd07ab8a9761bc6d4414b69451c8b8cf4a334e2da448cfd336841d8a7fe481c2911503c4d448276a1d6005240ee0ac18b9b2db00fa0d09e9
|
7
|
+
data.tar.gz: 918c0b19e472b27f71776573e241738fe0d9bd6f1d700b891bd36a7496a5f5410f638b4c98be62f2ce9fa58835e363086ac4322df4de3afc90542e15c30c295b
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
##
|
1
|
+
## 3.0 (6/3/2014)
|
2
2
|
|
3
|
-
|
3
|
+
**Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details.
|
4
|
+
|
5
|
+
* [#150](https://github.com/intridea/hashie/pull/159): Handle nil intermediate object on deep fetch - [@stephenaument](https://github.com/stephenaument).
|
6
|
+
* [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock).
|
7
|
+
* [#152](https://github.com/intridea/hashie/pull/152): Do not convert keys to String in Hashie::Dash and Hashie::Trash, use Hashie::Extensions::Dash::IndifferentAccess to achieve backward compatible behavior - [@dblock](https://github.com/dblock).
|
8
|
+
* [#152](https://github.com/intridea/hashie/pull/152): Do not automatically stringify keys in Hashie::Hash#to_hash, pass `:stringify_keys` to achieve backward compatible behavior - [@dblock](https://github.com/dblock).
|
9
|
+
* [#148](https://github.com/intridea/hashie/pull/148): Consolidated Hashie::Hash#stringify_keys implementation - [@dblock](https://github.com/dblock).
|
10
|
+
* [#149](https://github.com/intridea/hashie/issues/149): Allow IgnoreUndeclared and DeepMerge to be used with undeclared properties - [@jhaesus](https://github.com/jhaesus).
|
11
|
+
* Your contribution here.
|
4
12
|
|
5
13
|
## 2.1.1 (4/12/2014)
|
6
14
|
|
@@ -11,7 +19,7 @@
|
|
11
19
|
* [#134](https://github.com/intridea/hashie/pull/134): Add deep_fetch extension for nested access - [@tylerdooling](https://github.com/tylerdooling).
|
12
20
|
* Removed support for Ruby 1.8.7 - [@dblock](https://github.com/dblock).
|
13
21
|
* Ruby style now enforced with Rubocop - [@dblock](https://github.com/dblock).
|
14
|
-
* [#138](https://github.com/intridea/hashie/pull/138): Added Hashie
|
22
|
+
* [#138](https://github.com/intridea/hashie/pull/138): Added Hashie#Rash, a hash whose keys can be regular expressions or ranges - [@epitron](https://github.com/epitron).
|
15
23
|
* [#131](https://github.com/intridea/hashie/pull/131): Added IgnoreUndeclared, an extension to silently ignore undeclared properties at intialization - [@righi](https://github.com/righi).
|
16
24
|
* [#136](https://github.com/intridea/hashie/issues/136): Removed Hashie::Extensions::Structure - [@markiz](https://github.com/markiz).
|
17
25
|
* [#107](https://github.com/intridea/hashie/pull/107): Fixed excessive value conversions, poor performance of deep merge in Hashie::Mash - [@davemitchell](https://github.com/dblock), [@dblock](https://github.com/dblock).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
# Hashie [![Build Status](https://secure.travis-ci.org/intridea/hashie.png)](http://travis-ci.org/intridea/hashie) [![Dependency Status](https://gemnasium.com/intridea/hashie.png)](https://gemnasium.com/intridea/hashie)
|
1
|
+
# Hashie [![Build Status](https://secure.travis-ci.org/intridea/hashie.png)](http://travis-ci.org/intridea/hashie) [![Dependency Status](https://gemnasium.com/intridea/hashie.png)](https://gemnasium.com/intridea/hashie) [![Code Climate](https://codeclimate.com/github/intridea/hashie.png)](https://codeclimate.com/github/intridea/hashie)
|
2
2
|
|
3
|
-
Hashie is a growing collection of tools that extend Hashes and make
|
4
|
-
them more useful.
|
3
|
+
Hashie is a growing collection of tools that extend Hashes and make them more useful.
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
@@ -11,22 +10,19 @@ Hashie is available as a RubyGem:
|
|
11
10
|
$ gem install hashie
|
12
11
|
```
|
13
12
|
|
13
|
+
## Upgrading
|
14
|
+
|
15
|
+
You're reading the documentation for the stable release of Hashie, 3.0. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
|
16
|
+
|
14
17
|
## Hash Extensions
|
15
18
|
|
16
|
-
The library is broken up into a number of atomically includeable Hash
|
17
|
-
extension modules as described below. This provides maximum flexibility
|
18
|
-
for users to mix and match functionality while maintaining feature parity
|
19
|
-
with earlier versions of Hashie.
|
19
|
+
The library is broken up into a number of atomically includeable Hash extension modules as described below. This provides maximum flexibility for users to mix and match functionality while maintaining feature parity with earlier versions of Hashie.
|
20
20
|
|
21
|
-
Any of the extensions listed below can be mixed into a class by
|
22
|
-
`include`-ing `Hashie::Extensions::ExtensionName`.
|
21
|
+
Any of the extensions listed below can be mixed into a class by `include`-ing `Hashie::Extensions::ExtensionName`.
|
23
22
|
|
24
23
|
### Coercion
|
25
24
|
|
26
|
-
Coercions allow you to set up "coercion rules" based either on the key
|
27
|
-
or the value type to massage data as it's being inserted into the Hash.
|
28
|
-
Key coercions might be used, for example, in lightweight data modeling
|
29
|
-
applications such as an API client:
|
25
|
+
Coercions allow you to set up "coercion rules" based either on the key or the value type to massage data as it's being inserted into the Hash. Key coercions might be used, for example, in lightweight data modeling applications such as an API client:
|
30
26
|
|
31
27
|
```ruby
|
32
28
|
class Tweet < Hash
|
@@ -40,9 +36,7 @@ Tweet.new(user: user_hash)
|
|
40
36
|
# User.new(user_hash) if that isn't present.
|
41
37
|
```
|
42
38
|
|
43
|
-
Value coercions, on the other hand, will coerce values based on the type
|
44
|
-
of the value being inserted. This is useful if you are trying to build a
|
45
|
-
Hash-like class that is self-propagating.
|
39
|
+
Value coercions, on the other hand, will coerce values based on the type of the value being inserted. This is useful if you are trying to build a Hash-like class that is self-propagating.
|
46
40
|
|
47
41
|
```ruby
|
48
42
|
class SpecialHash < Hash
|
@@ -60,22 +54,15 @@ end
|
|
60
54
|
|
61
55
|
### KeyConversion
|
62
56
|
|
63
|
-
The KeyConversion extension gives you the convenience methods of
|
64
|
-
`symbolize_keys` and `stringify_keys` along with their bang
|
65
|
-
counterparts. You can also include just stringify or just symbolize with
|
66
|
-
`Hashie::Extensions::StringifyKeys` or `Hashie::Extensions::SymbolizeKeys`.
|
57
|
+
The KeyConversion extension gives you the convenience methods of `symbolize_keys` and `stringify_keys` along with their bang counterparts. You can also include just stringify or just symbolize with `Hashie::Extensions::StringifyKeys` or `Hashie::Extensions::SymbolizeKeys`.
|
67
58
|
|
68
59
|
### MergeInitializer
|
69
60
|
|
70
|
-
The MergeInitializer extension simply makes it possible to initialize a
|
71
|
-
Hash subclass with another Hash, giving you a quick short-hand.
|
61
|
+
The MergeInitializer extension simply makes it possible to initialize a Hash subclass with another Hash, giving you a quick short-hand.
|
72
62
|
|
73
63
|
### MethodAccess
|
74
64
|
|
75
|
-
The MethodAccess extension allows you to quickly build method-based
|
76
|
-
reading, writing, and querying into your Hash descendant. It can also be
|
77
|
-
included as individual modules, i.e. `Hashie::Extensions::MethodReader`,
|
78
|
-
`Hashie::Extensions::MethodWriter` and `Hashie::Extensions::MethodQuery`
|
65
|
+
The MethodAccess extension allows you to quickly build method-based reading, writing, and querying into your Hash descendant. It can also be included as individual modules, i.e. `Hashie::Extensions::MethodReader`, `Hashie::Extensions::MethodWriter` and `Hashie::Extensions::MethodQuery`.
|
79
66
|
|
80
67
|
```ruby
|
81
68
|
class MyHash < Hash
|
@@ -90,22 +77,15 @@ h.abc? # => true
|
|
90
77
|
|
91
78
|
### IndifferentAccess
|
92
79
|
|
93
|
-
This extension can be mixed in to instantly give you indifferent access
|
94
|
-
|
95
|
-
|
96
|
-
keys, you will get the same results.
|
80
|
+
This extension can be mixed in to instantly give you indifferent access to your Hash subclass. This works just like the params hash in Rails and other frameworks where whether you provide symbols or strings to access keys, you will get the same results.
|
81
|
+
|
82
|
+
A unique feature of Hashie's IndifferentAccess mixin is that it will inject itself recursively into subhashes *without* reinitializing the hash in question. This means you can safely merge together indifferent and non-indifferent hashes arbitrarily deeply without worrying about whether you'll be able to `hash[:other][:another]` properly.
|
97
83
|
|
98
|
-
|
99
|
-
inject itself recursively into subhashes *without* reinitializing the
|
100
|
-
hash in question. This means you can safely merge together indifferent
|
101
|
-
and non-indifferent hashes arbitrarily deeply without worrying about
|
102
|
-
whether you'll be able to `hash[:other][:another]` properly.
|
84
|
+
Use `Hashie::Extensions::Dash::IndifferentAccess` for instances of `Hashie::Dash`.
|
103
85
|
|
104
86
|
### IgnoreUndeclared
|
105
87
|
|
106
|
-
This extension can be mixed in to silently ignore undeclared properties on
|
107
|
-
initialization instead of raising an error. This is useful when using a
|
108
|
-
Trash to capture a subset of a larger hash.
|
88
|
+
This extension can be mixed in to silently ignore undeclared properties on initialization instead of raising an error. This is useful when using a Trash to capture a subset of a larger hash.
|
109
89
|
|
110
90
|
```ruby
|
111
91
|
class Person < Trash
|
@@ -143,19 +123,15 @@ h2 = MyHash.new
|
|
143
123
|
h1 = { x: { y: [4,5,6] }, z: [7,8,9] }
|
144
124
|
h2 = { x: { y: [7,8,9] }, z: "xyz" }
|
145
125
|
|
146
|
-
h1.deep_merge(h2)
|
147
|
-
h2.deep_merge(h1)
|
126
|
+
h1.deep_merge(h2) # => { x: { y: [7, 8, 9] }, z: "xyz" }
|
127
|
+
h2.deep_merge(h1) # => { x: { y: [4, 5, 6] }, z: [7, 8, 9] }
|
148
128
|
```
|
149
129
|
|
150
130
|
### DeepFetch
|
151
131
|
|
152
|
-
This extension can be mixed in to provide for safe and concise retrieval of
|
153
|
-
deeply nested hash values. In the event that the requested key does not exist
|
154
|
-
a block can be provided and its value will be returned.
|
132
|
+
This extension can be mixed in to provide for safe and concise retrieval of deeply nested hash values. In the event that the requested key does not exist a block can be provided and its value will be returned.
|
155
133
|
|
156
|
-
Though this is a hash extension, it conveniently allows for arrays to be
|
157
|
-
present in the nested structure. This feature makes the extension particularly
|
158
|
-
useful for working with JSON API responses.
|
134
|
+
Though this is a hash extension, it conveniently allows for arrays to be present in the nested structure. This feature makes the extension particularly useful for working with JSON API responses.
|
159
135
|
|
160
136
|
```ruby
|
161
137
|
user = {
|
@@ -165,24 +141,22 @@ user = {
|
|
165
141
|
{ name: 'Open source enthusiasts' }
|
166
142
|
]
|
167
143
|
}
|
144
|
+
|
168
145
|
user.extend Hashie::Extensions::DeepFetch
|
169
146
|
|
170
|
-
user.deep_fetch :name, :first
|
171
|
-
user.deep_fetch :name, :middle
|
147
|
+
user.deep_fetch :name, :first # => 'Bob'
|
148
|
+
user.deep_fetch :name, :middle # => 'KeyError: Could not fetch middle'
|
172
149
|
|
173
150
|
# using a default block
|
174
|
-
user.deep_fetch :name, :middle { |key| 'default' }
|
151
|
+
user.deep_fetch :name, :middle { |key| 'default' } # => 'default'
|
175
152
|
|
176
153
|
# a nested array
|
177
|
-
user.deep_fetch :groups, 1, :name
|
154
|
+
user.deep_fetch :groups, 1, :name # => 'Open source enthusiasts'
|
178
155
|
```
|
179
156
|
|
180
157
|
## Mash
|
181
158
|
|
182
|
-
Mash is an extended Hash that gives simple pseudo-object functionality
|
183
|
-
that can be built from hashes and easily extended. It is designed to
|
184
|
-
be used in RESTful API libraries to provide easy object-like access
|
185
|
-
to JSON and XML parsed hashes.
|
159
|
+
Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended. It is designed to be used in RESTful API libraries to provide easy object-like access to JSON and XML parsed hashes.
|
186
160
|
|
187
161
|
### Example:
|
188
162
|
|
@@ -195,27 +169,22 @@ mash.name # => "My Mash"
|
|
195
169
|
mash.name? # => true
|
196
170
|
mash.inspect # => <Hashie::Mash name="My Mash">
|
197
171
|
|
198
|
-
mash = Mash.new
|
172
|
+
mash = Hashie::Mash.new
|
199
173
|
# use bang methods for multi-level assignment
|
200
174
|
mash.author!.name = "Michael Bleigh"
|
201
175
|
mash.author # => <Hashie::Mash name="Michael Bleigh">
|
202
176
|
|
203
|
-
mash = Mash.new
|
177
|
+
mash = Hashie::Mash.new
|
204
178
|
# use under-bang methods for multi-level testing
|
205
179
|
mash.author_.name? # => false
|
206
180
|
mash.inspect # => <Hashie::Mash>
|
207
181
|
```
|
208
182
|
|
209
|
-
**Note:** The `?` method will return false if a key has been set
|
210
|
-
to false or nil. In order to check if a key has been set at all, use the
|
211
|
-
`mash.key?('some_key')` method instead.
|
183
|
+
**Note:** The `?` method will return false if a key has been set to false or nil. In order to check if a key has been set at all, use the `mash.key?('some_key')` method instead.
|
212
184
|
|
213
185
|
## Dash
|
214
186
|
|
215
|
-
Dash is an extended Hash that has a discrete set of defined properties
|
216
|
-
and only those properties may be set on the hash. Additionally, you
|
217
|
-
can set defaults for each property. You can also flag a property as
|
218
|
-
required. Required properties will raise an exception if unset.
|
187
|
+
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.
|
219
188
|
|
220
189
|
### Example:
|
221
190
|
|
@@ -238,10 +207,40 @@ p[:awesome] # => NoMethodError
|
|
238
207
|
p[:occupation] # => 'Rubyist'
|
239
208
|
```
|
240
209
|
|
210
|
+
Properties defined as symbols are not the same thing as properties defined as strings.
|
211
|
+
|
212
|
+
### Example:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
class Tricky < Hashie::Dash
|
216
|
+
property :trick
|
217
|
+
property 'trick'
|
218
|
+
end
|
219
|
+
|
220
|
+
p = Tricky.new(trick: 'one', 'trick' => 'two')
|
221
|
+
p.trick # => 'one', always symbol version
|
222
|
+
p[:trick] # => 'one'
|
223
|
+
p['trick'] # => 'two'
|
224
|
+
```
|
225
|
+
|
226
|
+
Note that accessing a property as a method always uses the symbol version.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
class Tricky < Hashie::Dash
|
230
|
+
property 'trick'
|
231
|
+
end
|
232
|
+
|
233
|
+
p = Tricky.new('trick' => 'two')
|
234
|
+
p.trick # => NoMethodError
|
235
|
+
```
|
236
|
+
|
237
|
+
### Mash and Rails 4 Strong Parameters
|
238
|
+
|
239
|
+
To enable compatibility with Rails 4 use the [hashie_rails](http://rubygems.org/gems/hashie_rails) gem.
|
240
|
+
|
241
241
|
## Trash
|
242
242
|
|
243
|
-
A Trash is a Dash that allows you to translate keys on initialization.
|
244
|
-
It is used like so:
|
243
|
+
A Trash is a Dash that allows you to translate keys on initialization. It is used like so:
|
245
244
|
|
246
245
|
```ruby
|
247
246
|
class Person < Hashie::Trash
|
@@ -256,8 +255,7 @@ when it is initialized using a hash such as through:
|
|
256
255
|
Person.new(firstName: 'Bob')
|
257
256
|
```
|
258
257
|
|
259
|
-
Trash also supports translations using lambda, this could be useful when dealing with
|
260
|
-
external API's. You can use it in this way:
|
258
|
+
Trash also supports translations using lambda, this could be useful when dealing with external API's. You can use it in this way:
|
261
259
|
|
262
260
|
```ruby
|
263
261
|
class Result < Hashie::Trash
|
@@ -276,13 +274,9 @@ result.created_at.class # => Time
|
|
276
274
|
|
277
275
|
## Clash
|
278
276
|
|
279
|
-
Clash is a Chainable Lazy Hash that allows you to easily construct
|
280
|
-
complex hashes using method notation chaining. This will allow you
|
281
|
-
to use a more action-oriented approach to building options hashes.
|
277
|
+
Clash is a Chainable Lazy Hash that allows you to easily construct complex hashes using method notation chaining. This will allow you to use a more action-oriented approach to building options hashes.
|
282
278
|
|
283
|
-
Essentially, a Clash is a generalized way to provide much of the same
|
284
|
-
kind of "chainability" that libraries like Arel or Rails 2.x's named_scopes
|
285
|
-
provide.
|
279
|
+
Essentially, a Clash is a generalized way to provide much of the same kind of "chainability" that libraries like Arel or Rails 2.x's named_scopes provide.
|
286
280
|
|
287
281
|
### Example:
|
288
282
|
|
@@ -317,22 +311,21 @@ If the Rash's value is a `proc`, the `proc` will be automatically called with th
|
|
317
311
|
|
318
312
|
# Mapping names to appropriate greetings
|
319
313
|
greeting = Hashie::Rash.new( /^Mr./ => "Hello sir!", /^Mrs./ => "Evening, madame." )
|
320
|
-
greeting["Mr. Steve Austin"]
|
321
|
-
greeting["Mrs. Steve Austin"]
|
314
|
+
greeting["Mr. Steve Austin"] # => "Hello sir!"
|
315
|
+
greeting["Mrs. Steve Austin"] # => "Evening, madame."
|
322
316
|
|
323
317
|
# Mapping statements to saucy retorts
|
324
318
|
mapper = Hashie::Rash.new(
|
325
319
|
/I like (.+)/ => proc { |m| "Who DOESN'T like #{m[1]}?!" },
|
326
320
|
/Get off my (.+)!/ => proc { |m| "Forget your #{m[1]}, old man!" }
|
327
321
|
)
|
328
|
-
mapper["I like traffic lights"]
|
329
|
-
mapper["Get off my lawn!"]
|
322
|
+
mapper["I like traffic lights"] # => "Who DOESN'T like traffic lights?!"
|
323
|
+
mapper["Get off my lawn!"] # => "Forget your lawn, old man!"
|
330
324
|
```
|
331
325
|
|
332
326
|
### Auto-optimized
|
333
327
|
|
334
|
-
**Note:** The Rash is automatically optimized every 500 accesses
|
335
|
-
(which means that it sorts the list of Regexps, putting the most frequently matched ones at the beginning).
|
328
|
+
**Note:** The Rash is automatically optimized every 500 accesses (which means that it sorts the list of Regexps, putting the most frequently matched ones at the beginning).
|
336
329
|
|
337
330
|
If this value is too low or too high for your needs, you can tune it by setting: `rash.optimize_every = n`.
|
338
331
|
|
data/UPGRADING.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
Upgrading Hashie
|
2
|
+
================
|
3
|
+
|
4
|
+
### Upgrading to 3.0
|
5
|
+
|
6
|
+
#### Compatibility with Rails 4 Strong Parameters
|
7
|
+
|
8
|
+
Version 2.1 introduced support to prevent default Rails 4 mass-assignment protection behavior. This was [issue #89](https://github.com/intridea/hashie/issues/89), resolved in [#104](https://github.com/intridea/hashie/pull/104). In version 2.2 this behavior has been removed in [#147](https://github.com/intridea/hashie/pull/147) in favor of a mixin and finally extracted into a separate gem in Hashie 3.0.
|
9
|
+
|
10
|
+
To enable 2.1 compatible behavior with Rails 4, use the [hashie_rails](http://rubygems.org/gems/hashie_rails) gem.
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'hashie_rails'
|
14
|
+
```
|
15
|
+
|
16
|
+
See [#154](https://github.com/intridea/hashie/pull/154) and [Mash and Rails 4 Strong Parameters](README.md#mash-and-rails-4-strong-parameters) for more details.
|
17
|
+
|
18
|
+
#### Key Conversions in Hashie::Dash and Hashie::Trash
|
19
|
+
|
20
|
+
Version 2.1 and older of Hashie::Dash and Hashie::Trash converted keys to strings by default. This is no longer the case in 2.2.
|
21
|
+
|
22
|
+
Consider the following code.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class Person < Hashie::Dash
|
26
|
+
property :name
|
27
|
+
end
|
28
|
+
|
29
|
+
p = Person.new(name: 'dB.')
|
30
|
+
```
|
31
|
+
|
32
|
+
Version 2.1 behaves as follows.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
p.name # => 'dB.'
|
36
|
+
p[:name] # => 'dB.'
|
37
|
+
p['name'] # => 'dB.'
|
38
|
+
|
39
|
+
# not what I put in
|
40
|
+
p.inspect # => { 'name' => 'dB.' }
|
41
|
+
p.to_hash # => { 'name' => 'dB.' }
|
42
|
+
```
|
43
|
+
|
44
|
+
It was not possible to achieve the behavior of preserving keys, as described in [issue #151](https://github.com/intridea/hashie/issues/151).
|
45
|
+
|
46
|
+
Version 2.2 does not perform this conversion by default.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
p.name # => 'dB.'
|
50
|
+
p[:name] # => 'dB.'
|
51
|
+
# p['name'] # => NoMethodError
|
52
|
+
|
53
|
+
p.inspect # => { :name => 'dB.' }
|
54
|
+
p.to_hash # => { :name => 'dB.' }
|
55
|
+
```
|
56
|
+
|
57
|
+
To enable behavior compatible with older versions, use `Hashie::Extensions::Dash::IndifferentAccess`.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class Person < Hashie::Dash
|
61
|
+
include Hashie::Extensions::Dash::IndifferentAccess
|
62
|
+
property :name
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
#### Key Conversions in Hashie::Hash#to_hash
|
67
|
+
|
68
|
+
Version 2.1 or older of Hash#to_hash converted keys to strings automatically.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
instance = Hashie::Hash[first: 'First', 'last' => 'Last']
|
72
|
+
instance.to_hash # => { "first" => 'First', "last" => 'Last' }
|
73
|
+
```
|
74
|
+
|
75
|
+
It was possible to symbolize keys by passing `:symbolize_keys`, however it was not possible to retrieve the hash with initial key values.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
instance.to_hash(symbolize_keys: true) # => { :first => 'First', :last => 'Last' }
|
79
|
+
instance.to_hash(stringify_keys: true) # => { "first" => 'First', "last" => 'Last' }
|
80
|
+
```
|
81
|
+
|
82
|
+
Version 2.2 no longer converts keys by default.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
instance = Hashie::Hash[first: 'First', 'last' => 'Last']
|
86
|
+
instance.to_hash # => { :first => 'First', "last" => 'Last' }
|
87
|
+
```
|
88
|
+
|
89
|
+
The behavior with `symbolize_keys` and `stringify_keys` is unchanged.
|
90
|
+
|
91
|
+
See [#152](https://github.com/intridea/hashie/pull/152) for more information.
|
92
|
+
|
93
|
+
|
data/hashie.gemspec
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
require File.expand_path('../lib/hashie/version', __FILE__)
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
|
-
gem.authors = [
|
5
|
-
gem.email = [
|
6
|
-
gem.description =
|
7
|
-
gem.summary =
|
4
|
+
gem.authors = ['Michael Bleigh', 'Jerry Cheung']
|
5
|
+
gem.email = ['michael@intridea.com', 'jollyjerry@gmail.com']
|
6
|
+
gem.description = 'Hashie is a collection of classes and mixins that make hashes more powerful.'
|
7
|
+
gem.summary = 'Your friendly neighborhood hash library.'
|
8
8
|
gem.homepage = 'https://github.com/intridea/hashie'
|
9
9
|
|
10
|
-
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
10
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
11
11
|
gem.files = `git ls-files`.split("\n")
|
12
12
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
-
gem.name =
|
13
|
+
gem.name = 'hashie'
|
14
14
|
gem.require_paths = ['lib']
|
15
15
|
gem.version = Hashie::VERSION
|
16
|
-
gem.license =
|
16
|
+
gem.license = 'MIT'
|
17
17
|
|
18
18
|
gem.add_development_dependency 'rake'
|
19
|
-
gem.add_development_dependency 'rspec'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 3.0'
|
20
20
|
end
|