casted_hash 0.5.2 → 0.5.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: b6f21e00d6939ee05431e9cd4a91d3a05a3a0cb8
4
- data.tar.gz: 2795940878a1721f5e08f76c0e89d33af294a9ca
3
+ metadata.gz: bf3b3d257f7006192515041d52f399aca8090943
4
+ data.tar.gz: ae3a58bcf07664a3403e66eb0db81ae761c7b871
5
5
  SHA512:
6
- metadata.gz: 99487582e081aeb4d5f81edbe97a926f58909dfae05d4925e4a825292aa6d9b1c314590f2465e5fc1936f5f927471da4ba131f5e602d0d84ae1cf9eba72c4425
7
- data.tar.gz: 63ab1e583432fd53eae7c04ef525971765b4786d128e7203bf4b3e46c86acefb7233bab665877b638e0f1a2259de4cec044095dc92eccc969db830dbad8baf26
6
+ metadata.gz: 5b51d3de96a7ed760fb02673dc6aba39677081f856075dd719cd1c8245e69c826f53e63fb7e6dc02cd1373692703054943b81dad2fbf30b4bc4ea0d61c85b3e4
7
+ data.tar.gz: 24aa339f2d51e5679e81e6eaadfaa1fa98f601b3707de39e4a02a829a5df7c70ef19fcf586a2b23ed9afc8c436ef08cc618aac05b63db062e0ccbb726d11b1d5
data/lib/casted_hash.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class CastedHash < Hash
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
 
4
4
  def initialize(constructor = {}, cast_proc = nil)
5
5
  raise ArgumentError, "`cast_proc` required" unless cast_proc
@@ -53,15 +53,16 @@ class CastedHash < Hash
53
53
  def update(other_hash)
54
54
  return self if other_hash.empty?
55
55
 
56
- if other_hash.is_a? CastedHash
57
- other_hash.keys.each do |key|
58
- if other_hash.casted?(key)
59
- casted!(key)
60
- elsif casted?(key)
61
- uncast!(key)
56
+ if other_hash.is_a?(CastedHash)
57
+ super(other_hash).tap do
58
+ other_hash.keys.each do |key|
59
+ if other_hash.casted?(key)
60
+ casted!(key)
61
+ elsif casted?(key)
62
+ uncast!(key)
63
+ end
62
64
  end
63
65
  end
64
- super(other_hash)
65
66
  else
66
67
  other_hash.each_pair { |key, value| self[key] = value }
67
68
  self
@@ -102,22 +102,23 @@ describe CastedHash do
102
102
 
103
103
  assert_equal 11, hash1[:a]
104
104
  assert_equal 12, hash1[:b]
105
- assert_equal 104, hash2[:d]
106
105
  assert hash1.casted?(:a)
107
106
  assert hash1.casted?(:b)
108
- assert hash2.casted?(:d)
109
107
 
108
+ assert_equal 104, hash2[:d]
109
+ assert hash2.casted?(:d)
110
110
  assert !hash2.casted?(:a)
111
111
 
112
112
  hash3 = hash1.merge hash2
113
113
  assert_equal 10, hash3[:z]
114
+ assert hash3.casted?(:z)
114
115
  assert !hash1.casted?(:z)
115
116
 
116
117
  assert_equal ["a", "b", "c", "d", "z"], hash3.keys.sort
117
- assert !hash3.casted?(:a)
118
- assert hash3.casted?(:b)
119
- assert !hash3.casted?(:c)
120
- assert hash3.casted?(:d) # already casted
118
+ assert !hash3.casted?(:a) # overwritten with uncasted value
119
+ assert hash3.casted?(:b) # not overwritten, still casted
120
+ assert !hash3.casted?(:c) # overwritten with uncasted value
121
+ assert hash3.casted?(:d) # overwritten with casted value
121
122
 
122
123
  assert_equal 12, hash3[:a]
123
124
  assert_equal 12, hash3[:b]
@@ -126,18 +127,13 @@ describe CastedHash do
126
127
  end
127
128
 
128
129
  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
-
130
+ hash1 = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 1})
132
131
  assert_equal 2, hash1[:a]
133
- assert_equal 3, hash1[:b]
134
- assert_equal 4, hash1[:c]
135
132
 
136
- hash3 = hash1.merge(hash2)
133
+ hash1.merge!(hash1)
137
134
 
138
- assert !hash3.casted?(:a) # value is different
139
- assert !hash3.casted?(:b)
140
- assert hash3.casted?(:c)
135
+ assert hash1.casted?(:a)
136
+ assert !hash1.casted?(:b)
141
137
  end
142
138
 
143
139
  it "does not cast all values when merging hashes" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casted_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-25 00:00:00.000000000 Z
11
+ date: 2014-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler