casted_hash 0.5.0 → 0.5.1
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 +11 -5
- 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: e8d0e1bf59f7f60cbc0ade6d0845c3e5dac3cad7
|
|
4
|
+
data.tar.gz: 44f2485a4e0c2a5a686ac72f025e638240be5e34
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b706bb8d8e5d4efbe59a633c075776938198bd0d00a50951500fda289d30f003b376e94ab5b12d364df7c199ded8fb0ce78180b37b92039bfe670aca8c45526f
|
|
7
|
+
data.tar.gz: 3d65221300c869f5670b4757bf1c9471056f8fec2607f0889978394eb5aef9e582c2a465085c65f1c680dd459e1fe6c803614fda43a6ea7c4fa3defce6dc31a8
|
data/lib/casted_hash.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class CastedHash < Hash
|
|
2
|
-
VERSION = "0.5.
|
|
2
|
+
VERSION = "0.5.1"
|
|
3
3
|
|
|
4
4
|
def initialize(constructor = {}, cast_proc = nil)
|
|
5
5
|
raise ArgumentError, "`cast_proc` required" unless cast_proc
|
|
@@ -40,7 +40,7 @@ class CastedHash < Hash
|
|
|
40
40
|
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
|
41
41
|
|
|
42
42
|
def []=(key, value)
|
|
43
|
-
|
|
43
|
+
uncast! key
|
|
44
44
|
regular_writer(convert_key(key), value)
|
|
45
45
|
end
|
|
46
46
|
|
|
@@ -54,7 +54,7 @@ class CastedHash < Hash
|
|
|
54
54
|
return self if other_hash.empty?
|
|
55
55
|
|
|
56
56
|
if other_hash.is_a? CastedHash
|
|
57
|
-
other_hash.keys.each{|key|
|
|
57
|
+
other_hash.keys.each{|key| uncast! key}
|
|
58
58
|
super(other_hash)
|
|
59
59
|
else
|
|
60
60
|
other_hash.each_pair { |key, value| self[key] = value }
|
|
@@ -81,7 +81,7 @@ class CastedHash < Hash
|
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def delete(key)
|
|
84
|
-
|
|
84
|
+
uncast! key
|
|
85
85
|
super(convert_key(key))
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -126,6 +126,10 @@ class CastedHash < Hash
|
|
|
126
126
|
|
|
127
127
|
protected
|
|
128
128
|
|
|
129
|
+
def uncast!(*keys)
|
|
130
|
+
@casted_keys.delete *keys.map(&:to_s)
|
|
131
|
+
end
|
|
132
|
+
|
|
129
133
|
def cast!(key)
|
|
130
134
|
return unless key?(key)
|
|
131
135
|
return regular_reader(convert_key(key)) if casted?(key)
|
|
@@ -134,7 +138,9 @@ protected
|
|
|
134
138
|
value = if @cast_proc.arity == 1
|
|
135
139
|
@cast_proc.call regular_reader(convert_key(key))
|
|
136
140
|
elsif @cast_proc.arity == 2
|
|
137
|
-
@cast_proc.call
|
|
141
|
+
@cast_proc.call self, regular_reader(convert_key(key))
|
|
142
|
+
elsif @cast_proc.arity == 3
|
|
143
|
+
@cast_proc.call self, key, regular_reader(convert_key(key))
|
|
138
144
|
else
|
|
139
145
|
@cast_proc.call
|
|
140
146
|
end
|