casted_hash 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/casted_hash.rb +6 -2
- data/test/test_casted_hash.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67cd2ef7fba5b62383915098fa8c7ed095daae42
|
4
|
+
data.tar.gz: f556daabdb5492f66d54dd16ec1906902ff69538
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d2d005f170d7448823d6f1e6e124ad8e1e3868319e1181e8c2e7428b7022fa84b0baf074654a1cae3d90f7d954424c4eb0e3b4b9137a32f000105dc2f5ea247
|
7
|
+
data.tar.gz: 9d0f46b2265d9da6117055e8746406156e8ebfb53bb587ab6cc19fc5a56bf1ccf86047cf0ad7600789704a512ea8fca3a75aacdce3b3cb13ea65220956743d48
|
data/lib/casted_hash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class CastedHash < Hash
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.5.0"
|
3
3
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
@@ -120,12 +120,16 @@ class CastedHash < Hash
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
+
def casted!(*keys)
|
124
|
+
@casted_keys.concat keys.map(&:to_s)
|
125
|
+
end
|
126
|
+
|
123
127
|
protected
|
124
128
|
|
125
129
|
def cast!(key)
|
126
130
|
return unless key?(key)
|
127
131
|
return regular_reader(convert_key(key)) if casted?(key)
|
128
|
-
|
132
|
+
casted! key
|
129
133
|
|
130
134
|
value = if @cast_proc.arity == 1
|
131
135
|
@cast_proc.call regular_reader(convert_key(key))
|
data/test/test_casted_hash.rb
CHANGED
@@ -230,4 +230,12 @@ describe CastedHash do
|
|
230
230
|
assert_equal 3, hash.fetch(:b)
|
231
231
|
assert_equal({"a" => 2, "b" => 3}, hash.casted)
|
232
232
|
end
|
233
|
+
|
234
|
+
it "allows bypassing of casting" do
|
235
|
+
hash = CastedHash.new({:a => 1, :b => 2, :c => 3}, lambda {|x|x + 1})
|
236
|
+
hash.casted! "a", :b
|
237
|
+
assert_equal 1, hash[:a]
|
238
|
+
assert_equal 2, hash[:b]
|
239
|
+
assert_equal 4, hash[:c]
|
240
|
+
end
|
233
241
|
end
|