bound 0.0.6 → 0.0.7

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: 407f1b12ba2b429451193286bed998eb48a47fcb
4
- data.tar.gz: b97c11dcaa8c42fe8eda4291d9653a3722a14ceb
3
+ metadata.gz: fc68645b9e6959ecd3ece462ec5dee9ad156f4b9
4
+ data.tar.gz: 33b710637f5f69d3641b0a0db647de2b9ac5588b
5
5
  SHA512:
6
- metadata.gz: 93d70c496b9f16d3380620e56b48e7de691069cbd1d29c156184470b25db29dc096477cab62ed576c413c9a1874add6dcd8298a1bef8a153e7383696ebf60ca1
7
- data.tar.gz: ffe2af419bf9692707cca7ef4442103dbc42666fa989a24bc02e6accc031cba31a20ade6027af97cc40be28b59e0bcb9f3f3a9c7cf7a7b7d459274ed7b66d6ed
6
+ metadata.gz: 4abcc5efccc8d0ef8a0f34240e175b344b4fd363b7e78b886bba5f6347d9061607d1bc79533eddd00580c8d8b280fb2ca37f55823c542eb4bd3c0953e5049205
7
+ data.tar.gz: 23c47fd4ed1031cc045059c51c3e8c59153cbda8b77aec962860dd01c7ef90982e84fa6c42b82ecd3079576ebfc0a1b168f9d781bb890901118ed5bd223db03d
data/lib/bound.rb CHANGED
@@ -2,31 +2,44 @@ require "bound/version"
2
2
 
3
3
  class Bound
4
4
  def self.new(*args)
5
- new_bound_class(*args)
5
+ new_bound_class.set_attributes(*args)
6
+ end
7
+
8
+ def self.nested(*args)
9
+ new_bound_class.nested(*args)
10
+ end
11
+
12
+ def self.optional(*args)
13
+ new_bound_class.optional(*args)
6
14
  end
7
15
 
8
16
  private
9
17
 
10
- def self.new_bound_class(*attributes)
18
+ def self.new_bound_class
11
19
  Class.new(BoundClass) do
12
- set_attributes(*attributes)
20
+ initialize_values
13
21
  end
14
22
  end
15
23
 
16
24
  class BoundClass
17
25
  class << self
18
- attr_accessor :attributes, :optionals, :nested
26
+ attr_accessor :attributes, :optional_attributes, :nested_attributes
27
+
28
+ def initialize_values
29
+ self.attributes = []
30
+ self.optional_attributes = []
31
+ self.nested_attributes = []
32
+ end
19
33
 
20
34
  def set_attributes(*attributes)
21
- self.attributes = attributes
35
+ self.attributes += attributes
22
36
  attr_accessor *attributes
23
37
 
24
- self.optionals = []
25
- self.nested = []
38
+ self
26
39
  end
27
40
 
28
41
  def optional(*optionals)
29
- self.optionals = optionals
42
+ self.optional_attributes += optionals
30
43
  attr_accessor *optionals
31
44
 
32
45
  self
@@ -34,7 +47,7 @@ class Bound
34
47
 
35
48
  def nested(nested_attributes)
36
49
  attributes = nested_attributes.keys
37
- self.nested = attributes
50
+ self.nested_attributes += attributes
38
51
  self.attributes += attributes
39
52
  attr_reader *attributes
40
53
 
@@ -67,7 +80,7 @@ class Bound
67
80
  def inspect
68
81
  class_name = self.class.name
69
82
  id = '%0#16x' % (object_id << 1)
70
- values = (self.class.attributes + self.class.optionals).map do |attr|
83
+ values = (self.class.attributes + self.class.optional_attributes).map do |attr|
71
84
  "#{attr}=#{public_send(attr).inspect}"
72
85
  end
73
86
 
@@ -109,7 +122,7 @@ class Bound
109
122
  else
110
123
  @hash = {}
111
124
  insert_into_hash(self.class.attributes, hash_or_object)
112
- insert_into_hash(self.class.optionals, hash_or_object)
125
+ insert_into_hash(self.class.optional_attributes, hash_or_object)
113
126
  end
114
127
  end
115
128
 
data/lib/bound/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Bound
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/spec/bound_spec.rb CHANGED
@@ -166,4 +166,22 @@ describe Bound do
166
166
 
167
167
  end
168
168
  end
169
+
170
+ describe 'allows optional as constructor' do
171
+ Person = Bound.optional(:gender)
172
+
173
+ it 'works' do
174
+ assert_nil Person.new.gender
175
+ assert_equal "M", Person.new(:gender => 'M').gender
176
+ end
177
+ end
178
+
179
+ describe 'allows nested as constructor' do
180
+ Car = Bound.nested(:producer => Bound.new(:name))
181
+
182
+ it 'works' do
183
+ assert_raises(ArgumentError) { Car.new }
184
+ assert_equal "VW", Car.new(:producer => {:name => 'VW'}).producer.name
185
+ end
186
+ end
169
187
  end
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: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Holderbaum