aduki 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 269ae6607c034416e171d45d6452c4fba0e5bf84
4
- data.tar.gz: bcd5234c6bf73552897059acb43633df077ce4bb
3
+ metadata.gz: 79dc256c2fd9daa39c568a3a85d1f30a3104a70b
4
+ data.tar.gz: 46d3a666d1711db748a7d8657c7eca5000dd1739
5
5
  SHA512:
6
- metadata.gz: 30966ec2c14406a5a3d91e7c3943119997c0285f90bf7f4de39166d65801374de6674a6c8278bf39b7710f35d52e720d7257817b8fbc7b542c9bc7c87248d9c0
7
- data.tar.gz: cbd2ae5d83392f9b1c6ed3e8158d10b46aff04685ad67ae06cacb137c3e44155e473177d05c00b36de9d7be08a0deac1e3eb7cab045bf647d1220b456f52f887
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 spec. It's a single file.
37
+ For a complete example, please see the specs.
38
38
 
39
- Here's an abbreviated version:
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, :weight, :speed, :builder, :team
49
- attr_accessor :assemblies, :dimensions
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
- class Model
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
- attr_accessor :name, :email, :item, :thing, :gadget
58
- attr_accessor :machines, :contraptions, :countries
59
- aduki :gadget => Gadget, :machines => Machine, :contraptions => Contraption
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
- Aduki::Initializer adds an #initialize instance method that knows how to read a hash of properties and apply them, much like ActiveRecord::Base,
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
- The #aduki class method allows you identify which types to initialise for each field. When a type is not specified for a field, aduki sets the given value directly.
110
+ m.name # => "Guillotine"
111
+ m.assembly # => nil
68
112
 
69
- aduki :gadget => Gadget, :machines => Machine, :contraptions => Contraption
113
+ You can specify an initializer for `assembly` thus:
70
114
 
71
- This line instructs aduki to initialise the #gadget field with a Gadget object. It also instructs aduki to initialize each element of the #machines
72
- array with a Machine object. Aduki decides to create an object or an array of objects depending on the attributes-hash contents, rather than on any
73
- metadata you may specify here.
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
- The following line tells aduki to expect a hash for the #helpers attribute, and for each value in the hash is should construct a new MachineBuilder instance:
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
- aduki :helpers => { :key => MachineBuilder }
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
- Aduki.apply_attributes existing_value, value
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
@@ -1,3 +1,3 @@
1
1
  module Aduki
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aduki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton