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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e0f2fb8b553d3505325d01febe43401189b0a7d
4
- data.tar.gz: cbcfe7fb86cbbe6bb2befa53c6a6ac10436ed8a3
3
+ metadata.gz: d7d1e26f467044a7d5b7d059f7553d841550b95f
4
+ data.tar.gz: f905c88e348f9a48a0cdd73539dcc5403604cd9e
5
5
  SHA512:
6
- metadata.gz: 29df172201f7b0ffc5549ca5440e9c68953c67fdf433d29b2bc0a5163e8ab54e7e67fd7960d8b3981290a3398612e3f48619a718611b913a3f46b85cb8fb0833
7
- data.tar.gz: 823e6e0ce48c462c8b4aa89c107f56a9d188729bca21e1c5ad12deab508c8e44529b9e619390beb2cda40e8b3dbc24059fe5e331bcdaa1201c20569ac64ad4cb
6
+ metadata.gz: 6143460aa9445965acd03318c3d4dfe65745b9804e09eecca3aae92b75be208f803ae9da4f2065a1973ed2c14e010d85ec1f99758c8581fa399fc3c611a457ae
7
+ data.tar.gz: 9e2ef962d99a8e1438bbde9c6bd8b59dfa05327a9a184fdbf514cb168f3a3b47541d6e80c71f8a416f7f1f9c8ebefe2777ffacdd0495be69949b066c6b9b2e81
data/casted_hash.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "rake"
21
21
  spec.add_development_dependency "minitest", "~> 5"
22
22
  spec.add_development_dependency "coveralls"
23
+ spec.add_development_dependency "mocha"
23
24
  end
data/lib/casted_hash.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class CastedHash < Hash
2
- VERSION = "0.7.3"
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(convert_key(key), *extras)
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(convert_key(key), value)
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
- if other_hash.is_a?(CastedHash)
67
- super(other_hash).tap do
68
- other_hash.keys.each do |key|
69
- if other_hash.casted?(key)
70
- casted!(key)
71
- elsif casted?(key)
72
- uncast!(key)
73
- end
74
- end
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(convert_key(key))
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.to_s)
123
+ @casted_keys.include?(convert_key(key))
122
124
  end
123
125
 
124
126
  def casting?(key)
125
- @casting_keys.include?(key.to_s)
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.map(&:to_s).each do |key|
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.map(&:to_s).each do |key|
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.map(&:to_s)
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 convert_key(key)
187
+ @casting_keys.delete key
187
188
  end
188
189
 
189
190
  def cast_all!
data/test/helper.rb CHANGED
@@ -21,4 +21,5 @@ end
21
21
  require 'casted_hash'
22
22
 
23
23
  require 'minitest/autorun'
24
- require 'minitest/pride'
24
+ require 'minitest/pride'
25
+ require 'mocha/setup'
@@ -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.7.3
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-04-14 00:00:00.000000000 Z
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