casted_hash 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/lib/casted_hash.rb +22 -17
- data/test/test_casted_hash.rb +17 -5
- 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: 7e0f2fb8b553d3505325d01febe43401189b0a7d
|
4
|
+
data.tar.gz: cbcfe7fb86cbbe6bb2befa53c6a6ac10436ed8a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29df172201f7b0ffc5549ca5440e9c68953c67fdf433d29b2bc0a5163e8ab54e7e67fd7960d8b3981290a3398612e3f48619a718611b913a3f46b85cb8fb0833
|
7
|
+
data.tar.gz: 823e6e0ce48c462c8b4aa89c107f56a9d188729bca21e1c5ad12deab508c8e44529b9e619390beb2cda40e8b3dbc24059fe5e331bcdaa1201c20569ac64ad4cb
|
data/.travis.yml
CHANGED
data/lib/casted_hash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CastedHash < Hash
|
2
|
-
VERSION = "0.7.
|
2
|
+
VERSION = "0.7.3"
|
3
3
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
@@ -23,6 +23,15 @@ class CastedHash < Hash
|
|
23
23
|
|
24
24
|
alias_method :regular_reader, :[] unless method_defined?(:regular_reader)
|
25
25
|
|
26
|
+
def transform_keys
|
27
|
+
return enum_for(:transform_keys) unless block_given?
|
28
|
+
result = dup
|
29
|
+
each_key do |key|
|
30
|
+
result[yield(key)] = regular_reader(key)
|
31
|
+
end
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
26
35
|
def [](key)
|
27
36
|
cast! key
|
28
37
|
end
|
@@ -150,26 +159,22 @@ protected
|
|
150
159
|
@casted_keys.delete *keys.map(&:to_s)
|
151
160
|
end
|
152
161
|
|
153
|
-
def cast!(key
|
162
|
+
def cast!(key)
|
154
163
|
key = convert_key(key)
|
155
164
|
return unless key?(key)
|
165
|
+
return regular_reader(key) if casted?(key)
|
166
|
+
raise SystemStackError, "already casting #{key}" if casting?(key)
|
156
167
|
|
157
|
-
|
158
|
-
return value if casted?(key)
|
168
|
+
casting! key
|
159
169
|
|
160
|
-
if
|
161
|
-
|
170
|
+
value = if @cast_proc.arity == 1
|
171
|
+
@cast_proc.call regular_reader(key)
|
172
|
+
elsif @cast_proc.arity == 2
|
173
|
+
@cast_proc.call self, regular_reader(key)
|
174
|
+
elsif @cast_proc.arity == 3
|
175
|
+
@cast_proc.call self, key, regular_reader(key)
|
162
176
|
else
|
163
|
-
|
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
|
177
|
+
@cast_proc.call
|
173
178
|
end
|
174
179
|
|
175
180
|
value = regular_writer(key, value)
|
@@ -182,7 +187,7 @@ protected
|
|
182
187
|
end
|
183
188
|
|
184
189
|
def cast_all!
|
185
|
-
keys.each{|key| cast!
|
190
|
+
keys.each{|key| cast! key}
|
186
191
|
end
|
187
192
|
|
188
193
|
def convert_key(key)
|
data/test/test_casted_hash.rb
CHANGED
@@ -2,6 +2,23 @@ require 'helper'
|
|
2
2
|
|
3
3
|
describe CastedHash do
|
4
4
|
|
5
|
+
it "is able to transform keys without implicit casting" do
|
6
|
+
hash = CastedHash.new({:foo => 1, :bar => 1}, lambda { |x| x + 1 })
|
7
|
+
new_hash = hash.transform_keys do |key|
|
8
|
+
key.to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_equal 2, hash[:bar]
|
12
|
+
assert hash.casted?(:bar)
|
13
|
+
assert !hash.casted?(:foo)
|
14
|
+
|
15
|
+
assert_equal 2, new_hash[:bar]
|
16
|
+
assert new_hash.casted?(:bar)
|
17
|
+
assert !new_hash.casted?(:foo)
|
18
|
+
assert_equal 2, new_hash[:foo]
|
19
|
+
assert new_hash.casted?(:foo)
|
20
|
+
end
|
21
|
+
|
5
22
|
it "is able to define a cast method" do
|
6
23
|
hash = CastedHash.new({:foo => 1}, lambda { |x| x.to_s })
|
7
24
|
|
@@ -15,11 +32,6 @@ describe CastedHash do
|
|
15
32
|
assert_equal "3", hash[:bar]
|
16
33
|
end
|
17
34
|
|
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
|
-
|
23
35
|
it "does not loop when refering to itself" do
|
24
36
|
@hash = CastedHash.new({:a => 1}, lambda {|x| @hash[:a] + 1 })
|
25
37
|
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.3
|
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-04-
|
11
|
+
date: 2016-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.5.1
|
105
|
+
rubygems_version: 2.4.5.1
|
106
106
|
signing_key:
|
107
107
|
specification_version: 4
|
108
108
|
summary: A casted hash
|