casted_hash 0.7.0 → 0.7.1

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: 0e74cebfdf5a17231faffc0fd84d81649e03ebc2
4
- data.tar.gz: 6e73d43bd3338e2d2f7f29954d26f26faa54af62
3
+ metadata.gz: a1417da9dece7f7c45f0ada321d3a8323cd9d870
4
+ data.tar.gz: 58bf74f4889f564454b40aa08cc1904d611b4478
5
5
  SHA512:
6
- metadata.gz: 86f80bc590f0705ae084ed8d2b2c5f42ae860da57fb2243625431f282e5b412fe58135e902ce223104dac202645583b134f5239851c140b22d20ba0aeb5434b3
7
- data.tar.gz: 69a9513a358a264f7a649427d8151e930de231598a79d050a5870a0d7c557f0518b608654c3885af0a6aa218ea7afe46ace06378f981d2df7209a9f83d3dff34
6
+ metadata.gz: 78a0cd7f8705a8b389e1ebd5e051444e34c300fceba7bbfe4b751537f68547b27b6cbbdc06c17c0f28ce7480a681d2ea9c7e2e8cae8daa56ac5b221ce70f4b30
7
+ data.tar.gz: 05cd9e5be0896c45a755aecb64bdb5e70778e40fcda9b191dfc314bd756c4b40a519561cb21797e6c9c1e8046e0e1ffb4d00457c8f36e4b0051ecce62f045197
data/lib/casted_hash.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class CastedHash < Hash
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
 
4
4
  def initialize(constructor = {}, cast_proc = nil)
5
5
  raise ArgumentError, "`cast_proc` required" unless cast_proc
@@ -148,31 +148,33 @@ protected
148
148
 
149
149
  def uncast!(*keys)
150
150
  @casted_keys.delete *keys.map(&:to_s)
151
- @casting_keys.delete *keys.map(&:to_s)
152
151
  end
153
152
 
154
153
  def cast!(key)
154
+ key = convert_key(key)
155
155
  return unless key?(key)
156
- return regular_reader(convert_key(key)) if casted?(key)
157
- raise SystemStackError if casting?(key)
156
+ return regular_reader(key) if casted?(key)
157
+ raise SystemStackError, "already casting #{key}" if casting?(key)
158
158
 
159
159
  casting! key
160
160
 
161
161
  value = if @cast_proc.arity == 1
162
- @cast_proc.call regular_reader(convert_key(key))
162
+ @cast_proc.call regular_reader(key)
163
163
  elsif @cast_proc.arity == 2
164
- @cast_proc.call self, regular_reader(convert_key(key))
164
+ @cast_proc.call self, regular_reader(key)
165
165
  elsif @cast_proc.arity == 3
166
- @cast_proc.call self, key, regular_reader(convert_key(key))
166
+ @cast_proc.call self, key, regular_reader(key)
167
167
  else
168
168
  @cast_proc.call
169
169
  end
170
170
 
171
- value = regular_writer(convert_key(key), value)
171
+ value = regular_writer(key, value)
172
172
 
173
173
  casted! key
174
174
 
175
175
  value
176
+ ensure
177
+ @casting_keys.delete convert_key(key)
176
178
  end
177
179
 
178
180
  def cast_all!
@@ -17,9 +17,13 @@ describe CastedHash do
17
17
 
18
18
  it "does not loop when refering to itself" do
19
19
  @hash = CastedHash.new({:a => 1}, lambda {|x| @hash[:a] + 1 })
20
- assert_raises(SystemStackError) do
20
+ error = assert_raises(SystemStackError) do
21
21
  @hash[:a]
22
22
  end
23
+ assert_equal "already casting a", error.message
24
+ assert_empty @hash.casted
25
+ assert !@hash.casted?(:a)
26
+ assert !@hash.casting?(:a)
23
27
  end
24
28
 
25
29
  it "can check size without casting" 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.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephan Kaag