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 CHANGED
@@ -14,6 +14,14 @@ of being "strict by default", forcing the application code to be explicit in
14
14
  what it accepts as input.
15
15
 
16
16
 
17
+ Installation
18
+ ------------
19
+
20
+ ```
21
+ $ gem install formeze
22
+ ```
23
+
24
+
17
25
  Example usage
18
26
  -------------
19
27
 
data/Rakefile.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rake/testtask'
2
2
 
3
- task :default => :test
3
+ task :default => :spec
4
4
 
5
- Rake::TestTask.new do |t|
5
+ Rake::TestTask.new(:spec) do |t|
6
6
  t.test_files = FileList['spec/*_spec.rb']
7
7
  end
data/formeze.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'formeze'
3
- s.version = '1.5.1'
3
+ s.version = '1.6.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
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
- ivar = :"@#{name}"
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
- values = instance_variable_get(ivar)
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
- errors << UserError.new("#{field.label} #{error}")
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, error|
232
- instance_eval(&check) ? next : errors << UserError.new(error)
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 errors
247
- @errors ||= []
256
+ def field_errors
257
+ @field_errors ||= Hash.new { |h, k| h[k] = [] }
248
258
  end
249
259
 
250
- def errors?
251
- errors.size > 0
252
- end
260
+ def error!(message, field_name = nil)
261
+ error = UserError.new(message)
253
262
 
254
- def valid?
255
- errors.empty?
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 an error message' do
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.5.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-22 00:00:00.000000000 Z
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n