aduki 0.1.2 → 0.1.3
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 +4 -4
- data/README.md +130 -20
- data/lib/aduki.rb +5 -1
- data/lib/aduki/version.rb +1 -1
- data/spec/initializer_spec.rb +14 -0
- data/spec/spec_helper.rb +2 -1
- 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: 79dc256c2fd9daa39c568a3a85d1f30a3104a70b
|
4
|
+
data.tar.gz: 46d3a666d1711db748a7d8657c7eca5000dd1739
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cf02afb1c9e116f720ade13f302d67fe54d7f1c29195a03211be5d5f7bdddcd804633889cb32da08dc446fced8f4d529aac230138f64308ef41897534daeb62
|
7
|
+
data.tar.gz: b97ea592dd63f5bb6cd986ccae8ed1f13dfbb03e7885894829df5668aaf078a973d9e3f006af7c1e3eb3dea08ebc932e475306fbeb4ee6464ca68fd2271c9dc7
|
data/README.md
CHANGED
@@ -34,49 +34,159 @@ Or install it yourself as:
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
-
For a complete example, please see the
|
37
|
+
For a complete example, please see the specs.
|
38
38
|
|
39
|
-
|
39
|
+
#### Initializing attributes
|
40
40
|
|
41
41
|
class Assembly
|
42
42
|
include Aduki::Initializer
|
43
43
|
attr_accessor :name, :colour, :size
|
44
44
|
end
|
45
45
|
|
46
|
+
a = Assembly.new name: "Cooker", colour: "blue", size: "4"
|
47
|
+
|
48
|
+
a.name # => "Cooker"
|
49
|
+
a.colour # => "blue"
|
50
|
+
a.size # => "4"
|
51
|
+
|
52
|
+
So far, so unsurprising. Let's tell aduki that `size` is an integer
|
53
|
+
|
46
54
|
class Machine
|
55
|
+
|
56
|
+
class Assembly
|
47
57
|
include Aduki::Initializer
|
48
|
-
attr_accessor :name, :
|
49
|
-
|
50
|
-
aduki :assemblies => Assembly, :builder => MachineBuilder
|
51
|
-
aduki :helpers => { :key => MachineBuilder }
|
58
|
+
attr_accessor :name, :colour
|
59
|
+
aduki size: Integer
|
52
60
|
end
|
53
61
|
|
54
|
-
|
62
|
+
a = Assembly.new name: "Cooker", colour: "blue", size: "4"
|
63
|
+
|
64
|
+
a.name # => "Cooker"
|
65
|
+
a.colour # => "blue"
|
66
|
+
a.size # => 4
|
67
|
+
|
68
|
+
Yay!
|
69
|
+
|
70
|
+
#### Nested object types
|
71
|
+
|
72
|
+
Suppose you have a Machine that uses an Assembly instance
|
73
|
+
|
74
|
+
class Machine
|
55
75
|
include Aduki::Initializer
|
76
|
+
attr_accessor :name, :speed
|
77
|
+
aduki :assembly => Assembly
|
78
|
+
end
|
56
79
|
|
57
|
-
|
58
|
-
|
59
|
-
|
80
|
+
class Assembly
|
81
|
+
include Aduki::Initializer
|
82
|
+
attr_accessor :name, :colour
|
83
|
+
aduki size: Integer
|
60
84
|
end
|
61
85
|
|
86
|
+
props = {
|
87
|
+
"name" => "Guillotine",
|
88
|
+
"speed" => "fast",
|
89
|
+
"assembly.name" => "blade",
|
90
|
+
"assembly.colour" => "steely-blue",
|
91
|
+
"assembly.size" => "12",
|
92
|
+
}
|
93
|
+
|
94
|
+
m = Machine.new props
|
95
|
+
|
96
|
+
m.name # => "Guillotine"
|
97
|
+
m.assembly.name # => "blade"
|
98
|
+
m.assembly.colour # => "steely-blue"
|
99
|
+
m.assembly.size # => 12
|
100
|
+
|
101
|
+
However, in this configuration, if you don't specify attributes for the Assembly, it will be nil:
|
102
|
+
|
103
|
+
props = {
|
104
|
+
"name" => "Guillotine",
|
105
|
+
"speed" => "fast",
|
106
|
+
}
|
62
107
|
|
63
|
-
|
64
|
-
with one major difference: Aduki splits property names on "." and applies the hash recursively. Aduki also pays special attention to "[]" in property
|
65
|
-
names, initializing an array of items instead of an item directly. Aduki uses the array index inside "[]" to distinguish elements, but not to order them.
|
108
|
+
m = Machine.new props
|
66
109
|
|
67
|
-
|
110
|
+
m.name # => "Guillotine"
|
111
|
+
m.assembly # => nil
|
68
112
|
|
69
|
-
|
113
|
+
You can specify an initializer for `assembly` thus:
|
70
114
|
|
71
|
-
|
72
|
-
|
73
|
-
|
115
|
+
class Machine
|
116
|
+
include Aduki::Initializer
|
117
|
+
attr_accessor :name, :speed
|
118
|
+
aduki_initialize :assembly, Assembly
|
119
|
+
end
|
120
|
+
|
121
|
+
props = {
|
122
|
+
"name" => "Guillotine",
|
123
|
+
"speed" => "fast",
|
124
|
+
}
|
74
125
|
|
126
|
+
m = Machine.new props
|
75
127
|
|
76
|
-
|
128
|
+
m.name # => "Guillotine"
|
129
|
+
m.assembly.name # => nil
|
130
|
+
|
131
|
+
|
132
|
+
#### Nested array types
|
133
|
+
|
134
|
+
What if your Machine needs an array of Assembly instances?
|
135
|
+
|
136
|
+
class Machine
|
137
|
+
include Aduki::Initializer
|
138
|
+
attr_accessor :name, :speed
|
139
|
+
aduki :assemblies => Assembly
|
140
|
+
end
|
141
|
+
|
142
|
+
props = {
|
143
|
+
"name" => "Truck",
|
144
|
+
"assemblies[0].name" => "cabin",
|
145
|
+
"assemblies[0].colour" => "orange",
|
146
|
+
"assemblies[0].size" => "12",
|
147
|
+
"assemblies[1].name" => "trailer",
|
148
|
+
"assemblies[1].colour" => "green",
|
149
|
+
"assemblies[1].size" => "48",
|
150
|
+
}
|
151
|
+
|
152
|
+
m = Machine.new props
|
153
|
+
|
154
|
+
m.name # => "Truck"
|
155
|
+
m.assemblies[0].name # => "cabin"
|
156
|
+
m.assemblies[0].colour # => "orange"
|
157
|
+
m.assemblies[0].size # => 12
|
158
|
+
m.assemblies[1].name # => "trailer"
|
159
|
+
m.assemblies[1].colour # => "green"
|
160
|
+
m.assemblies[1].size # => 48
|
161
|
+
|
162
|
+
However, like before, if there are no `assemblies` declarations in the initializer parameter, the `assemblies` attribute will be nil:
|
163
|
+
|
164
|
+
props = {
|
165
|
+
"name" => "Truck",
|
166
|
+
}
|
167
|
+
|
168
|
+
m = Machine.new props
|
169
|
+
|
170
|
+
m.name # => "Truck"
|
171
|
+
m.assemblies # => nil
|
172
|
+
|
173
|
+
|
174
|
+
If you want to be sure to always have an array, even empty, do this:
|
175
|
+
|
176
|
+
class Machine
|
177
|
+
include Aduki::Initializer
|
178
|
+
attr_accessor :name, :speed
|
179
|
+
aduki_initialize :assemblies, Array, Assembly
|
180
|
+
end
|
181
|
+
|
182
|
+
props = {
|
183
|
+
"name" => "Truck",
|
184
|
+
}
|
77
185
|
|
78
|
-
|
186
|
+
m = Machine.new props
|
79
187
|
|
188
|
+
m.name # => "Truck"
|
189
|
+
m.assemblies # => []
|
80
190
|
|
81
191
|
|
82
192
|
## Contributing
|
data/lib/aduki.rb
CHANGED
@@ -107,7 +107,11 @@ module Aduki
|
|
107
107
|
if value.is_a?(Hash)
|
108
108
|
existing_value = object.send setter if object.respond_to?(setter)
|
109
109
|
if existing_value
|
110
|
-
|
110
|
+
if existing_value.is_a? Hash
|
111
|
+
existing_value.merge! value
|
112
|
+
else
|
113
|
+
Aduki.apply_attributes existing_value, value
|
114
|
+
end
|
111
115
|
return
|
112
116
|
end
|
113
117
|
end
|
data/lib/aduki/version.rb
CHANGED
data/spec/initializer_spec.rb
CHANGED
@@ -251,4 +251,18 @@ describe Aduki::Initializer do
|
|
251
251
|
|
252
252
|
expect(model.gadget.speaker.threads).to eq [ 12.4, 8.16, 21.42 ]
|
253
253
|
end
|
254
|
+
|
255
|
+
it "should handle pre-initialized hashes with a previously-set array type" do
|
256
|
+
props = {
|
257
|
+
"name" => "Brackish Water",
|
258
|
+
"gadget.name" => "The Loud Gadget",
|
259
|
+
"gadget.variables.x" => "29",
|
260
|
+
"gadget.variables.y" => "12.4",
|
261
|
+
"gadget.variables.z" => "8.16",
|
262
|
+
}
|
263
|
+
|
264
|
+
model = Model.new props
|
265
|
+
|
266
|
+
expect(model.gadget.variables).to eq({ "x"=> "29", "y" => "12.4", "z" => "8.16"})
|
267
|
+
end
|
254
268
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -49,9 +49,10 @@ end
|
|
49
49
|
|
50
50
|
class Gadget
|
51
51
|
include Aduki::Initializer
|
52
|
-
attr_accessor :name, :price, :supplier
|
52
|
+
attr_accessor :name, :price, :supplier, :variables
|
53
53
|
attr_writer :wattage
|
54
54
|
aduki_initialize :speaker, Speaker
|
55
|
+
aduki_initialize :variables, Hash, nil
|
55
56
|
|
56
57
|
def watts
|
57
58
|
@wattage
|