dot_hash 1.1.5 → 1.1.6
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 +30 -12
- data/lib/dot_hash/version.rb +1 -1
- data/test/dot_hash/properties_test.rb +23 -1
- 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: 3a348ba75c80c353302b1857142a1c41c4e7cee8
|
4
|
+
data.tar.gz: 9e88b56233a96201a90f0bb6052659fc9188949d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52ac95b9d2bd388bcb773c032fa2553f0f73b8a127585983d9d22758a5b12ca88b8a9e02af5e212de3759642a45570fee6b0197908b61f8a4e98e9b3173b0d3e
|
7
|
+
data.tar.gz: 7a83b68221204451034086766eb55910a49f9c3b4ce3a83bcb3e605e9b93753261e77f9e264e606732040a2ec122ba02274d0b6a85c281d443c7a9a4ab2f483c
|
data/lib/dot_hash/properties.rb
CHANGED
@@ -20,7 +20,8 @@ module DotHash
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def ==(other)
|
23
|
-
super(other)
|
23
|
+
super(other) ||
|
24
|
+
(other.respond_to?(:to_hash) && other.to_hash == hash)
|
24
25
|
end
|
25
26
|
|
26
27
|
def to_s
|
@@ -39,8 +40,14 @@ module DotHash
|
|
39
40
|
key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
|
40
41
|
value = hash.fetch(key, *args, &block)
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
if value.nil? or hash.respond_to?(key)
|
44
|
+
hashify_item value
|
45
|
+
elsif cached.has_key?(key)
|
46
|
+
value
|
47
|
+
else
|
48
|
+
cached[key] = true
|
49
|
+
hash[key] = hashify_item(value)
|
50
|
+
end
|
44
51
|
end
|
45
52
|
|
46
53
|
def has_key?(key)
|
@@ -51,22 +58,33 @@ module DotHash
|
|
51
58
|
|
52
59
|
private
|
53
60
|
|
61
|
+
def cached
|
62
|
+
@cached ||= self.class.new({})
|
63
|
+
end
|
64
|
+
|
54
65
|
def execute(key, *args, &block)
|
55
66
|
fetch(key) do
|
56
|
-
|
57
|
-
|
67
|
+
if block
|
68
|
+
hash.public_send(key, *args) do |*values|
|
69
|
+
block.call(*hashify_list(values))
|
70
|
+
end
|
71
|
+
else
|
72
|
+
hash.public_send(key, *args)
|
58
73
|
end
|
59
74
|
end
|
60
75
|
end
|
61
76
|
|
62
|
-
def
|
77
|
+
def hashify_list(args)
|
63
78
|
args.each_with_index do |a, i|
|
64
|
-
args[i] =
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
79
|
+
args[i] = hashify_item(a)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def hashify_item(item)
|
84
|
+
case item
|
85
|
+
when Hash then self.class.new(item)
|
86
|
+
when Array then hashify_list(item)
|
87
|
+
else item
|
70
88
|
end
|
71
89
|
end
|
72
90
|
end
|
data/lib/dot_hash/version.rb
CHANGED
@@ -54,13 +54,35 @@ module DotHash
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
describe 'method delegation' do
|
57
|
+
describe 'with method delegation' do
|
58
58
|
before do
|
59
59
|
@properties = Properties.new({
|
60
60
|
ninja: {name: 'Naruto'}
|
61
61
|
})
|
62
62
|
end
|
63
63
|
|
64
|
+
it 'does not cache method calls' do
|
65
|
+
assert_equal(
|
66
|
+
properties.map { |k, v| v },
|
67
|
+
[{name: 'Naruto'}]
|
68
|
+
)
|
69
|
+
|
70
|
+
assert_equal(
|
71
|
+
properties.map { |k, v| {k => v.name} },
|
72
|
+
[{ninja: 'Naruto'}]
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
it '#merge! without crashing' do
|
77
|
+
properties.merge!({hero: 'One Punch'})
|
78
|
+
properties.merge!({hero: 'One Punch 2'})
|
79
|
+
|
80
|
+
assert_equal(properties, {
|
81
|
+
hero: 'One Punch 2',
|
82
|
+
ninja: {name: 'Naruto'}
|
83
|
+
})
|
84
|
+
end
|
85
|
+
|
64
86
|
it '#map sets inner-hashes to Properties' do
|
65
87
|
result = properties.map { |k, v| "#{k}: #{v.name}" }
|
66
88
|
|