dot_hash 1.1.3 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dot_hash/properties.rb +19 -20
- data/lib/dot_hash/version.rb +1 -1
- data/test/dot_hash/properties_test.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 204c2956dd13177e9bda794e4b1f335fa2e86f15
|
4
|
+
data.tar.gz: 8d194f674b666d999e7435c2c4ba6dc8d32dbe85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11a3a166c97133802ce7f236bd5755460fec4fe17d20c2c4fe297baf63498aa270875a0a67abdca3d60fc2b589918f788bd0e2c404d9f4824d769d20d5b22b9d
|
7
|
+
data.tar.gz: 74c27217e04d9037b129ee265b71a3022d94db8d693ebae4943a0714c15844cc8bc2cbbc359178ef548c47c49399e1be2df7ff9187155666a7611080cdaf5a38
|
data/lib/dot_hash/properties.rb
CHANGED
@@ -20,7 +20,7 @@ module DotHash
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def ==(other)
|
23
|
-
super
|
23
|
+
super(other) or other.to_hash == hash
|
24
24
|
end
|
25
25
|
|
26
26
|
def to_s
|
@@ -32,10 +32,16 @@ module DotHash
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def [](key)
|
35
|
-
|
35
|
+
fetch(key, nil)
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
def fetch(key, *args, &block)
|
39
|
+
key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
|
40
|
+
value = hash.fetch(key, *args, &block)
|
41
|
+
|
42
|
+
return value unless value.is_a?(Hash)
|
43
|
+
hash[key] = self.class.new value
|
44
|
+
end
|
39
45
|
|
40
46
|
def has_key?(key)
|
41
47
|
hash.has_key?(key.to_s) or
|
@@ -43,8 +49,10 @@ module DotHash
|
|
43
49
|
hash.respond_to?(key)
|
44
50
|
end
|
45
51
|
|
52
|
+
private
|
53
|
+
|
46
54
|
def execute(key, *args, &block)
|
47
|
-
|
55
|
+
fetch(key) do
|
48
56
|
hash.public_send(key, *args) do |*values|
|
49
57
|
block.call(*hashify_args(values))
|
50
58
|
end
|
@@ -52,23 +60,14 @@ module DotHash
|
|
52
60
|
end
|
53
61
|
|
54
62
|
def hashify_args(args)
|
55
|
-
args.
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
hashify_args(a)
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
+
args.each_with_index do |a, i|
|
64
|
+
args[i] =
|
65
|
+
case a
|
66
|
+
when Hash then self.class.new(a)
|
67
|
+
when Array then hashify_args(a)
|
68
|
+
else a
|
69
|
+
end
|
63
70
|
end
|
64
71
|
end
|
65
|
-
|
66
|
-
def get_value(key, &fallback)
|
67
|
-
key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
|
68
|
-
value = hash.fetch(key) { fallback ? fallback.call : nil }
|
69
|
-
|
70
|
-
return value unless value.is_a?(Hash)
|
71
|
-
hash[key] = self.class.new value
|
72
|
-
end
|
73
72
|
end
|
74
73
|
end
|
data/lib/dot_hash/version.rb
CHANGED
@@ -74,6 +74,14 @@ module DotHash
|
|
74
74
|
|
75
75
|
assert_equal result, Properties.new({ninja: 'Naruto'})
|
76
76
|
end
|
77
|
+
|
78
|
+
it '#each_with_object goes nested on on array' do
|
79
|
+
result = properties.each_with_object([]) do |(k, v), acc|
|
80
|
+
acc.push v.name
|
81
|
+
end
|
82
|
+
|
83
|
+
assert_equal result, ['Naruto']
|
84
|
+
end
|
77
85
|
end
|
78
86
|
end
|
79
87
|
|