navigable_hash 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/navigable_hash.rb +12 -4
- data/navigable_hash.gemspec +1 -1
- data/spec/lib/navigable_hash_spec.rb +47 -0
- metadata +2 -2
data/lib/navigable_hash.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
class NavigableHash < Hash
|
2
2
|
|
3
|
-
def initialize(constructor = {})
|
4
|
-
if
|
3
|
+
def initialize(constructor = {}, &block)
|
4
|
+
if block_given?
|
5
|
+
yield self
|
6
|
+
elsif constructor.is_a?(Hash)
|
5
7
|
super()
|
6
8
|
update(constructor)
|
7
9
|
else
|
@@ -168,14 +170,20 @@ class NavigableHash < Hash
|
|
168
170
|
value
|
169
171
|
end
|
170
172
|
|
173
|
+
def navigate_hash_from_block(key, &block)
|
174
|
+
self[key] = self.class.new &block
|
175
|
+
end
|
176
|
+
|
171
177
|
private
|
172
178
|
|
173
179
|
def method_missing(m, *args, &block)
|
174
180
|
m = m.to_s
|
175
181
|
if args.size == 1 && m.gsub!(/(.+)=$/, '\1')
|
176
|
-
self
|
182
|
+
self[m] = args.first
|
183
|
+
elsif args.size == 0 && block_given?
|
184
|
+
self.navigate_hash_from_block m, &block
|
177
185
|
elsif args.size == 0
|
178
|
-
self
|
186
|
+
self[m]
|
179
187
|
else
|
180
188
|
raise ArgumentError, "wrong number of arguments(#{args.size} for 0)"
|
181
189
|
end
|
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.0
|
7
|
+
spec.version = "1.1.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.}
|
@@ -22,6 +22,29 @@ describe NavigableHash do
|
|
22
22
|
expect { NavigableHash.new({:a => 1}) }.to_not raise_error
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
context "given a block" do
|
27
|
+
it "should not raise an error" do
|
28
|
+
expect do
|
29
|
+
NavigableHash.new do |v|
|
30
|
+
v.foo = :bar
|
31
|
+
end
|
32
|
+
end.to_not raise_error
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should create a new navigable hash" do
|
36
|
+
navigable = NavigableHash.new do |v|
|
37
|
+
end
|
38
|
+
navigable.should be_a NavigableHash
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should assign values in the block to the hash" do
|
42
|
+
navigable = NavigableHash.new do |v|
|
43
|
+
v.foo = :bar
|
44
|
+
end
|
45
|
+
navigable.foo.should == :bar
|
46
|
+
end
|
47
|
+
end
|
25
48
|
end
|
26
49
|
|
27
50
|
describe ".new(TEST_HASH)" do
|
@@ -71,6 +94,30 @@ describe NavigableHash do
|
|
71
94
|
it "should raise an error with arguments" do
|
72
95
|
expect { navigable.__any_method__ :foo }.to raise_error
|
73
96
|
end
|
97
|
+
|
98
|
+
context "given a block" do
|
99
|
+
it "should not raise an error" do
|
100
|
+
expect do
|
101
|
+
navigable.__any_method__ do |v|
|
102
|
+
v.foo = :bar
|
103
|
+
end
|
104
|
+
end.to_not raise_error
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should create a new navigable hash" do
|
108
|
+
navigable.__any_method__ do |v|
|
109
|
+
end
|
110
|
+
navigable.__any_method__.should be_a NavigableHash
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should assign values in the block to the hash" do
|
114
|
+
navigable.__any_method__ do |v|
|
115
|
+
v.foo = :bar
|
116
|
+
end
|
117
|
+
navigable.__any_method__.foo.should == :bar
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
74
121
|
end
|
75
122
|
|
76
123
|
describe "#__any_method__=" 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.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-04-
|
13
|
+
date: 2013-04-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|