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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20f5d3ca34ab88a7b6456373f515b26493d47d77
4
- data.tar.gz: ffe8dbdbcff47b7113471a73dfd9148d2db6aaa9
3
+ metadata.gz: b1524c82c2864754bdf75fd4845f8d2a1b4b829f
4
+ data.tar.gz: 343ee8b30f139c23502e7476019058185aac4629
5
5
  SHA512:
6
- metadata.gz: 6541f82e40508c13111f9456f811efc3a7d6fea2d5ba53a90e0993f852f82f9d99bcb90aebd93d2eddbb4e77f396d5e5ca45389ca22db9ad0326988df9ca8426
7
- data.tar.gz: f9be5b96e77e4a465609a27016888fa14c7a520c48d858895d96833bc0fa7f56bf5a5f8f0e113dac6533f194fe502bf077f09d1f9f6ecf478fec7d570a0f0ed2
6
+ metadata.gz: d5ba40da58b48bb4ce2b0b520720f914945686e1a8c91a9557f88d619fdd7a027509d36f6489e8c0790407ced3c1ad22fa0bedf471379c2aa061ec0411cb3ae3
7
+ data.tar.gz: aba92b626a9386e2451205b0ec99311b434f8ab33041ae6127aab6bd1d76e569b91f4fffe91ca98df043b17b19e5951eec1d67c38fbd24fb714a002a459df63a
@@ -1,3 +1,3 @@
1
1
  class CastedHash
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/casted_hash.rb CHANGED
@@ -82,6 +82,7 @@ class CastedHash
82
82
 
83
83
  self
84
84
  end
85
+ alias_method :merge!, :update
85
86
 
86
87
  def merge(other)
87
88
  self.dup.update other
@@ -86,64 +86,126 @@ class TestCastedHash < Minitest::Test
86
86
  assert_equal({"a" => 11}, hash.casted_hash)
87
87
  end
88
88
 
89
- it "should merge values" do
90
- hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
91
- assert_equal 11, hash[:a]
92
- assert_equal 12, hash[:b]
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
- hash = hash.merge({:a => 3})
95
+ hash = hash.merge({:a => 3})
95
96
 
96
- assert_equal 13, hash[:a]
97
- assert_equal 12, hash[:b]
97
+ assert_equal 13, hash[:a]
98
+ assert_equal 12, hash[:b]
98
99
 
99
- assert_equal hash, hash.merge({})
100
- end
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
- it "should take over scope when merging two casted hashes" do
103
- hash1 = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
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
- assert_equal 11, hash1[:a]
107
- assert_equal 12, hash1[:b]
108
- assert hash1.casted?(:a)
109
- assert hash1.casted?(:b)
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
- cast_proc1_object_id = hash1.cast_proc.object_id
112
- assert_equal [hash1.object_id], hash1.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
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
- hash3 = hash1.merge hash2
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
- assert_equal [hash3.object_id], hash3.send(:raw).values.map{|v|v.casted_hash.object_id}.uniq
117
- assert_equal hash3.cast_proc.object_id, cast_proc1_object_id
136
+ assert !hash.casted?(:a)
137
+ assert !hash.casted?(:b)
138
+ assert !hash.casted?(:c)
118
139
 
119
- assert_equal ["a", "b", "c", "d"], hash3.keys
120
- assert hash3.casted?(:a)
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
- assert_equal 11, hash3[:a]
126
- assert_equal 12, hash3[:b]
127
- assert_equal 13, hash3[:c]
128
- assert_equal 14, hash3[:d]
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
- it "should not cast all values when merging hashes" do
132
- hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
133
- hash = hash.merge :c => 3
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
- assert !hash.casted?(:a)
136
- assert !hash.casted?(:b)
137
- assert !hash.casted?(:c)
157
+ hash.merge!({:a => 3})
138
158
 
139
- other_hash = CastedHash.new({:c => 1, :d => 2}, lambda {|x| x + 10 })
140
- hash = hash.merge other_hash
159
+ assert_equal 13, hash[:a]
160
+ assert_equal 12, hash[:b]
161
+ end
141
162
 
142
- assert !hash.casted?(:a)
143
- assert !hash.casted?(:b)
144
- assert !hash.casted?(:c)
145
- assert !other_hash.casted?(:c)
146
- assert !other_hash.casted?(:d)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casted_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag