casted_hash 0.8.4 → 0.9.0

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: 5e0fdde9d0be05ae67fc01e13efb414a6391f440
4
- data.tar.gz: 05fd32baa77c4e96a67a6b09b2b0bf5eb3d20e64
3
+ metadata.gz: c5c39bef30e00f8e9271c14a2f670c885b302235
4
+ data.tar.gz: 20954b45304a2d3b80ef2b8ed179a2759dffc192
5
5
  SHA512:
6
- metadata.gz: f5a65a41fdb982bc127128715d6e4a3fa8ce2de31069d230a5fc5f61bf7165da5c9da46691c2d7f4697c49014e98c2944fd4432cca7010ab6f23110179008570
7
- data.tar.gz: 7ca40f81878b5399d013e9f4f0d75d3424a56eb499c745eeacc9888bfc8e13b9a325d33287ec100394061c4ed565b5ab52fa56a0e68c83e754f22158fcad8460
6
+ metadata.gz: 4c9a20a7693c337031c66de360d8975962191d1daee1677a2bb7c40d65b298a581348c87cd6c52990db938536032c829bb0c33424ccd9355e6c823ad77c31a5a
7
+ data.tar.gz: 082d300b4df4ebc59befe661e091f14666540136fd18a3c3b60d04df968f114caeab139baa06628e99011e73d77ccb096194604b5b5079b511ad5669bc95c409
data/lib/casted_hash.rb CHANGED
@@ -1,22 +1,22 @@
1
1
  class CastedHash < Hash
2
- VERSION = "0.8.4"
2
+ VERSION = "0.9.0"
3
3
 
4
4
  def initialize(constructor = {}, cast_proc = nil)
5
5
  raise ArgumentError, "`cast_proc` required" unless cast_proc
6
6
 
7
7
  @cast_proc = cast_proc
8
- @casting_keys = []
8
+ @casting_keys = Set.new
9
9
 
10
10
  if constructor.is_a?(CastedHash)
11
11
  @casted_keys = constructor.instance_variable_get(:@casted_keys).dup
12
12
  super()
13
13
  update(constructor)
14
14
  elsif constructor.is_a?(Hash)
15
- @casted_keys = []
15
+ @casted_keys = Set.new
16
16
  super()
17
17
  update(constructor)
18
18
  else
19
- @casted_keys = []
19
+ @casted_keys = Set.new
20
20
  super(constructor)
21
21
  end
22
22
  end
@@ -70,7 +70,7 @@ class CastedHash < Hash
70
70
  regular_writer converted_key, value
71
71
 
72
72
  if other_hash.is_a?(CastedHash) && other_hash.casted?(key)
73
- casted!(key)
73
+ casted!(key, true)
74
74
  elsif casted?(key)
75
75
  uncast!(converted_key)
76
76
  end
@@ -81,8 +81,9 @@ class CastedHash < Hash
81
81
 
82
82
  alias_method :merge!, :update
83
83
 
84
- def key?(key)
85
- super(convert_key(key))
84
+ def key?(key, converted = false)
85
+ key = convert_key(key) unless converted
86
+ super(key)
86
87
  end
87
88
 
88
89
  alias_method :include?, :key?
@@ -122,12 +123,9 @@ class CastedHash < Hash
122
123
  self
123
124
  end
124
125
 
125
- def casted?(key)
126
- @casted_keys.include?(convert_key(key))
127
- end
128
-
129
- def casting?(key)
130
- @casting_keys.include?(convert_key(key))
126
+ def casted?(key, converted = false)
127
+ key = convert_key(key) unless converted
128
+ @casted_keys.include?(key)
131
129
  end
132
130
 
133
131
  def to_hash
@@ -146,15 +144,11 @@ class CastedHash < Hash
146
144
  end
147
145
  end
148
146
 
149
- def casted!(*keys)
147
+ def casted!(keys, converted = false)
148
+ keys = [keys] unless keys.is_a?(Array)
150
149
  keys.each do |key|
151
- @casted_keys << convert_key(key) if key?(key)
152
- end
153
- end
154
-
155
- def casting!(*keys)
156
- keys.each do |key|
157
- @casting_keys << convert_key(key) if key?(key)
150
+ key = convert_key(key) unless converted
151
+ @casted_keys << key if key?(key, converted)
158
152
  end
159
153
  end
160
154
 
@@ -165,8 +159,8 @@ protected
165
159
  end
166
160
 
167
161
  def cast!(key)
168
- return unless key?(key)
169
- return regular_reader(key) if casted?(key)
162
+ return unless key?(key, true)
163
+ return regular_reader(key) if casted?(key, true)
170
164
  raise SystemStackError, "already casting #{key}" if casting?(key)
171
165
 
172
166
  casting! key
@@ -183,13 +177,21 @@ protected
183
177
 
184
178
  value = regular_writer(key, value)
185
179
 
186
- casted! key
180
+ casted! key, true
187
181
 
188
182
  value
189
183
  ensure
190
184
  @casting_keys.delete key
191
185
  end
192
186
 
187
+ def casting!(key)
188
+ @casting_keys << key
189
+ end
190
+
191
+ def casting?(key)
192
+ @casting_keys.include?(key)
193
+ end
194
+
193
195
  def cast_all!
194
196
  keys.each{|key| cast! key}
195
197
  end
@@ -32,6 +32,18 @@ describe CastedHash do
32
32
  assert_equal "3", hash[:bar]
33
33
  end
34
34
 
35
+ it "does not mark key multiple times as casted" do
36
+ hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
37
+ assert_equal [], hash.instance_variable_get(:@casted_keys).to_a
38
+ assert_equal 11, hash[:a]
39
+ assert_equal ["a"], hash.instance_variable_get(:@casted_keys).to_a
40
+ hash.casted! :a
41
+ hash.casted! :b
42
+ hash.casted! :b
43
+ hash.casted! :c
44
+ assert_equal ["a", "b"], hash.instance_variable_get(:@casted_keys).to_a
45
+ end
46
+
35
47
  it "does not loop when refering to itself" do
36
48
  @hash = CastedHash.new({:a => 1}, lambda {|x| @hash[:a] + 1 })
37
49
  error = assert_raises(SystemStackError) do
@@ -40,7 +52,7 @@ describe CastedHash do
40
52
  assert_equal "already casting a", error.message
41
53
  assert_empty @hash.casted
42
54
  assert !@hash.casted?(:a)
43
- assert !@hash.casting?(:a)
55
+ assert_equal [], @hash.instance_variable_get(:@casting_keys).to_a
44
56
  end
45
57
 
46
58
  it "can check size without casting" do
@@ -277,7 +289,7 @@ describe CastedHash do
277
289
 
278
290
  it "allows bypassing of casting" do
279
291
  hash = CastedHash.new({:a => 1, :b => 2, :c => 3}, lambda {|x|x + 1})
280
- hash.casted! "a", :b
292
+ hash.casted! ["a", :b]
281
293
  assert_equal 1, hash[:a]
282
294
  assert_equal 2, hash[:b]
283
295
  assert_equal 4, hash[:c]
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.8.4
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-02 00:00:00.000000000 Z
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler