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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8d0e1bf59f7f60cbc0ade6d0845c3e5dac3cad7
4
- data.tar.gz: 44f2485a4e0c2a5a686ac72f025e638240be5e34
3
+ metadata.gz: b6f21e00d6939ee05431e9cd4a91d3a05a3a0cb8
4
+ data.tar.gz: 2795940878a1721f5e08f76c0e89d33af294a9ca
5
5
  SHA512:
6
- metadata.gz: b706bb8d8e5d4efbe59a633c075776938198bd0d00a50951500fda289d30f003b376e94ab5b12d364df7c199ded8fb0ce78180b37b92039bfe670aca8c45526f
7
- data.tar.gz: 3d65221300c869f5670b4757bf1c9471056f8fec2607f0889978394eb5aef9e582c2a465085c65f1c680dd459e1fe6c803614fda43a6ea7c4fa3defce6dc31a8
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.1"
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{|key| uncast! key}
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
- @casted_keys.concat keys.map(&:to_s)
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
@@ -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
- hash3 = hash1.merge hash2
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 !hash3.casted?(:d)
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 114, hash3[:d] # casted twice
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
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.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag