casted_hash 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/casted_hash.rb +21 -3
- data/test/test_casted_hash.rb +3 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0e74cebfdf5a17231faffc0fd84d81649e03ebc2
|
|
4
|
+
data.tar.gz: 6e73d43bd3338e2d2f7f29954d26f26faa54af62
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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.
|
|
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
|
-
|
|
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!
|
data/test/test_casted_hash.rb
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
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-
|
|
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.
|
|
104
|
+
rubygems_version: 2.4.6
|
|
105
105
|
signing_key:
|
|
106
106
|
specification_version: 4
|
|
107
107
|
summary: A casted hash
|