roda-component 0.1.24 → 0.1.25

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: ff8336613ebc26d6354cb84d5f66318bb85bbcc9
4
- data.tar.gz: 600e954b6328e17635784196b778ee41da113db7
3
+ metadata.gz: 3597012e5d7d5f29e2313055ae0d4bf2636f2e61
4
+ data.tar.gz: 0c9f50edc7029a9a827b4605e27b5459c8dd5926
5
5
  SHA512:
6
- metadata.gz: 3c956b16f6ee27862ca8a9a9eff6ab2e60a3c784424777fa9c98790591835f286aacb0666b6dd9b9188d3ec7c109f83dce73acd875862d0be34e91025a2466a2
7
- data.tar.gz: 004554a394e137ed9424028168a2329e9aa92636a80a80174b61b46cd13b6e5b169bd536a94b71511e4dde7e1bc0463852e5282c62e9e5e96db0dfba59c54ded
6
+ metadata.gz: a71b97dd28899591a0e33bc68e5b6cd9b62ea9700ef8a42fff5bf714d8a734850776473c7d998f49c864a8fb0fbb554e45cd253b1e11097f30b615a6bbdf59d7
7
+ data.tar.gz: a42ee5d887dbb60490faf2f462d08729d426becb1fcef526cc7c16ab39bdbe20d13cbb349c2591c3865c21e3d6ea6b71367a69750baae4643b162c45ec2d0bd6
@@ -1,10 +1,25 @@
1
1
  require 'roda/component/form/validations'
2
+ require 'forwardable'
2
3
 
3
4
  class Roda
4
5
  class Component
5
6
  class Form
6
7
  include Validations
7
8
 
9
+ module Delegates
10
+ def _delegates(*names)
11
+ accessors = Module.new do
12
+ extend Forwardable # DISCUSS: do we really need Forwardable here?
13
+ names.each do |name|
14
+ delegate [name, "#{name}="] => :_attributes
15
+ end
16
+ end
17
+ include accessors
18
+ end
19
+ end
20
+
21
+ extend Delegates
22
+
8
23
  class Attributes
9
24
  def set_values(atts)
10
25
  @_attributes = []
@@ -22,6 +37,8 @@ class Roda
22
37
  define_singleton_method "#{attr}=" do |value|
23
38
  value = value.to_obj if value.is_a? Hash
24
39
  instance_variable_set(:"@#{attr}", value)
40
+ @_attributes ||= []
41
+ @_attributes << attr
25
42
  end
26
43
 
27
44
  define_singleton_method attr do
@@ -78,10 +95,16 @@ class Roda
78
95
  @_attributes.set_attr_accessors _attr_accessors
79
96
  @_attributes.set_values _data
80
97
 
98
+ _data.each do |key, val|
99
+ send("#{key}=", val)
100
+ end
101
+
81
102
  _form.each do |key, klass|
82
103
  opts = {}
83
- opts[key] = _data.send(key) if _data.respond_to?(key)
104
+ opts[key] = klass.new(_data.send(key)) if _data.respond_to?(key)
84
105
  @_attributes.set_values opts
106
+
107
+ send("#{key}=", opts[key])
85
108
  end
86
109
  end
87
110
 
@@ -101,15 +124,14 @@ class Roda
101
124
  end
102
125
  end
103
126
  end
127
+
128
+ _delegates(*_attr_accessors)
104
129
  end
105
130
 
106
131
  def method_missing method, *args, &block
107
- # respond_to?(symbol, include_all=false)
108
- if _attributes.respond_to? method, true
109
- _attributes.send method, *args, &block
110
- else
111
- super
112
- end
132
+ return if method[/\=\z/]
133
+
134
+ super
113
135
  end
114
136
 
115
137
  # Return hash of attributes and values.
@@ -131,10 +153,11 @@ class Roda
131
153
  data.each do |k, v|
132
154
  if klass = _form[k.to_s.to_sym]
133
155
  data = data[k]
156
+ data = data.attributes if data.is_a?(Form)
134
157
 
135
158
  f = klass.new data
136
159
  k = "#{k}_attributes"
137
- data = f.attributes
160
+ data = f.model_attributes
138
161
 
139
162
  hash[k] = model_attributes data
140
163
  elsif v.is_a? Hash
@@ -219,13 +242,6 @@ class Roda
219
242
  keys.each do |k|
220
243
  begin
221
244
  value = value ? value.send(k) : send(k)
222
-
223
- if klass = _form[k.to_s.to_sym]
224
- options = {}
225
- options[:key] = _options[:key] if _options.key? :key
226
-
227
- value = klass.new(value, options)
228
- end
229
245
  rescue
230
246
  value = ''
231
247
  end
@@ -244,8 +260,16 @@ class Roda
244
260
  end
245
261
  end
246
262
 
263
+ def _attributes
264
+ @_attributes ||= {}
265
+ end
266
+
247
267
  protected
248
268
 
269
+ def _data
270
+ @_data ||= {}
271
+ end
272
+
249
273
  def self._attr_accessors
250
274
  @_attr_accessors
251
275
  end
@@ -254,18 +278,10 @@ class Roda
254
278
  @_form
255
279
  end
256
280
 
257
- def _attributes
258
- @_attributes ||= []
259
- end
260
-
261
281
  def _form
262
282
  self.class._form
263
283
  end
264
284
 
265
- def _data
266
- @_data ||= []
267
- end
268
-
269
285
  def _attr_accessors
270
286
  self.class._attr_accessors
271
287
  end
@@ -112,7 +112,7 @@ class Roda
112
112
  options = {}
113
113
  options[:key] = _options[:key] if _options.key? :key
114
114
 
115
- f = klass.new(send(att), options)
115
+ f = klass.new(send(att).attributes, options)
116
116
  assert(f.valid?, [att, f.errors])
117
117
  else
118
118
  binding.pry if att.to_s == 'line1'
@@ -1,5 +1,5 @@
1
1
  class Roda
2
2
  class Component
3
- VERSION = "0.1.24"
3
+ VERSION = "0.1.25"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj