formeze 1.5.1 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -0
- data/Rakefile.rb +2 -2
- data/formeze.gemspec +1 -1
- data/lib/formeze.rb +38 -28
- data/spec/formeze_spec.rb +39 -1
- metadata +2 -2
data/README.md
CHANGED
data/Rakefile.rb
CHANGED
data/formeze.gemspec
CHANGED
data/lib/formeze.rb
CHANGED
@@ -120,17 +120,7 @@ module Formeze
|
|
120
120
|
module ArrayAttrAccessor
|
121
121
|
def array_attr_reader(name)
|
122
122
|
define_method(name) do
|
123
|
-
|
124
|
-
|
125
|
-
values = instance_variable_get(ivar)
|
126
|
-
|
127
|
-
if values.nil?
|
128
|
-
values = []
|
129
|
-
|
130
|
-
instance_variable_set(ivar, values)
|
131
|
-
end
|
132
|
-
|
133
|
-
values
|
123
|
+
Array(instance_variable_get(:"@#{name}"))
|
134
124
|
end
|
135
125
|
end
|
136
126
|
|
@@ -138,13 +128,7 @@ module Formeze
|
|
138
128
|
define_method(:"#{name}=") do |value|
|
139
129
|
ivar = :"@#{name}"
|
140
130
|
|
141
|
-
|
142
|
-
|
143
|
-
if values.nil?
|
144
|
-
instance_variable_set(ivar, [value])
|
145
|
-
else
|
146
|
-
values << value
|
147
|
-
end
|
131
|
+
instance_variable_set(ivar, Array(instance_variable_get(ivar)) + [value])
|
148
132
|
end
|
149
133
|
end
|
150
134
|
|
@@ -188,6 +172,10 @@ module Formeze
|
|
188
172
|
def error(message)
|
189
173
|
errors << message
|
190
174
|
end
|
175
|
+
|
176
|
+
def parse(encoded_form_data)
|
177
|
+
new.tap { |form| form.parse(encoded_form_data) }
|
178
|
+
end
|
191
179
|
end
|
192
180
|
|
193
181
|
class KeyError < StandardError; end
|
@@ -219,7 +207,7 @@ module Formeze
|
|
219
207
|
scrubbed_value = field.scrub(value)
|
220
208
|
|
221
209
|
field.validate(scrubbed_value) do |error|
|
222
|
-
|
210
|
+
error!("#{field.label} #{error}", field.name)
|
223
211
|
end
|
224
212
|
|
225
213
|
send(:"#{field.name}=", scrubbed_value)
|
@@ -228,11 +216,33 @@ module Formeze
|
|
228
216
|
|
229
217
|
raise KeyError unless form_data.empty?
|
230
218
|
|
231
|
-
self.class.checks.zip(self.class.errors) do |check,
|
232
|
-
instance_eval(&check) ? next :
|
219
|
+
self.class.checks.zip(self.class.errors) do |check, message|
|
220
|
+
instance_eval(&check) ? next : error!(message)
|
233
221
|
end
|
234
222
|
end
|
235
223
|
|
224
|
+
def valid?
|
225
|
+
errors.empty?
|
226
|
+
end
|
227
|
+
|
228
|
+
def errors?
|
229
|
+
errors.size > 0
|
230
|
+
end
|
231
|
+
|
232
|
+
def errors
|
233
|
+
@errors ||= []
|
234
|
+
end
|
235
|
+
|
236
|
+
def errors_on?(field_name)
|
237
|
+
field_errors[field_name].size > 0
|
238
|
+
end
|
239
|
+
|
240
|
+
def errors_on(field_name)
|
241
|
+
field_errors[field_name]
|
242
|
+
end
|
243
|
+
|
244
|
+
private
|
245
|
+
|
236
246
|
def field_defined?(field)
|
237
247
|
if field.defined_if?
|
238
248
|
instance_eval(&field.defined_if)
|
@@ -243,16 +253,16 @@ module Formeze
|
|
243
253
|
end
|
244
254
|
end
|
245
255
|
|
246
|
-
def
|
247
|
-
@
|
256
|
+
def field_errors
|
257
|
+
@field_errors ||= Hash.new { |h, k| h[k] = [] }
|
248
258
|
end
|
249
259
|
|
250
|
-
def
|
251
|
-
|
252
|
-
end
|
260
|
+
def error!(message, field_name = nil)
|
261
|
+
error = UserError.new(message)
|
253
262
|
|
254
|
-
|
255
|
-
|
263
|
+
errors << error
|
264
|
+
|
265
|
+
field_errors[field_name] << error unless field_name.nil?
|
256
266
|
end
|
257
267
|
end
|
258
268
|
|
data/spec/formeze_spec.rb
CHANGED
@@ -69,6 +69,20 @@ describe 'FormWithField after parsing valid input' do
|
|
69
69
|
@form.errors.must_be_empty
|
70
70
|
end
|
71
71
|
end
|
72
|
+
|
73
|
+
describe 'errors_on query method' do
|
74
|
+
it 'should return false when given the title field name' do
|
75
|
+
@form.errors_on?(:title).must_equal(false)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'errors_on method' do
|
80
|
+
it 'should return an empty array when given the title field name' do
|
81
|
+
errors = @form.errors_on(:title)
|
82
|
+
errors.must_be_instance_of(Array)
|
83
|
+
errors.must_be_empty
|
84
|
+
end
|
85
|
+
end
|
72
86
|
end
|
73
87
|
|
74
88
|
describe 'FormWithField after parsing blank input' do
|
@@ -90,12 +104,27 @@ describe 'FormWithField after parsing blank input' do
|
|
90
104
|
end
|
91
105
|
|
92
106
|
describe 'errors method' do
|
93
|
-
it 'should return an array containing
|
107
|
+
it 'should return an array containing a single error message' do
|
94
108
|
@form.errors.must_be_instance_of(Array)
|
95
109
|
@form.errors.length.must_equal(1)
|
96
110
|
@form.errors.first.to_s.must_equal('Title is required')
|
97
111
|
end
|
98
112
|
end
|
113
|
+
|
114
|
+
describe 'errors_on query method' do
|
115
|
+
it 'should return true when given the title field name' do
|
116
|
+
@form.errors_on?(:title).must_equal(true)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'errors_on method' do
|
121
|
+
it 'should return an array containing a single error message when given the title field name' do
|
122
|
+
errors = @form.errors_on(:title)
|
123
|
+
errors.must_be_instance_of(Array)
|
124
|
+
errors.length.must_equal(1)
|
125
|
+
errors.first.to_s.must_equal('Title is required')
|
126
|
+
end
|
127
|
+
end
|
99
128
|
end
|
100
129
|
|
101
130
|
describe 'FormWithField after parsing input containing newlines' do
|
@@ -111,6 +140,15 @@ describe 'FormWithField after parsing input containing newlines' do
|
|
111
140
|
end
|
112
141
|
end
|
113
142
|
|
143
|
+
describe 'FormWithField parse class method' do
|
144
|
+
it 'should create a new instance of the class and call the parse instance method' do
|
145
|
+
form = FormWithField.parse('title=Untitled')
|
146
|
+
form.must_be_instance_of(FormWithField)
|
147
|
+
form.valid?.must_equal(true)
|
148
|
+
form.title.must_equal('Untitled')
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
114
152
|
class FormWithOptionalField < Formeze::Form
|
115
153
|
field :title, required: false
|
116
154
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formeze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|