navigable_hash 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.ruby-version +1 -0
- data/.travis.yml +3 -5
- data/README.md +5 -4
- data/lib/navigable_hash.rb +20 -6
- data/navigable_hash.gemspec +1 -1
- data/spec/helpers/helper_methods.rb +3 -3
- data/spec/lib/navigable_hash_spec.rb +37 -34
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NjQ0MGEwNTdmZTE2ZTU5YjMwODAyZmI0M2RmMzE2OThhMmI4ZjIwNWIzMjli
|
10
|
-
ZGM1N2E1MWJlNTJlODhhNjU5NDJmZWMzMGMzOWUyMzA4MTNjNjhiNmY0NDZl
|
11
|
-
YzcyN2EzN2M3ODEwNDk5MGIwNjc1YWFhYzBmZThkZDc1M2JhM2Y=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MmZkNjk4ODU1MzNlNTNkNzg5MjZlMDc5ZmRiMDgxYjI5NDEyMTAzMWNiN2Vm
|
14
|
-
ZWMwMzEwODY3ZjU4Nzk3MjgwMDllNGY0NzZmZWYxOGEzMzVlMGM0MzU5MjUz
|
15
|
-
ODg4ZTZkNjYyNDU1NDI0NzkyNGRiNDM4YzhiNzJjYjg5NTRlNmU=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea470547e2314253ab6f4a4897e0cb1c257108d3
|
4
|
+
data.tar.gz: 290f62d648a95c7008fb8e3691ff8c0d087700a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f8244b8936e50a1e1e87f4600a89cf4215109525b714900df06e593bb6e93da4553e13269123520a16a84e3c6e2483f24431adca07b260fb24d78ad7882f20d6
|
7
|
+
data.tar.gz: 5869f906e535da3cf6449915c50b11e604146601434d9d5e7699f0d4024c305e3eb961bd931e811ac8c399edf5e6b20bc32ead1da18c5976893ce8fd12c84375
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.2
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# NavigableHash
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
[![
|
3
|
+
[![Version](http://allthebadges.io/jwaldrip/navigable_hash/badge_fury.png)](http://allthebadges.io/jwaldrip/navigable_hash/badge_fury)
|
4
|
+
[![Dependencies](http://allthebadges.io/jwaldrip/navigable_hash/gemnasium.png)](http://allthebadges.io/jwaldrip/navigable_hash/gemnasium)
|
5
|
+
[![Build Status](http://allthebadges.io/jwaldrip/navigable_hash/travis.png)](http://allthebadges.io/jwaldrip/navigable_hash/travis)
|
6
|
+
[![Coverage](http://allthebadges.io/jwaldrip/navigable_hash/coveralls.png)](http://allthebadges.io/jwaldrip/navigable_hash/coveralls)
|
7
|
+
[![Code Climate](http://allthebadges.io/jwaldrip/navigable_hash/code_climate.png)](http://allthebadges.io/jwaldrip/navigable_hash/code_climate)
|
7
8
|
|
8
9
|
NavigableHash was built as lightweight and quick way to navigate through a hash or array object using the familiar ruby dot notation. See 'Usage' below for examples. Keys as strings or symbols don't matter, its all included.
|
9
10
|
|
data/lib/navigable_hash.rb
CHANGED
@@ -176,16 +176,30 @@ class NavigableHash < Hash
|
|
176
176
|
|
177
177
|
private
|
178
178
|
|
179
|
+
def set_and_cache_value(key, value)
|
180
|
+
cache_getter! key
|
181
|
+
self[key] = value
|
182
|
+
end
|
183
|
+
|
184
|
+
def get_and_cache_value(key)
|
185
|
+
cache_getter! key
|
186
|
+
self[key]
|
187
|
+
end
|
188
|
+
|
189
|
+
def cache_getter!(key)
|
190
|
+
define_singleton_method(key) { self[key] } unless respond_to? key
|
191
|
+
end
|
192
|
+
|
179
193
|
def method_missing(m, *args, &block)
|
180
194
|
m = m.to_s
|
181
|
-
if
|
182
|
-
|
183
|
-
elsif args.
|
195
|
+
if m.chomp!('=') && args.count == 1
|
196
|
+
set_and_cache_value(m, *args)
|
197
|
+
elsif args.empty? && block_given?
|
184
198
|
self.navigate_hash_from_block m, &block
|
185
|
-
elsif args.
|
186
|
-
|
199
|
+
elsif args.empty?
|
200
|
+
get_and_cache_value(m)
|
187
201
|
else
|
188
|
-
|
202
|
+
fail ArgumentError, "wrong number of arguments (#{args.count} for 0)"
|
189
203
|
end
|
190
204
|
end
|
191
205
|
|
data/navigable_hash.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "navigable_hash"
|
7
|
-
spec.version = "1.
|
7
|
+
spec.version = "1.3.0"
|
8
8
|
spec.authors = ["Jason Waldrip", "Kevin Wanek"]
|
9
9
|
spec.email = ["jason@waldrip.net", "k@dmcy.us"]
|
10
10
|
spec.description = %q{Allows a hash to be navigated with dot notation or indifferent access.}
|
@@ -12,7 +12,7 @@ module NavigableHash::HelperMethods
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should have a valid value" do
|
15
|
-
navigable.send
|
15
|
+
expect(navigable.send key).to eq hash[key]
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -29,7 +29,7 @@ module NavigableHash::HelperMethods
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should have a valid value" do
|
32
|
-
navigable[key.to_sym].
|
32
|
+
expect(navigable[key.to_sym]).to eq hash[key]
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -46,7 +46,7 @@ module NavigableHash::HelperMethods
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should have a valid value" do
|
49
|
-
navigable[key.to_s].
|
49
|
+
expect(navigable[key.to_s]).to eq hash[key]
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -35,14 +35,14 @@ describe NavigableHash do
|
|
35
35
|
it "should create a new navigable hash" do
|
36
36
|
navigable = NavigableHash.new do |v|
|
37
37
|
end
|
38
|
-
navigable.
|
38
|
+
expect(navigable).to be_a NavigableHash
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should assign values in the block to the hash" do
|
42
42
|
navigable = NavigableHash.new do |v|
|
43
43
|
v.foo = :bar
|
44
44
|
end
|
45
|
-
navigable.foo.
|
45
|
+
expect(navigable.foo).to eq :bar
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -77,7 +77,7 @@ describe NavigableHash do
|
|
77
77
|
navigable.foo = {}
|
78
78
|
navigable.foo.bar = {}
|
79
79
|
navigable.foo.bar.baz = value
|
80
|
-
navigable.foo.bar.baz.
|
80
|
+
expect(navigable.foo.bar.baz).to eq value
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -88,11 +88,14 @@ describe NavigableHash do
|
|
88
88
|
|
89
89
|
it "should get a value" do
|
90
90
|
navigable.__any_method__ = "foo"
|
91
|
-
navigable.__any_method__.
|
91
|
+
expect(navigable.__any_method__).to eq "foo"
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should raise an error with arguments" do
|
95
|
-
expect {
|
95
|
+
expect {
|
96
|
+
$pry = true
|
97
|
+
navigable.__any_method__ :foo
|
98
|
+
}.to raise_error
|
96
99
|
end
|
97
100
|
|
98
101
|
context "given a block" do
|
@@ -107,14 +110,14 @@ describe NavigableHash do
|
|
107
110
|
it "should create a new navigable hash" do
|
108
111
|
navigable.__any_method__ do |v|
|
109
112
|
end
|
110
|
-
navigable.__any_method__.
|
113
|
+
expect(navigable.__any_method__).to be_a NavigableHash
|
111
114
|
end
|
112
115
|
|
113
116
|
it "should assign values in the block to the hash" do
|
114
117
|
navigable.__any_method__ do |v|
|
115
118
|
v.foo = :bar
|
116
119
|
end
|
117
|
-
navigable.__any_method__.foo.
|
120
|
+
expect(navigable.__any_method__.foo).to eq :bar
|
118
121
|
end
|
119
122
|
end
|
120
123
|
|
@@ -138,25 +141,25 @@ describe NavigableHash do
|
|
138
141
|
|
139
142
|
context "given a hash" do
|
140
143
|
it "should return a new instance of navigable hash" do
|
141
|
-
navigable[:hash_item].
|
144
|
+
expect(navigable[:hash_item]).to be_an_instance_of NavigableHash
|
142
145
|
end
|
143
146
|
end
|
144
147
|
|
145
148
|
context "given an array" do
|
146
149
|
it "should call #navigate with each value" do
|
147
|
-
navigable[:array].
|
150
|
+
expect(navigable[:array]).to be_a Array
|
148
151
|
end
|
149
152
|
end
|
150
153
|
|
151
154
|
context "given any object" do
|
152
155
|
it "should return a value" do
|
153
|
-
navigable[:object].
|
156
|
+
expect(navigable[:object]).to be_an_instance_of Object
|
154
157
|
end
|
155
158
|
end
|
156
159
|
|
157
160
|
context "given nil" do
|
158
161
|
it "should return a value" do
|
159
|
-
navigable[:nil_value].
|
162
|
+
expect(navigable[:nil_value]).to be_nil
|
160
163
|
end
|
161
164
|
end
|
162
165
|
|
@@ -170,9 +173,9 @@ describe NavigableHash do
|
|
170
173
|
context "given a hash" do
|
171
174
|
it "should return a new instance of navigable hash" do
|
172
175
|
hash = {:a => 1, :b => 2, :c => 3}
|
173
|
-
navigable.
|
176
|
+
expect(navigable).to receive(:navigate_hash).with(hash).and_call_original
|
174
177
|
navigable[:hash] = hash
|
175
|
-
navigable[:hash].
|
178
|
+
expect(navigable[:hash]).to be_an_instance_of NavigableHash
|
176
179
|
end
|
177
180
|
|
178
181
|
context "given a subclass, setting with a navigable hash" do
|
@@ -182,9 +185,9 @@ describe NavigableHash do
|
|
182
185
|
subject(:navigable){ SubNavigableHash.new(hash) }
|
183
186
|
it 'should return an instance of the subclass' do
|
184
187
|
hash = NavigableHash.new :a => 1, :b => 2, :c => 3
|
185
|
-
navigable.
|
188
|
+
expect(navigable).to receive(:navigate_hash).with(hash).and_call_original
|
186
189
|
navigable[:hash] = hash
|
187
|
-
navigable[:hash].
|
190
|
+
expect(navigable[:hash]).to be_an_instance_of SubNavigableHash
|
188
191
|
end
|
189
192
|
end
|
190
193
|
end
|
@@ -192,7 +195,7 @@ describe NavigableHash do
|
|
192
195
|
context "given an array" do
|
193
196
|
it "should call #navigate with each value" do
|
194
197
|
array = [1, 2, 3]
|
195
|
-
navigable.
|
198
|
+
expect(navigable).to receive(:navigate_array).with(array)
|
196
199
|
navigable[:array] = array
|
197
200
|
end
|
198
201
|
end
|
@@ -200,14 +203,14 @@ describe NavigableHash do
|
|
200
203
|
context "given any object" do
|
201
204
|
it "should return a value" do
|
202
205
|
object = Object.new
|
203
|
-
navigable.
|
206
|
+
expect(navigable).to receive(:navigate_value).with(object)
|
204
207
|
navigable[:object] = object
|
205
208
|
end
|
206
209
|
end
|
207
210
|
|
208
211
|
context "given nil" do
|
209
212
|
it "should return a value" do
|
210
|
-
navigable.
|
213
|
+
expect(navigable).to receive(:navigate_value).with(nil)
|
211
214
|
navigable[:nil_value] = nil
|
212
215
|
end
|
213
216
|
end
|
@@ -215,7 +218,7 @@ describe NavigableHash do
|
|
215
218
|
|
216
219
|
describe "#==" do
|
217
220
|
it "should be equal to a hash" do
|
218
|
-
navigable.
|
221
|
+
expect(navigable).to eq hash
|
219
222
|
end
|
220
223
|
end
|
221
224
|
|
@@ -236,11 +239,11 @@ describe NavigableHash do
|
|
236
239
|
|
237
240
|
describe "#dup" do
|
238
241
|
it "should return a copy of navigable hash" do
|
239
|
-
navigable.dup.
|
242
|
+
expect(navigable.dup).to be_an_instance_of NavigableHash
|
240
243
|
end
|
241
244
|
|
242
245
|
it "should be equal to its original" do
|
243
|
-
navigable.dup.
|
246
|
+
expect(navigable.dup).to eq navigable
|
244
247
|
end
|
245
248
|
end
|
246
249
|
|
@@ -248,13 +251,13 @@ describe NavigableHash do
|
|
248
251
|
context "if a key exists" do
|
249
252
|
it "should be true" do
|
250
253
|
navigable[:present_key] = 'value'
|
251
|
-
navigable.key?
|
254
|
+
expect(navigable.key? :present_key).to be_true
|
252
255
|
end
|
253
256
|
end
|
254
257
|
|
255
258
|
context "if a key does not exist" do
|
256
259
|
it "should be false" do
|
257
|
-
navigable.key?
|
260
|
+
expect(navigable.key? :no_key).to be_false
|
258
261
|
end
|
259
262
|
end
|
260
263
|
end
|
@@ -263,7 +266,7 @@ describe NavigableHash do
|
|
263
266
|
context "given a hash" do
|
264
267
|
let(:hash){ { :hash => {} } }
|
265
268
|
it "should return an instance of NavigableHash" do
|
266
|
-
navigable.fetch
|
269
|
+
expect(navigable.fetch :hash).to be_an_instance_of NavigableHash
|
267
270
|
end
|
268
271
|
end
|
269
272
|
end
|
@@ -272,19 +275,19 @@ describe NavigableHash do
|
|
272
275
|
context "given a hash" do
|
273
276
|
it "should have a navigable hash" do
|
274
277
|
merged = navigable.merge({ :some_hash => {}})
|
275
|
-
merged[:some_hash].
|
278
|
+
expect(merged[:some_hash]).to be_an_instance_of NavigableHash
|
276
279
|
end
|
277
280
|
end
|
278
281
|
end
|
279
282
|
|
280
283
|
describe "#to_hash" do
|
281
284
|
it "should be an instance of Hash" do
|
282
|
-
navigable.to_hash.
|
285
|
+
expect(navigable.to_hash).to be_an_instance_of Hash
|
283
286
|
end
|
284
287
|
|
285
288
|
it "inner hash should be an instance of hash" do
|
286
|
-
navigable[:hash_item].
|
287
|
-
navigable.to_hash['hash_item'].
|
289
|
+
expect(navigable[:hash_item]).to be_an_instance_of NavigableHash
|
290
|
+
expect(navigable.to_hash['hash_item']).to be_an_instance_of Hash
|
288
291
|
end
|
289
292
|
end
|
290
293
|
|
@@ -292,7 +295,7 @@ describe NavigableHash do
|
|
292
295
|
context "given a hash" do
|
293
296
|
it "should have a navigable hash" do
|
294
297
|
navigable.update({ :some_hash => {}})
|
295
|
-
navigable[:some_hash].
|
298
|
+
expect(navigable[:some_hash]).to be_an_instance_of NavigableHash
|
296
299
|
end
|
297
300
|
end
|
298
301
|
end
|
@@ -300,20 +303,20 @@ describe NavigableHash do
|
|
300
303
|
describe "#respond_to?" do
|
301
304
|
context "if the key exists" do
|
302
305
|
it "should be " do
|
303
|
-
navigable.respond_to
|
306
|
+
expect(navigable).to_not respond_to :this_is_not_a_method
|
304
307
|
end
|
305
308
|
end
|
306
309
|
|
307
310
|
context "if the key does not exist" do
|
308
311
|
it "should be " do
|
309
312
|
navigable[:this_is_not_a_method] = "Hello"
|
310
|
-
navigable.respond_to
|
313
|
+
expect(navigable).to respond_to :this_is_not_a_method
|
311
314
|
end
|
312
315
|
end
|
313
316
|
|
314
317
|
context "with a real method" do
|
315
318
|
it "should be true" do
|
316
|
-
navigable.respond_to
|
319
|
+
expect(navigable).to respond_to :to_hash
|
317
320
|
end
|
318
321
|
end
|
319
322
|
end
|
@@ -321,13 +324,13 @@ describe NavigableHash do
|
|
321
324
|
describe "#values_at" do
|
322
325
|
let (:hash){ { :foo => "foo_value", :bar => "bar_value", :baz => "baz_value" } }
|
323
326
|
it "should have the correct values" do
|
324
|
-
navigable.values_at
|
327
|
+
expect(navigable.values_at :foo, :bar).to eq [hash[:foo], hash[:bar]]
|
325
328
|
end
|
326
329
|
end
|
327
330
|
|
328
331
|
describe "#method_missing" do
|
329
332
|
it "should return nil" do
|
330
|
-
navigable.this_is_not_a_method.
|
333
|
+
expect(navigable.this_is_not_a_method).to be_nil
|
331
334
|
end
|
332
335
|
|
333
336
|
it "should not raise an error" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: navigable_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Waldrip
|
@@ -9,20 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-05-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - ~>
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '1.3'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - ~>
|
25
|
+
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '1.3'
|
28
28
|
- !ruby/object:Gem::Dependency
|
@@ -145,10 +145,11 @@ executables: []
|
|
145
145
|
extensions: []
|
146
146
|
extra_rdoc_files: []
|
147
147
|
files:
|
148
|
-
- .coveralls.yml
|
149
|
-
- .gitignore
|
150
|
-
- .rspec
|
151
|
-
- .
|
148
|
+
- ".coveralls.yml"
|
149
|
+
- ".gitignore"
|
150
|
+
- ".rspec"
|
151
|
+
- ".ruby-version"
|
152
|
+
- ".travis.yml"
|
152
153
|
- Gemfile
|
153
154
|
- Guardfile
|
154
155
|
- LICENSE.txt
|
@@ -169,17 +170,17 @@ require_paths:
|
|
169
170
|
- lib
|
170
171
|
required_ruby_version: !ruby/object:Gem::Requirement
|
171
172
|
requirements:
|
172
|
-
- -
|
173
|
+
- - ">="
|
173
174
|
- !ruby/object:Gem::Version
|
174
175
|
version: '0'
|
175
176
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
177
|
requirements:
|
177
|
-
- -
|
178
|
+
- - ">="
|
178
179
|
- !ruby/object:Gem::Version
|
179
180
|
version: '0'
|
180
181
|
requirements: []
|
181
182
|
rubyforge_project:
|
182
|
-
rubygems_version: 2.
|
183
|
+
rubygems_version: 2.2.2
|
183
184
|
signing_key:
|
184
185
|
specification_version: 4
|
185
186
|
summary: Allows a hash to be navigated with dot notation or indifferent access.
|