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 +4 -4
- data/lib/roda/component/form.rb +39 -23
- data/lib/roda/component/form/validations.rb +1 -1
- data/lib/roda/component/version.rb +1 -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: 3597012e5d7d5f29e2313055ae0d4bf2636f2e61
|
4
|
+
data.tar.gz: 0c9f50edc7029a9a827b4605e27b5459c8dd5926
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a71b97dd28899591a0e33bc68e5b6cd9b62ea9700ef8a42fff5bf714d8a734850776473c7d998f49c864a8fb0fbb554e45cd253b1e11097f30b615a6bbdf59d7
|
7
|
+
data.tar.gz: a42ee5d887dbb60490faf2f462d08729d426becb1fcef526cc7c16ab39bdbe20d13cbb349c2591c3865c21e3d6ea6b71367a69750baae4643b162c45ec2d0bd6
|
data/lib/roda/component/form.rb
CHANGED
@@ -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
|
-
|
108
|
-
|
109
|
-
|
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.
|
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'
|