casted_hash 0.5.1 → 0.5.2
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.rb +11 -3
- data/test/test_casted_hash.rb +20 -3
- 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: b6f21e00d6939ee05431e9cd4a91d3a05a3a0cb8
|
4
|
+
data.tar.gz: 2795940878a1721f5e08f76c0e89d33af294a9ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99487582e081aeb4d5f81edbe97a926f58909dfae05d4925e4a825292aa6d9b1c314590f2465e5fc1936f5f927471da4ba131f5e602d0d84ae1cf9eba72c4425
|
7
|
+
data.tar.gz: 63ab1e583432fd53eae7c04ef525971765b4786d128e7203bf4b3e46c86acefb7233bab665877b638e0f1a2259de4cec044095dc92eccc969db830dbad8baf26
|
data/lib/casted_hash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CastedHash < Hash
|
2
|
-
VERSION = "0.5.
|
2
|
+
VERSION = "0.5.2"
|
3
3
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
@@ -54,7 +54,13 @@ class CastedHash < Hash
|
|
54
54
|
return self if other_hash.empty?
|
55
55
|
|
56
56
|
if other_hash.is_a? CastedHash
|
57
|
-
other_hash.keys.each
|
57
|
+
other_hash.keys.each do |key|
|
58
|
+
if other_hash.casted?(key)
|
59
|
+
casted!(key)
|
60
|
+
elsif casted?(key)
|
61
|
+
uncast!(key)
|
62
|
+
end
|
63
|
+
end
|
58
64
|
super(other_hash)
|
59
65
|
else
|
60
66
|
other_hash.each_pair { |key, value| self[key] = value }
|
@@ -121,7 +127,9 @@ class CastedHash < Hash
|
|
121
127
|
end
|
122
128
|
|
123
129
|
def casted!(*keys)
|
124
|
-
|
130
|
+
keys.map(&:to_s).each do |key|
|
131
|
+
@casted_keys << key if key?(key)
|
132
|
+
end
|
125
133
|
end
|
126
134
|
|
127
135
|
protected
|
data/test/test_casted_hash.rb
CHANGED
@@ -105,9 +105,11 @@ describe CastedHash do
|
|
105
105
|
assert_equal 104, hash2[:d]
|
106
106
|
assert hash1.casted?(:a)
|
107
107
|
assert hash1.casted?(:b)
|
108
|
+
assert hash2.casted?(:d)
|
108
109
|
|
109
|
-
|
110
|
+
assert !hash2.casted?(:a)
|
110
111
|
|
112
|
+
hash3 = hash1.merge hash2
|
111
113
|
assert_equal 10, hash3[:z]
|
112
114
|
assert !hash1.casted?(:z)
|
113
115
|
|
@@ -115,12 +117,27 @@ describe CastedHash do
|
|
115
117
|
assert !hash3.casted?(:a)
|
116
118
|
assert hash3.casted?(:b)
|
117
119
|
assert !hash3.casted?(:c)
|
118
|
-
assert
|
120
|
+
assert hash3.casted?(:d) # already casted
|
119
121
|
|
120
122
|
assert_equal 12, hash3[:a]
|
121
123
|
assert_equal 12, hash3[:b]
|
122
124
|
assert_equal 13, hash3[:c]
|
123
|
-
assert_equal
|
125
|
+
assert_equal 104, hash3[:d] # already casted
|
126
|
+
end
|
127
|
+
|
128
|
+
it "doesn't uncast when merging same value" do
|
129
|
+
hash1 = CastedHash.new({:a => 1, :b => 2, :c => 3}, lambda {|x| x + 1})
|
130
|
+
hash2 = CastedHash.new({:a => 0, :b => 2, :c => 4}, lambda {|x| x + 2})
|
131
|
+
|
132
|
+
assert_equal 2, hash1[:a]
|
133
|
+
assert_equal 3, hash1[:b]
|
134
|
+
assert_equal 4, hash1[:c]
|
135
|
+
|
136
|
+
hash3 = hash1.merge(hash2)
|
137
|
+
|
138
|
+
assert !hash3.casted?(:a) # value is different
|
139
|
+
assert !hash3.casted?(:b)
|
140
|
+
assert hash3.casted?(:c)
|
124
141
|
end
|
125
142
|
|
126
143
|
it "does not cast all values when merging hashes" do
|