bound 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f7a861be5786e9c63288302cd956278041d4465
4
+ data.tar.gz: 76a7db28ebbac84fe9596a443e8dda2cdc2dc199
5
+ SHA512:
6
+ metadata.gz: 0eaa78599c10b8719ce5b7953529015877b3b39fab5a2eb1c01f93500c0ddd5b5a92b1f02002e2236254eea9d47dbbdc8f8b1a5865fa4f6df26e87c9a8c637ff
7
+ data.tar.gz: 486ae7ca380ebb1247f40e4ffe06a5a45e229170b637069dd3d3c1f8f3a1cec7c50ca407762c46d4c9ced2baac67bb4177b67873929020c131fc9b57c41dbb23
@@ -33,6 +33,8 @@ class Bound
33
33
 
34
34
  class BoundClass
35
35
  class Attribute
36
+ NotImplemented = Class.new(RuntimeError)
37
+
36
38
  attr_reader :name, :value
37
39
  attr_accessor :nested_class
38
40
 
@@ -59,7 +61,7 @@ class Bound
59
61
  if object.respond_to?(method)
60
62
  object.send method
61
63
  else
62
- raise NoMethodError, "undefined method `#{method}' for #{object}"
64
+ raise NotImplemented, "undefined method `#{method}' for #{object}"
63
65
  end
64
66
  end
65
67
 
@@ -197,9 +199,11 @@ class Bound
197
199
  end
198
200
  end
199
201
 
200
- def initialize(hash_or_object = {})
202
+ def initialize(*seeds)
201
203
  @attributes = {}
202
- seed hash_or_object
204
+ seeds.reverse.each do |seed|
205
+ seed_with seed
206
+ end
203
207
  validate!
204
208
  end
205
209
 
@@ -220,12 +224,12 @@ class Bound
220
224
  get_attributes.map(&:name)
221
225
  end
222
226
 
223
- private
224
-
225
227
  def get_attribute(attribute_name)
226
228
  attribute_class = self.class.attrs[attribute_name]
227
229
  nested_class = self.class.nested_attr_classes[attribute_name]
228
230
 
231
+ return nil if attribute_class.nil?
232
+
229
233
  attribute = @attributes[attribute_name]
230
234
 
231
235
  unless attribute
@@ -237,21 +241,23 @@ class Bound
237
241
  attribute
238
242
  end
239
243
 
244
+ private
245
+
240
246
  def validate!
241
247
  get_attributes.each do |attribute|
242
248
  raise ArgumentError.new("Missing attribute: #{attribute.name}") unless attribute.valid?
243
249
  end
244
250
  end
245
251
 
246
- def seed(hash_or_object)
247
- case hash_or_object
252
+ def seed_with(seed)
253
+ case seed
248
254
  when Hash
249
255
  seeder = HashSeeder.new(self)
250
256
  else
251
257
  seeder = ObjectSeeder.new(self)
252
258
  end
253
259
 
254
- seeder.seed(hash_or_object)
260
+ seeder.seed(seed)
255
261
  end
256
262
  end
257
263
 
@@ -262,6 +268,9 @@ class Bound
262
268
 
263
269
  def seed(hash)
264
270
  hash.each do |key, value|
271
+ attribute = @receiver.get_attribute(key)
272
+ next if attribute && attribute.is_assigned?
273
+
265
274
  method = "#{key}="
266
275
  @receiver.send method, value
267
276
  end
@@ -275,19 +284,29 @@ class Bound
275
284
 
276
285
  def seed(object)
277
286
  @receiver.get_attributes.each do |attribute|
287
+ next if attribute.is_assigned?
288
+
278
289
  begin
279
290
  value = attribute.call_on(object)
280
- rescue NoMethodError
281
- value = nil
282
- raise ArgumentError, "missing #{attribute.name}" if attribute.required?
291
+ assign_to_receiver attribute, value
292
+ rescue BoundClass::Attribute::NotImplemented
293
+ # no assignment if object hasn't desired method
294
+ # this prevents null-assignments
283
295
  end
296
+ end
297
+ end
284
298
 
285
- method = "#{attribute.name}="
286
- if @receiver.respond_to?(method)
287
- @receiver.send method, value
288
- else
289
- raise NoMethodError, "undefined method `#{method}' for #{self}"
290
- end
299
+ private
300
+ def assign_to_receiver(attribute, value)
301
+ method = "#{attribute.name}="
302
+ send_method @receiver, method, value
303
+ end
304
+
305
+ def send_method(receiver, method, *args)
306
+ if receiver.respond_to?(method)
307
+ receiver.send method, *args
308
+ else
309
+ raise NoMethodError, "undefined method `#{method}' for #{receiver}"
291
310
  end
292
311
  end
293
312
  end
@@ -1,3 +1,3 @@
1
1
  class Bound
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -266,5 +266,25 @@ describe Bound do
266
266
  end
267
267
  end
268
268
 
269
+ describe 'seeding with multiple seeds' do
270
+ FunnyUser = Bound.required(:joke, :nose_color)
271
+
272
+ let(:hash) { {:joke => 'Text', :nose_color => 'blue'} }
273
+
274
+ it 'overwrites attributes from first to last' do
275
+ overwriting_hash = {:nose_color => 'RED'}
276
+ overwriting_object = HashObject.new(overwriting_hash)
277
+
278
+ [hash, object].each do |subject|
279
+ [overwriting_hash, overwriting_object].each do |overwriting_subject|
280
+ user = FunnyUser.new(subject, overwriting_subject)
281
+
282
+ assertion_description = [subject, overwriting_subject].inspect
283
+ assert_equal 'RED', user.nose_color, assertion_description
284
+ end
285
+ end
286
+ end
287
+ end
288
+
269
289
 
270
290
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bound
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.1.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jakob Holderbaum
@@ -15,7 +14,6 @@ dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: bundler
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ~>
29
26
  - !ruby/object:Gem::Version
@@ -31,23 +28,20 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: rake
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - '>='
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - '>='
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: minitest
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
46
  - - ~>
53
47
  - !ruby/object:Gem::Version
@@ -55,7 +49,6 @@ dependencies:
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
53
  - - ~>
61
54
  - !ruby/object:Gem::Version
@@ -63,17 +56,15 @@ dependencies:
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: simplecov
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
- - - ! '>='
60
+ - - '>='
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
- - - ! '>='
67
+ - - '>='
77
68
  - !ruby/object:Gem::Version
78
69
  version: '0'
79
70
  description:
@@ -100,33 +91,26 @@ files:
100
91
  homepage: ''
101
92
  licenses:
102
93
  - MIT
94
+ metadata: {}
103
95
  post_install_message:
104
96
  rdoc_options: []
105
97
  require_paths:
106
98
  - lib
107
99
  required_ruby_version: !ruby/object:Gem::Requirement
108
- none: false
109
100
  requirements:
110
- - - ! '>='
101
+ - - '>='
111
102
  - !ruby/object:Gem::Version
112
103
  version: '0'
113
- segments:
114
- - 0
115
- hash: -3582315724912543482
116
104
  required_rubygems_version: !ruby/object:Gem::Requirement
117
- none: false
118
105
  requirements:
119
- - - ! '>='
106
+ - - '>='
120
107
  - !ruby/object:Gem::Version
121
108
  version: '0'
122
- segments:
123
- - 0
124
- hash: -3582315724912543482
125
109
  requirements: []
126
110
  rubyforge_project:
127
- rubygems_version: 1.8.25
111
+ rubygems_version: 2.0.0
128
112
  signing_key:
129
- specification_version: 3
113
+ specification_version: 4
130
114
  summary: Implements a nice helper for fast boundary definitions
131
115
  test_files:
132
116
  - spec/bound_spec.rb