hashie 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/CHANGELOG.md +9 -0
- data/lib/hashie/dash.rb +7 -0
- data/lib/hashie/extensions/coercion.rb +6 -0
- data/lib/hashie/extensions/indifferent_access.rb +8 -2
- data/lib/hashie/mash.rb +29 -23
- data/lib/hashie/version.rb +1 -1
- data/spec/hashie/clash_spec.rb +8 -0
- data/spec/hashie/dash_spec.rb +27 -0
- data/spec/hashie/extensions/coercion_spec.rb +31 -1
- data/spec/hashie/extensions/indifferent_access_spec.rb +25 -0
- data/spec/hashie/mash_spec.rb +43 -0
- data/spec/hashie/trash_spec.rb +10 -0
- metadata +45 -14
- checksums.yaml +0 -7
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 2.0.3
|
4
|
+
|
5
|
+
* Hashie::Mash.new(abc: true).respond_to?(:abc?) works 7even #88
|
6
|
+
* Fix #replace jimeh #68
|
7
|
+
|
8
|
+
## 2.0.2
|
9
|
+
|
10
|
+
* adding symbolize_keys back to to_hash cromulus #85
|
11
|
+
|
3
12
|
## 2.0.1
|
4
13
|
|
5
14
|
* remove Mash#object_id override matschaffer #81
|
data/lib/hashie/dash.rb
CHANGED
@@ -127,6 +127,13 @@ module Hashie
|
|
127
127
|
super(property.to_s, value)
|
128
128
|
end
|
129
129
|
|
130
|
+
def replace(other_hash)
|
131
|
+
other_hash = self.class.defaults.merge(other_hash)
|
132
|
+
(keys - other_hash.keys).each { |key| delete(key) }
|
133
|
+
other_hash.each { |key, value| self[key] = value }
|
134
|
+
self
|
135
|
+
end
|
136
|
+
|
130
137
|
private
|
131
138
|
|
132
139
|
def initialize_attributes(attributes)
|
@@ -27,7 +27,7 @@ module Hashie
|
|
27
27
|
base.class_eval do
|
28
28
|
alias_method :regular_writer, :[]=
|
29
29
|
alias_method :[]=, :indifferent_writer
|
30
|
-
%w(default update fetch delete key? values_at).each do |m|
|
30
|
+
%w(default update replace fetch delete key? values_at).each do |m|
|
31
31
|
alias_method "regular_#{m}", m
|
32
32
|
alias_method m, "indifferent_#{m}"
|
33
33
|
end
|
@@ -95,7 +95,13 @@ module Hashie
|
|
95
95
|
def indifferent_values_at(*indices); indices.map{|i| self[i] } end
|
96
96
|
|
97
97
|
def indifferent_access?; true end
|
98
|
-
|
98
|
+
|
99
|
+
def indifferent_replace(other_hash)
|
100
|
+
(keys - other_hash.keys).each { |key| delete(key) }
|
101
|
+
other_hash.each { |key, value| self[key] = value }
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
99
105
|
protected
|
100
106
|
|
101
107
|
def hash_lacking_indifference?(other)
|
data/lib/hashie/mash.rb
CHANGED
@@ -175,29 +175,35 @@ module Hashie
|
|
175
175
|
self
|
176
176
|
end
|
177
177
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
178
|
+
def replace(other_hash)
|
179
|
+
(keys - other_hash.keys).each { |key| delete(key) }
|
180
|
+
other_hash.each { |key, value| self[key] = value }
|
181
|
+
self
|
182
|
+
end
|
183
|
+
|
184
|
+
# Will return true if the Mash has had a key
|
185
|
+
# set in addition to normal respond_to? functionality.
|
186
|
+
def respond_to?(method_name, include_private=false)
|
187
|
+
return true if key?(method_name) || method_name.to_s.slice(/[=?!_]\Z/)
|
188
|
+
super
|
189
|
+
end
|
190
|
+
|
191
|
+
def method_missing(method_name, *args, &blk)
|
192
|
+
return self.[](method_name, &blk) if key?(method_name)
|
193
|
+
match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
|
194
|
+
case match[2]
|
195
|
+
when "="
|
196
|
+
self[match[1]] = args.first
|
197
|
+
when "?"
|
198
|
+
!!self[match[1]]
|
199
|
+
when "!"
|
200
|
+
initializing_reader(match[1])
|
201
|
+
when "_"
|
202
|
+
underbang_reader(match[1])
|
203
|
+
else
|
204
|
+
default(method_name, *args, &blk)
|
205
|
+
end
|
206
|
+
end
|
201
207
|
|
202
208
|
protected
|
203
209
|
|
data/lib/hashie/version.rb
CHANGED
data/spec/hashie/clash_spec.rb
CHANGED
@@ -39,4 +39,12 @@ describe Hashie::Clash do
|
|
39
39
|
@c.where(:abc => 'def').where(:hgi => 123)
|
40
40
|
@c.should == {:where => {:abc => 'def', :hgi => 123}}
|
41
41
|
end
|
42
|
+
|
43
|
+
it 'should be able to replace all of its own keys with #replace' do
|
44
|
+
@c.foo(:bar).hello(:world)
|
45
|
+
@c.replace(:baz => 123, :hgi => 123).should == {:baz => 123, :hgi => 123}
|
46
|
+
@c.should == {:baz => 123, :hgi => 123}
|
47
|
+
@c[:foo].should be_nil
|
48
|
+
@c[:hello].should be_nil
|
49
|
+
end
|
42
50
|
end
|
data/spec/hashie/dash_spec.rb
CHANGED
@@ -170,6 +170,33 @@ describe DashTest do
|
|
170
170
|
described_class.defaults.should == { :count => 0 }
|
171
171
|
end
|
172
172
|
end
|
173
|
+
|
174
|
+
describe '#replace' do
|
175
|
+
before { subject.replace(:first_name => "Cain") }
|
176
|
+
|
177
|
+
it 'return self' do
|
178
|
+
subject.replace(:email => "bar").to_hash.
|
179
|
+
should == {"email" => "bar", "count" => 0}
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'sets all specified keys to their corresponding values' do
|
183
|
+
subject.first_name.should == "Cain"
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'leaves only specified keys and keys with default values' do
|
187
|
+
subject.keys.sort.should == ['count', 'first_name']
|
188
|
+
subject.email.should be_nil
|
189
|
+
subject.count.should == 0
|
190
|
+
end
|
191
|
+
|
192
|
+
context 'when replacing keys with default values' do
|
193
|
+
before { subject.replace(:count => 3) }
|
194
|
+
|
195
|
+
it 'sets all specified keys to their corresponding values' do
|
196
|
+
subject.count.should == 3
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
173
200
|
end
|
174
201
|
|
175
202
|
describe Hashie::Dash, 'inheritance' do
|
@@ -58,15 +58,45 @@ describe Hashie::Extensions::Coercion do
|
|
58
58
|
|
59
59
|
instance[:foo].should be_coerced
|
60
60
|
end
|
61
|
+
|
62
|
+
context 'when #replace is used' do
|
63
|
+
before { subject.coerce_key :foo, :bar, Coercable }
|
64
|
+
|
65
|
+
let(:instance) do
|
66
|
+
subject.new(:foo => "bar").
|
67
|
+
replace(:foo => "foz", :bar => "baz", :hi => "bye")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should coerce relevant keys" do
|
71
|
+
instance[:foo].should be_coerced
|
72
|
+
instance[:bar].should be_coerced
|
73
|
+
instance[:hi].should_not respond_to(:coerced?)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should set correct values" do
|
77
|
+
instance[:hi].should == "bye"
|
78
|
+
end
|
79
|
+
end
|
61
80
|
end
|
62
81
|
|
63
82
|
describe '.coerce_value' do
|
64
83
|
context 'with :strict => true' do
|
65
84
|
it 'should coerce any value of the exact right class' do
|
66
85
|
subject.coerce_value String, Coercable
|
67
|
-
|
86
|
+
|
68
87
|
instance[:foo] = "bar"
|
69
88
|
instance[:bar] = "bax"
|
89
|
+
instance[:hi] = :bye
|
90
|
+
instance[:foo].should be_coerced
|
91
|
+
instance[:bar].should be_coerced
|
92
|
+
instance[:hi].should_not respond_to(:coerced?)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should coerce values from a #replace call' do
|
96
|
+
subject.coerce_value String, Coercable
|
97
|
+
|
98
|
+
instance[:foo] = :bar
|
99
|
+
instance.replace(:foo => "bar", :bar => "bax")
|
70
100
|
instance[:foo].should be_coerced
|
71
101
|
instance[:bar].should be_coerced
|
72
102
|
end
|
@@ -71,4 +71,29 @@ describe Hashie::Extensions::IndifferentAccess do
|
|
71
71
|
Hash.new.should_not be_respond_to(:indifferent_access?)
|
72
72
|
end
|
73
73
|
end
|
74
|
+
|
75
|
+
describe '#replace' do
|
76
|
+
subject do
|
77
|
+
IndifferentHash.new(:foo => 'bar').replace(:bar => 'baz', :hi => 'bye')
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'returns self' do
|
81
|
+
subject.should be_a(IndifferentHash)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should remove old keys' do
|
85
|
+
[:foo, 'foo'].each do |k|
|
86
|
+
subject[k].should be_nil
|
87
|
+
subject.key?(k).should be_false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'creates new keys with indifferent access' do
|
92
|
+
[:bar, 'bar', :hi, 'hi'].each { |k| subject.key?(k).should be_true }
|
93
|
+
subject[:bar].should == 'baz'
|
94
|
+
subject['bar'].should == 'baz'
|
95
|
+
subject[:hi].should == 'bye'
|
96
|
+
subject['hi'].should == 'bye'
|
97
|
+
end
|
98
|
+
end
|
74
99
|
end
|
data/spec/hashie/mash_spec.rb
CHANGED
@@ -183,6 +183,33 @@ describe Hashie::Mash do
|
|
183
183
|
end
|
184
184
|
end
|
185
185
|
|
186
|
+
describe '#replace' do
|
187
|
+
before do
|
188
|
+
subject.replace(:middle_name => "Cain",
|
189
|
+
:details => {:city => "Imagination"})
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'return self' do
|
193
|
+
subject.replace(:foo => "bar").to_hash.should == {"foo" => "bar"}
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'sets all specified keys to their corresponding values' do
|
197
|
+
subject.middle_name?.should be_true
|
198
|
+
subject.details?.should be_true
|
199
|
+
subject.middle_name.should == "Cain"
|
200
|
+
subject.details.city?.should be_true
|
201
|
+
subject.details.city.should == "Imagination"
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'leaves only specified keys' do
|
205
|
+
subject.keys.sort.should == ['details', 'middle_name']
|
206
|
+
subject.first_name?.should be_false
|
207
|
+
subject.should_not respond_to(:first_name)
|
208
|
+
subject.last_name?.should be_false
|
209
|
+
subject.should_not respond_to(:last_name)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
186
213
|
describe 'delete' do
|
187
214
|
it 'should delete with String key' do
|
188
215
|
subject.delete('details')
|
@@ -270,6 +297,22 @@ describe Hashie::Mash do
|
|
270
297
|
it 'should respond to a set key' do
|
271
298
|
Hashie::Mash.new(:abc => 'def').should be_respond_to(:abc)
|
272
299
|
end
|
300
|
+
|
301
|
+
it 'should respond to a set key with a suffix' do
|
302
|
+
%w(= ? ! _).each do |suffix|
|
303
|
+
Hashie::Mash.new(:abc => 'def').should be_respond_to(:"abc#{suffix}")
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
it 'should respond to an unknown key with a suffix' do
|
308
|
+
%w(= ? ! _).each do |suffix|
|
309
|
+
Hashie::Mash.new(:abc => 'def').should be_respond_to(:"xyz#{suffix}")
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should not respond to an unknown key without a suffix" do
|
314
|
+
Hashie::Mash.new(:abc => 'def').should_not be_respond_to(:xyz)
|
315
|
+
end
|
273
316
|
end
|
274
317
|
|
275
318
|
context "#initialize" do
|
data/spec/hashie/trash_spec.rb
CHANGED
@@ -52,6 +52,16 @@ describe Hashie::Trash do
|
|
52
52
|
trash.firstName = 'Franklin'
|
53
53
|
trash.first_name.should == 'Franklin'
|
54
54
|
end
|
55
|
+
|
56
|
+
it 'writes to a translated property using #replace' do
|
57
|
+
trash.replace(:firstName => 'Franklin')
|
58
|
+
trash.first_name.should == 'Franklin'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'writes to a non-translated property using #replace' do
|
62
|
+
trash.replace(:first_name => 'Franklin')
|
63
|
+
trash.first_name.should == 'Franklin'
|
64
|
+
end
|
55
65
|
end
|
56
66
|
|
57
67
|
describe ' initializing with a Hash' do
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Michael Bleigh
|
@@ -9,11 +10,12 @@ authors:
|
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
+
date: 2013-03-18 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rake
|
16
17
|
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
17
19
|
requirements:
|
18
20
|
- - ~>
|
19
21
|
- !ruby/object:Gem::Version
|
@@ -21,6 +23,7 @@ dependencies:
|
|
21
23
|
type: :development
|
22
24
|
prerelease: false
|
23
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
24
27
|
requirements:
|
25
28
|
- - ~>
|
26
29
|
- !ruby/object:Gem::Version
|
@@ -28,6 +31,7 @@ dependencies:
|
|
28
31
|
- !ruby/object:Gem::Dependency
|
29
32
|
name: rspec
|
30
33
|
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
31
35
|
requirements:
|
32
36
|
- - ~>
|
33
37
|
- !ruby/object:Gem::Version
|
@@ -35,6 +39,7 @@ dependencies:
|
|
35
39
|
type: :development
|
36
40
|
prerelease: false
|
37
41
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
38
43
|
requirements:
|
39
44
|
- - ~>
|
40
45
|
- !ruby/object:Gem::Version
|
@@ -42,43 +47,49 @@ dependencies:
|
|
42
47
|
- !ruby/object:Gem::Dependency
|
43
48
|
name: guard
|
44
49
|
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
45
51
|
requirements:
|
46
|
-
- - '>='
|
52
|
+
- - ! '>='
|
47
53
|
- !ruby/object:Gem::Version
|
48
54
|
version: '0'
|
49
55
|
type: :development
|
50
56
|
prerelease: false
|
51
57
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
52
59
|
requirements:
|
53
|
-
- - '>='
|
60
|
+
- - ! '>='
|
54
61
|
- !ruby/object:Gem::Version
|
55
62
|
version: '0'
|
56
63
|
- !ruby/object:Gem::Dependency
|
57
64
|
name: guard-rspec
|
58
65
|
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
59
67
|
requirements:
|
60
|
-
- - '>='
|
68
|
+
- - ! '>='
|
61
69
|
- !ruby/object:Gem::Version
|
62
70
|
version: '0'
|
63
71
|
type: :development
|
64
72
|
prerelease: false
|
65
73
|
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
66
75
|
requirements:
|
67
|
-
- - '>='
|
76
|
+
- - ! '>='
|
68
77
|
- !ruby/object:Gem::Version
|
69
78
|
version: '0'
|
70
79
|
- !ruby/object:Gem::Dependency
|
71
80
|
name: growl
|
72
81
|
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
73
83
|
requirements:
|
74
|
-
- - '>='
|
84
|
+
- - ! '>='
|
75
85
|
- !ruby/object:Gem::Version
|
76
86
|
version: '0'
|
77
87
|
type: :development
|
78
88
|
prerelease: false
|
79
89
|
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
80
91
|
requirements:
|
81
|
-
- - '>='
|
92
|
+
- - ! '>='
|
82
93
|
- !ruby/object:Gem::Version
|
83
94
|
version: '0'
|
84
95
|
description: Hashie is a small collection of tools that make hashes more powerful.
|
@@ -134,25 +145,45 @@ files:
|
|
134
145
|
homepage: https://github.com/intridea/hashie
|
135
146
|
licenses:
|
136
147
|
- MIT
|
137
|
-
metadata: {}
|
138
148
|
post_install_message:
|
139
149
|
rdoc_options: []
|
140
150
|
require_paths:
|
141
151
|
- lib
|
142
152
|
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
143
154
|
requirements:
|
144
|
-
- - '>='
|
155
|
+
- - ! '>='
|
145
156
|
- !ruby/object:Gem::Version
|
146
157
|
version: '0'
|
158
|
+
segments:
|
159
|
+
- 0
|
160
|
+
hash: -486186307453190267
|
147
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
none: false
|
148
163
|
requirements:
|
149
|
-
- - '>='
|
164
|
+
- - ! '>='
|
150
165
|
- !ruby/object:Gem::Version
|
151
166
|
version: '0'
|
167
|
+
segments:
|
168
|
+
- 0
|
169
|
+
hash: -486186307453190267
|
152
170
|
requirements: []
|
153
171
|
rubyforge_project:
|
154
|
-
rubygems_version:
|
172
|
+
rubygems_version: 1.8.23
|
155
173
|
signing_key:
|
156
|
-
specification_version:
|
174
|
+
specification_version: 3
|
157
175
|
summary: Your friendly neighborhood hash toolkit.
|
158
|
-
test_files:
|
176
|
+
test_files:
|
177
|
+
- spec/hashie/clash_spec.rb
|
178
|
+
- spec/hashie/dash_spec.rb
|
179
|
+
- spec/hashie/extensions/coercion_spec.rb
|
180
|
+
- spec/hashie/extensions/deep_merge_spec.rb
|
181
|
+
- spec/hashie/extensions/indifferent_access_spec.rb
|
182
|
+
- spec/hashie/extensions/key_conversion_spec.rb
|
183
|
+
- spec/hashie/extensions/merge_initializer_spec.rb
|
184
|
+
- spec/hashie/extensions/method_access_spec.rb
|
185
|
+
- spec/hashie/hash_spec.rb
|
186
|
+
- spec/hashie/mash_spec.rb
|
187
|
+
- spec/hashie/trash_spec.rb
|
188
|
+
- spec/spec.opts
|
189
|
+
- spec/spec_helper.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 1308b1b5e87e1b28328361a0b2f2d084839e7864
|
4
|
-
data.tar.gz: 2db28fef4c8dc89369658de62b59e046e2076ddb
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 7fa20289e9e8a0c57737ed4b79f3c7ea95dd907b8ce5912e75448b1dfe99b4df8b0197bc3719d0ba9db4f995d9d6492cd1a2d0bd0cd1dba748c14d209d4823db
|
7
|
-
data.tar.gz: 838e995acf432ebf669768a68d62797c601823ea521a93b448a584ecf9257323376c1632b5c45e324e2ced4a06265e27417fae15d189ecb8f0e8692a7ba9ad3b
|