casted_hash 0.6.0 → 0.7.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: a75e1ca5209da87620971999bede83c599689a7f
4
- data.tar.gz: 9dfa369c27920138f3bae6a523a2eccc886c66c2
3
+ metadata.gz: 0e74cebfdf5a17231faffc0fd84d81649e03ebc2
4
+ data.tar.gz: 6e73d43bd3338e2d2f7f29954d26f26faa54af62
5
5
  SHA512:
6
- metadata.gz: 3fb74c2c6fc7949c73c59593fb4b4657709fa50184b755593e85dcd8b1bd825e92cc395aebde8542b257cdc21ac69bc97b1f512fb10d7afb62e6114f55e2195f
7
- data.tar.gz: 0323de581f4e49bd0d455b9a9bea600aaa8fd1dbd5e1880fed7472e134490fd7b6309dc0c8902ff2f4c77f79bfa7caea112f56c81bf33c6ff1cffca3dec92536
6
+ metadata.gz: 86f80bc590f0705ae084ed8d2b2c5f42ae860da57fb2243625431f282e5b412fe58135e902ce223104dac202645583b134f5239851c140b22d20ba0aeb5434b3
7
+ data.tar.gz: 69a9513a358a264f7a649427d8151e930de231598a79d050a5870a0d7c557f0518b608654c3885af0a6aa218ea7afe46ace06378f981d2df7209a9f83d3dff34
data/lib/casted_hash.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  class CastedHash < Hash
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.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
9
 
9
10
  if constructor.is_a?(CastedHash)
10
11
  super()
@@ -111,6 +112,10 @@ class CastedHash < Hash
111
112
  @casted_keys.include?(key.to_s)
112
113
  end
113
114
 
115
+ def casting?(key)
116
+ @casting_keys.include?(key.to_s)
117
+ end
118
+
114
119
  def to_hash
115
120
  Hash.new.tap do |hash|
116
121
  keys.each do |key|
@@ -133,16 +138,25 @@ class CastedHash < Hash
133
138
  end
134
139
  end
135
140
 
141
+ def casting!(*keys)
142
+ keys.map(&:to_s).each do |key|
143
+ @casting_keys << key if key?(key)
144
+ end
145
+ end
146
+
136
147
  protected
137
148
 
138
149
  def uncast!(*keys)
139
150
  @casted_keys.delete *keys.map(&:to_s)
151
+ @casting_keys.delete *keys.map(&:to_s)
140
152
  end
141
153
 
142
154
  def cast!(key)
143
155
  return unless key?(key)
144
156
  return regular_reader(convert_key(key)) if casted?(key)
145
- casted! key
157
+ raise SystemStackError if casting?(key)
158
+
159
+ casting! key
146
160
 
147
161
  value = if @cast_proc.arity == 1
148
162
  @cast_proc.call regular_reader(convert_key(key))
@@ -154,7 +168,11 @@ protected
154
168
  @cast_proc.call
155
169
  end
156
170
 
157
- regular_writer(convert_key(key), value)
171
+ value = regular_writer(convert_key(key), value)
172
+
173
+ casted! key
174
+
175
+ value
158
176
  end
159
177
 
160
178
  def cast_all!
@@ -17,7 +17,9 @@ 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_equal 2, @hash[:a]
20
+ assert_raises(SystemStackError) do
21
+ @hash[:a]
22
+ end
21
23
  end
22
24
 
23
25
  it "can check size without casting" 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.6.0
4
+ version: 0.7.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: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  requirements: []
103
103
  rubyforge_project:
104
- rubygems_version: 2.4.5
104
+ rubygems_version: 2.4.6
105
105
  signing_key:
106
106
  specification_version: 4
107
107
  summary: A casted hash