casted_hash 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/casted_hash/version.rb +1 -1
- data/lib/casted_hash.rb +1 -0
- data/test/test_casted_hash.rb +105 -43
- 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: b1524c82c2864754bdf75fd4845f8d2a1b4b829f
|
4
|
+
data.tar.gz: 343ee8b30f139c23502e7476019058185aac4629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5ba40da58b48bb4ce2b0b520720f914945686e1a8c91a9557f88d619fdd7a027509d36f6489e8c0790407ced3c1ad22fa0bedf471379c2aa061ec0411cb3ae3
|
7
|
+
data.tar.gz: aba92b626a9386e2451205b0ec99311b434f8ab33041ae6127aab6bd1d76e569b91f4fffe91ca98df043b17b19e5951eec1d67c38fbd24fb714a002a459df63a
|
data/lib/casted_hash/version.rb
CHANGED
data/lib/casted_hash.rb
CHANGED
data/test/test_casted_hash.rb
CHANGED
@@ -86,64 +86,126 @@ class TestCastedHash < Minitest::Test
|
|
86
86
|
assert_equal({"a" => 11}, hash.casted_hash)
|
87
87
|
end
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
describe "merge" do
|
90
|
+
it "should merge values" do
|
91
|
+
hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
92
|
+
assert_equal 11, hash[:a]
|
93
|
+
assert_equal 12, hash[:b]
|
93
94
|
|
94
|
-
|
95
|
+
hash = hash.merge({:a => 3})
|
95
96
|
|
96
|
-
|
97
|
-
|
97
|
+
assert_equal 13, hash[:a]
|
98
|
+
assert_equal 12, hash[:b]
|
98
99
|
|
99
|
-
|
100
|
-
|
100
|
+
assert_equal hash, hash.merge({})
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should take over scope when merging two casted hashes" do
|
104
|
+
hash1 = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
105
|
+
hash2 = CastedHash.new({:c => 3, :d => 4}, lambda {|x| x + 100 })
|
106
|
+
|
107
|
+
assert_equal 11, hash1[:a]
|
108
|
+
assert_equal 12, hash1[:b]
|
109
|
+
assert hash1.casted?(:a)
|
110
|
+
assert hash1.casted?(:b)
|
111
|
+
|
112
|
+
cast_proc1_object_id = hash1.cast_proc.object_id
|
113
|
+
assert_equal [hash1.object_id], hash1.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
|
114
|
+
|
115
|
+
hash3 = hash1.merge hash2
|
101
116
|
|
102
|
-
|
103
|
-
|
104
|
-
hash2 = CastedHash.new({:c => 3, :d => 4}, lambda {|x| x + 100 })
|
117
|
+
assert_equal [hash3.object_id], hash3.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
|
118
|
+
assert_equal hash3.cast_proc.object_id, cast_proc1_object_id
|
105
119
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
120
|
+
assert_equal ["a", "b", "c", "d"], hash3.keys
|
121
|
+
assert hash3.casted?(:a)
|
122
|
+
assert hash3.casted?(:b)
|
123
|
+
assert !hash3.casted?(:c)
|
124
|
+
assert !hash3.casted?(:d)
|
110
125
|
|
111
|
-
|
112
|
-
|
126
|
+
assert_equal 11, hash3[:a]
|
127
|
+
assert_equal 12, hash3[:b]
|
128
|
+
assert_equal 13, hash3[:c]
|
129
|
+
assert_equal 14, hash3[:d]
|
130
|
+
end
|
113
131
|
|
114
|
-
|
132
|
+
it "should not cast all values when merging hashes" do
|
133
|
+
hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
134
|
+
hash = hash.merge :c => 3
|
115
135
|
|
116
|
-
|
117
|
-
|
136
|
+
assert !hash.casted?(:a)
|
137
|
+
assert !hash.casted?(:b)
|
138
|
+
assert !hash.casted?(:c)
|
118
139
|
|
119
|
-
|
120
|
-
|
121
|
-
assert hash3.casted?(:b)
|
122
|
-
assert !hash3.casted?(:c)
|
123
|
-
assert !hash3.casted?(:d)
|
140
|
+
other_hash = CastedHash.new({:c => 1, :d => 2}, lambda {|x| x + 10 })
|
141
|
+
hash = hash.merge other_hash
|
124
142
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
143
|
+
assert !hash.casted?(:a)
|
144
|
+
assert !hash.casted?(:b)
|
145
|
+
assert !hash.casted?(:c)
|
146
|
+
assert !other_hash.casted?(:c)
|
147
|
+
assert !other_hash.casted?(:d)
|
148
|
+
end
|
129
149
|
end
|
130
150
|
|
131
|
-
|
132
|
-
|
133
|
-
|
151
|
+
describe "merge!" do
|
152
|
+
it "should merge! values" do
|
153
|
+
hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
154
|
+
assert_equal 11, hash[:a]
|
155
|
+
assert_equal 12, hash[:b]
|
134
156
|
|
135
|
-
|
136
|
-
assert !hash.casted?(:b)
|
137
|
-
assert !hash.casted?(:c)
|
157
|
+
hash.merge!({:a => 3})
|
138
158
|
|
139
|
-
|
140
|
-
|
159
|
+
assert_equal 13, hash[:a]
|
160
|
+
assert_equal 12, hash[:b]
|
161
|
+
end
|
141
162
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
163
|
+
it "should take over scope when merging two casted hashes" do
|
164
|
+
hash1 = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
165
|
+
hash2 = CastedHash.new({:c => 3, :d => 4}, lambda {|x| x + 100 })
|
166
|
+
|
167
|
+
assert_equal 11, hash1[:a]
|
168
|
+
assert_equal 12, hash1[:b]
|
169
|
+
assert hash1.casted?(:a)
|
170
|
+
assert hash1.casted?(:b)
|
171
|
+
|
172
|
+
cast_proc1_object_id = hash1.cast_proc.object_id
|
173
|
+
assert_equal [hash1.object_id], hash1.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
|
174
|
+
|
175
|
+
hash1.merge! hash2
|
176
|
+
|
177
|
+
assert_equal [hash1.object_id], hash1.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
|
178
|
+
assert_equal hash1.cast_proc.object_id, cast_proc1_object_id
|
179
|
+
|
180
|
+
assert_equal ["a", "b", "c", "d"], hash1.keys
|
181
|
+
assert hash1.casted?(:a)
|
182
|
+
assert hash1.casted?(:b)
|
183
|
+
assert !hash1.casted?(:c)
|
184
|
+
assert !hash1.casted?(:d)
|
185
|
+
|
186
|
+
assert_equal 11, hash1[:a]
|
187
|
+
assert_equal 12, hash1[:b]
|
188
|
+
assert_equal 13, hash1[:c]
|
189
|
+
assert_equal 14, hash1[:d]
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should not cast all values when merging hashes" do
|
193
|
+
hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
194
|
+
hash.merge! :c => 3
|
195
|
+
|
196
|
+
assert !hash.casted?(:a)
|
197
|
+
assert !hash.casted?(:b)
|
198
|
+
assert !hash.casted?(:c)
|
199
|
+
|
200
|
+
other_hash = CastedHash.new({:c => 1, :d => 2}, lambda {|x| x + 10 })
|
201
|
+
hash.merge! other_hash
|
202
|
+
|
203
|
+
assert !hash.casted?(:a)
|
204
|
+
assert !hash.casted?(:b)
|
205
|
+
assert !hash.casted?(:c)
|
206
|
+
assert !other_hash.casted?(:c)
|
207
|
+
assert !other_hash.casted?(:d)
|
208
|
+
end
|
147
209
|
end
|
148
210
|
|
149
211
|
it "should define a hash method" do
|