casted_hash 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/README.md +1 -1
- data/lib/casted_hash.rb +17 -13
- data/test/test_casted_hash.rb +5 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70eb29b065f3d4a69aabf9338cb8805c1873444e
|
4
|
+
data.tar.gz: f01d58fd7cbba6eaad7ff0ed9c472091e13386e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e74b73ae86ecac1b17819be1ddb49976866bb8c01ccf1f3d9047516299e1b7124e4ee2d1bc4728f8b6507f791861badac84dd4edfea66ce3385368bba72cb380
|
7
|
+
data.tar.gz: 25b22c49c76ca944481fc27d90dae823a70e14f9d6ca9be606728b3b286c1ce6310b585df4dcc0cd9f0cfa56ff35d374cc03b2bad777b21898ed807c7b55c336
|
data/.travis.yml
ADDED
data/README.md
CHANGED
data/lib/casted_hash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CastedHash < Hash
|
2
|
-
VERSION = "0.7.
|
2
|
+
VERSION = "0.7.2"
|
3
3
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
@@ -150,22 +150,26 @@ protected
|
|
150
150
|
@casted_keys.delete *keys.map(&:to_s)
|
151
151
|
end
|
152
152
|
|
153
|
-
def cast!(key)
|
153
|
+
def cast!(key, do_raise = true)
|
154
154
|
key = convert_key(key)
|
155
155
|
return unless key?(key)
|
156
|
-
return regular_reader(key) if casted?(key)
|
157
|
-
raise SystemStackError, "already casting #{key}" if casting?(key)
|
158
156
|
|
159
|
-
|
157
|
+
value = regular_reader(key)
|
158
|
+
return value if casted?(key)
|
160
159
|
|
161
|
-
|
162
|
-
|
163
|
-
elsif @cast_proc.arity == 2
|
164
|
-
@cast_proc.call self, regular_reader(key)
|
165
|
-
elsif @cast_proc.arity == 3
|
166
|
-
@cast_proc.call self, key, regular_reader(key)
|
160
|
+
if casting?(key)
|
161
|
+
raise SystemStackError, "already casting #{key}" if do_raise
|
167
162
|
else
|
168
|
-
|
163
|
+
casting! key
|
164
|
+
value = if @cast_proc.arity == 1
|
165
|
+
@cast_proc.call value
|
166
|
+
elsif @cast_proc.arity == 2
|
167
|
+
@cast_proc.call self, value
|
168
|
+
elsif @cast_proc.arity == 3
|
169
|
+
@cast_proc.call self, key, value
|
170
|
+
else
|
171
|
+
@cast_proc.call
|
172
|
+
end
|
169
173
|
end
|
170
174
|
|
171
175
|
value = regular_writer(key, value)
|
@@ -178,7 +182,7 @@ protected
|
|
178
182
|
end
|
179
183
|
|
180
184
|
def cast_all!
|
181
|
-
keys.each{|key| cast!
|
185
|
+
keys.each{|key| cast!(key, false)}
|
182
186
|
end
|
183
187
|
|
184
188
|
def convert_key(key)
|
data/test/test_casted_hash.rb
CHANGED
@@ -15,6 +15,11 @@ describe CastedHash do
|
|
15
15
|
assert_equal "3", hash[:bar]
|
16
16
|
end
|
17
17
|
|
18
|
+
it "can use casted_hash in cast method" do
|
19
|
+
@hash = CastedHash.new({:a => 1}, lambda {|x| @hash.casted_hash; 2 })
|
20
|
+
assert_equal 2, @hash[:a]
|
21
|
+
end
|
22
|
+
|
18
23
|
it "does not loop when refering to itself" do
|
19
24
|
@hash = CastedHash.new({:a => 1}, lambda {|x| @hash[:a] + 1 })
|
20
25
|
error = assert_raises(SystemStackError) 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.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephan Kaag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
@@ -101,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
102
|
version: '0'
|
102
103
|
requirements: []
|
103
104
|
rubyforge_project:
|
104
|
-
rubygems_version: 2.
|
105
|
+
rubygems_version: 2.5.1
|
105
106
|
signing_key:
|
106
107
|
specification_version: 4
|
107
108
|
summary: A casted hash
|