casted_hash 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/casted_hash.gemspec +1 -0
- data/lib/casted_hash.rb +27 -26
- data/test/helper.rb +2 -1
- data/test/test_casted_hash.rb +11 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7d1e26f467044a7d5b7d059f7553d841550b95f
|
4
|
+
data.tar.gz: f905c88e348f9a48a0cdd73539dcc5403604cd9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6143460aa9445965acd03318c3d4dfe65745b9804e09eecca3aae92b75be208f803ae9da4f2065a1973ed2c14e010d85ec1f99758c8581fa399fc3c611a457ae
|
7
|
+
data.tar.gz: 9e2ef962d99a8e1438bbde9c6bd8b59dfa05327a9a184fdbf514cb168f3a3b47541d6e80c71f8a416f7f1f9c8ebefe2777ffacdd0495be69949b066c6b9b2e81
|
data/casted_hash.gemspec
CHANGED
data/lib/casted_hash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CastedHash < Hash
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.8.0"
|
3
3
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
@@ -33,14 +33,15 @@ class CastedHash < Hash
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def [](key)
|
36
|
-
cast! key
|
36
|
+
cast! convert_key(key)
|
37
37
|
end
|
38
38
|
|
39
39
|
def fetch(key, *extras)
|
40
|
+
key = convert_key(key)
|
40
41
|
value = cast!(key)
|
41
42
|
|
42
43
|
if value.nil?
|
43
|
-
super(
|
44
|
+
super(key, *extras)
|
44
45
|
else
|
45
46
|
value
|
46
47
|
end
|
@@ -50,8 +51,9 @@ class CastedHash < Hash
|
|
50
51
|
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
51
52
|
|
52
53
|
def []=(key, value)
|
54
|
+
key = convert_key(key)
|
53
55
|
uncast! key
|
54
|
-
regular_writer(
|
56
|
+
regular_writer(key, value)
|
55
57
|
end
|
56
58
|
|
57
59
|
alias_method :store, :[]=
|
@@ -63,20 +65,19 @@ class CastedHash < Hash
|
|
63
65
|
def update(other_hash)
|
64
66
|
return self if other_hash.empty?
|
65
67
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
other_hash.each_pair do |key, value|
|
69
|
+
converted_key = convert_key(key)
|
70
|
+
|
71
|
+
regular_writer converted_key, value
|
72
|
+
|
73
|
+
if other_hash.is_a?(CastedHash) && other_hash.casted?(key)
|
74
|
+
casted!(key)
|
75
|
+
elsif casted?(key)
|
76
|
+
uncast!(converted_key)
|
75
77
|
end
|
76
|
-
else
|
77
|
-
other_hash.each_pair { |key, value| self[key] = value }
|
78
|
-
self
|
79
78
|
end
|
79
|
+
|
80
|
+
self
|
80
81
|
end
|
81
82
|
|
82
83
|
alias_method :merge!, :update
|
@@ -98,8 +99,9 @@ class CastedHash < Hash
|
|
98
99
|
end
|
99
100
|
|
100
101
|
def delete(key)
|
102
|
+
key = convert_key(key)
|
101
103
|
uncast! key
|
102
|
-
super(
|
104
|
+
super(key)
|
103
105
|
end
|
104
106
|
|
105
107
|
def values
|
@@ -118,11 +120,11 @@ class CastedHash < Hash
|
|
118
120
|
end
|
119
121
|
|
120
122
|
def casted?(key)
|
121
|
-
@casted_keys.include?(key
|
123
|
+
@casted_keys.include?(convert_key(key))
|
122
124
|
end
|
123
125
|
|
124
126
|
def casting?(key)
|
125
|
-
@casting_keys.include?(key
|
127
|
+
@casting_keys.include?(convert_key(key))
|
126
128
|
end
|
127
129
|
|
128
130
|
def to_hash
|
@@ -142,25 +144,24 @@ class CastedHash < Hash
|
|
142
144
|
end
|
143
145
|
|
144
146
|
def casted!(*keys)
|
145
|
-
keys.
|
146
|
-
@casted_keys << key if key?(key)
|
147
|
+
keys.each do |key|
|
148
|
+
@casted_keys << convert_key(key) if key?(key)
|
147
149
|
end
|
148
150
|
end
|
149
151
|
|
150
152
|
def casting!(*keys)
|
151
|
-
keys.
|
152
|
-
@casting_keys << key if key?(key)
|
153
|
+
keys.each do |key|
|
154
|
+
@casting_keys << convert_key(key) if key?(key)
|
153
155
|
end
|
154
156
|
end
|
155
157
|
|
156
158
|
protected
|
157
159
|
|
158
160
|
def uncast!(*keys)
|
159
|
-
@casted_keys.delete *keys
|
161
|
+
@casted_keys.delete *keys
|
160
162
|
end
|
161
163
|
|
162
164
|
def cast!(key)
|
163
|
-
key = convert_key(key)
|
164
165
|
return unless key?(key)
|
165
166
|
return regular_reader(key) if casted?(key)
|
166
167
|
raise SystemStackError, "already casting #{key}" if casting?(key)
|
@@ -183,7 +184,7 @@ protected
|
|
183
184
|
|
184
185
|
value
|
185
186
|
ensure
|
186
|
-
@casting_keys.delete
|
187
|
+
@casting_keys.delete key
|
187
188
|
end
|
188
189
|
|
189
190
|
def cast_all!
|
data/test/helper.rb
CHANGED
data/test/test_casted_hash.rb
CHANGED
@@ -99,8 +99,10 @@ describe CastedHash do
|
|
99
99
|
|
100
100
|
it "deletes values" do
|
101
101
|
hash = CastedHash.new({:a => 1, :b => 2}, lambda {|x| x + 10 })
|
102
|
+
assert_equal 12, hash[:b]
|
102
103
|
hash.delete(:b)
|
103
104
|
assert_equal({"a" => 11}, hash.casted_hash)
|
105
|
+
assert !hash.casted?(:b)
|
104
106
|
end
|
105
107
|
|
106
108
|
describe "merge" do
|
@@ -280,4 +282,13 @@ describe CastedHash do
|
|
280
282
|
assert_equal 2, hash[:b]
|
281
283
|
assert_equal 4, hash[:c]
|
282
284
|
end
|
285
|
+
|
286
|
+
it "always uses the regular_writer method when merging" do
|
287
|
+
hash = CastedHash.new({}, lambda {|x| x + 1})
|
288
|
+
hash.expects(:regular_writer).twice
|
289
|
+
hash.merge!(:a => 1, :b => 2)
|
290
|
+
|
291
|
+
hash.expects(:regular_writer)
|
292
|
+
hash.merge!(CastedHash.new({:a => 1}, lambda {|x| x + 1}))
|
293
|
+
end
|
283
294
|
end
|
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.8.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: 2016-
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: A casted hash
|
70
84
|
email:
|
71
85
|
- stephan@ka.ag
|