bound 2.0.1 → 2.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7019abbcba367ffde338074a378485042f539b0b
4
- data.tar.gz: 41ec48902a59084d62cb5a6b7ee281b32dc29296
3
+ metadata.gz: 2b4fefc3a0104facef9758ab7c625956d2b7aa61
4
+ data.tar.gz: 6d9fd0b0f0751f68f9aab060d28f5784c441daa2
5
5
  SHA512:
6
- metadata.gz: 10fccfaecf7a65a8b92de435b3d8865306eee571ba916f9dfbeb97a95dc7aca6dcf2e856ac885b74210ff26d944f2c3c28125db1d04d4f0d5c7a0465a872ec12
7
- data.tar.gz: cbfc59f94eb61dbd59b70c15b119cf1635a159cdf6918d11cf891de114a4e97df161dd0e5f30a1830357cf2af74bb72aff44b358aabd86a19955fc397da51611
6
+ metadata.gz: 9a0d2d024a524b3d7f6072d293cf0a083315a72fc059630c0cd25a88a336f0423900c08511bd8108ea9a35c8c1009a298f11ab76c61ffbb4141685028e9a09e8
7
+ data.tar.gz: c97baed792361e20ee2a9673d7343d51905083e210e299caea362318c9ef71a73dec36ac5e296382dfa7e6ca2a12a640863e6e8fefcdfeb181e8d545d6301049
@@ -33,24 +33,26 @@ class Bound
33
33
  class BoundValidator
34
34
  attr_accessor :attributes, :optional_attributes, :nested_array_attributes
35
35
 
36
- def initialize(target, overwrite)
36
+ def initialize(bound, target, overwrite)
37
+ @bound = bound
37
38
  @target = target
38
39
  @overwrite = overwrite
39
40
  end
40
41
 
41
42
  def validate!
42
- ensure_all_attributes_are_known!
43
+ ensure_all_given_attributes_are_known!
43
44
  attributes.each do |attribute|
44
45
  ensure_present! attribute
45
46
  end
46
47
  nested_array_attributes.each do |nested_array_attribute|
47
48
  ensure_array! nested_array_attribute
48
49
  end
50
+ ensure_all_attributes_are_callable!
49
51
  end
50
52
 
51
53
  private
52
54
 
53
- def ensure_all_attributes_are_known!
55
+ def ensure_all_given_attributes_are_known!
54
56
  (overwritten_attrs + target_attrs).each do |attr|
55
57
  unless (attributes + optional_attributes).include? attr
56
58
  a = (attributes + optional_attributes).inspect
@@ -81,6 +83,16 @@ class Bound
81
83
  end
82
84
  end
83
85
 
86
+ def ensure_all_attributes_are_callable!
87
+ attributes.each do |attr|
88
+ @bound.send attr
89
+ end
90
+ optional_attributes.each do |attr|
91
+ @bound.send attr if set? attr
92
+ end
93
+ end
94
+
95
+
84
96
  def overwritten_attrs
85
97
  if @overwrite
86
98
  @overwrite.keys
@@ -114,6 +126,10 @@ class Bound
114
126
  @target &&
115
127
  @target.kind_of?(Hash)?@target[attr]:@target.send(attr)
116
128
  end
129
+
130
+ def set?(attr)
131
+ target_has?(attr) || overwritten?(attr)
132
+ end
117
133
  end
118
134
 
119
135
  class StaticBoundClass
@@ -185,7 +201,7 @@ class Bound
185
201
  end.join(',')
186
202
  code = <<-EOR
187
203
  def validate!
188
- v = Bound::BoundValidator.new(@t, @o)
204
+ v = Bound::BoundValidator.new(self, @t, @o)
189
205
  v.attributes = [#{attributes}]
190
206
  v.optional_attributes = [#{optional_attributes}]
191
207
  v.nested_array_attributes = [#{nested_array_attributes}]
@@ -1,3 +1,3 @@
1
1
  class Bound
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -15,6 +15,16 @@ describe Bound do
15
15
  end
16
16
  end
17
17
 
18
+ it 'does not cache the set attributes' do
19
+ user = User.new(hash)
20
+ hash[:name] = 'AAA'
21
+ assert_equal 'AAA', user.name
22
+
23
+ user = User.new(object)
24
+ object.name = 'AAA'
25
+ assert_equal 'AAA', user.name
26
+ end
27
+
18
28
  it 'fails if attribute is missing' do
19
29
  hash.delete :age
20
30
 
@@ -158,6 +168,16 @@ describe Bound do
158
168
  UserWithProfile.new(subject)
159
169
  end
160
170
  end
171
+
172
+ it 'fails if argument of optional nested bound is missing' do
173
+ hash[:profile].delete(:age)
174
+ [hash, object].each do |subject|
175
+ error = assert_raises ArgumentError do
176
+ UserWithProfile.new(subject)
177
+ end
178
+ assert_match(/missing/i, error.message)
179
+ end
180
+ end
161
181
  end
162
182
 
163
183
  describe 'no attributes' do
@@ -189,6 +209,26 @@ describe Bound do
189
209
  assert_equal hash[:company][:address][:street], user.company.address.street
190
210
  end
191
211
  end
212
+
213
+ it 'fails if nested attributes are missing' do
214
+ hash[:company].delete(:name)
215
+ [hash, object].each do |subject|
216
+ error = assert_raises ArgumentError do
217
+ user = EmployedUser.new(subject)
218
+ end
219
+ assert_match(/missing/i, error.message)
220
+ end
221
+ end
222
+
223
+ it 'does not cache values in the nested bound' do
224
+ user = EmployedUser.new(hash)
225
+ hash[:company][:name] = 'AAA'
226
+ assert_equal 'AAA', user.company.name
227
+
228
+ user = EmployedUser.new(object)
229
+ object.company.name = 'AAA'
230
+ assert_equal 'AAA', user.company.name
231
+ end
192
232
  end
193
233
 
194
234
  describe 'array of nested attribute' do
@@ -214,6 +254,26 @@ describe Bound do
214
254
  end
215
255
  end
216
256
 
257
+ it 'fails if nested bound is missing an attribute' do
258
+ hash[:posts][1].delete(:title)
259
+ [hash, object].each do |subject|
260
+ error = assert_raises ArgumentError do
261
+ BloggingUser.new(subject)
262
+ end
263
+ assert_match(/missing/i, error.message)
264
+ end
265
+ end
266
+
267
+ it 'does not cache values in the array' do
268
+ user = BloggingUser.new(hash)
269
+ hash[:posts][0][:title] = 'AAA'
270
+ assert_equal 'AAA', user.posts[0].title
271
+
272
+ user = BloggingUser.new(object)
273
+ object.posts[0].title = 'AAA'
274
+ assert_equal 'AAA', user.posts[0].title
275
+ end
276
+
217
277
  describe 'equality' do
218
278
  let(:user) { BloggingUser.new(hash) }
219
279
  it 'is given if the nested attributes are equal' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bound
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Holderbaum
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-23 00:00:00.000000000 Z
12
+ date: 2014-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler