bound 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bound.rb +24 -11
- data/lib/bound/version.rb +1 -1
- data/spec/bound_spec.rb +18 -0
- 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: fc68645b9e6959ecd3ece462ec5dee9ad156f4b9
|
4
|
+
data.tar.gz: 33b710637f5f69d3641b0a0db647de2b9ac5588b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
18
|
+
def self.new_bound_class
|
11
19
|
Class.new(BoundClass) do
|
12
|
-
|
20
|
+
initialize_values
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
16
24
|
class BoundClass
|
17
25
|
class << self
|
18
|
-
attr_accessor :attributes, :
|
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
|
35
|
+
self.attributes += attributes
|
22
36
|
attr_accessor *attributes
|
23
37
|
|
24
|
-
self
|
25
|
-
self.nested = []
|
38
|
+
self
|
26
39
|
end
|
27
40
|
|
28
41
|
def optional(*optionals)
|
29
|
-
self.
|
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.
|
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.
|
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.
|
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
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
|