formeze 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,20 +17,16 @@ what it accepts as input.
17
17
  Example usage
18
18
  -------------
19
19
 
20
- Forms are just "plain old ruby objects". Calling `Formeze.setup` will include
21
- some class methods and instance methods, but will otherwise leave the object
22
- untouched (i.e. you can define your own initialization). Here is a minimal
23
- example, which defines a form with a single "title" field:
20
+ Forms are just "plain old ruby objects" with added behaviour. Here is a
21
+ minimal example, which defines a form with a single "title" field:
24
22
 
25
23
  ```ruby
26
- class ExampleForm
27
- Formeze.setup(self)
28
-
24
+ class ExampleForm < Formeze::Form
29
25
  field :title
30
26
  end
31
27
  ```
32
28
 
33
- This form can then be used to parse and validate form/input data as follows:
29
+ This form can then be used to parse and validate input data like this:
34
30
 
35
31
  ```ruby
36
32
  form = ExampleForm.new
@@ -40,6 +36,22 @@ form.parse('title=Title')
40
36
  form.title # => "Title"
41
37
  ```
42
38
 
39
+ If you prefer not to inherit from the `Formeze::Form` class then you can
40
+ instead call the `Formeze.setup` method like this:
41
+
42
+ ```ruby
43
+ class ExampleForm
44
+ Formeze.setup(self)
45
+
46
+ field :title
47
+ end
48
+ ```
49
+
50
+ Both styles of setup will include the formeze class methods and instance
51
+ methods but will otherwise leave the object untouched (i.e. you can define
52
+ your own initialization logic).
53
+
54
+
43
55
  Detecting errors
44
56
  ----------------
45
57
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'formeze'
3
- s.version = '1.5.0'
3
+ s.version = '1.5.1'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -287,4 +287,10 @@ module Formeze
287
287
  def self.on_rails?
288
288
  defined?(Rails)
289
289
  end
290
+
291
+ class Form
292
+ def self.inherited(subclass)
293
+ Formeze.setup(subclass)
294
+ end
295
+ end
290
296
  end
@@ -3,9 +3,7 @@ require 'minitest/autorun'
3
3
  require_relative '../lib/formeze'
4
4
  require 'i18n'
5
5
 
6
- class FormWithField
7
- Formeze.setup(self)
8
-
6
+ class FormWithField < Formeze::Form
9
7
  field :title
10
8
  end
11
9
 
@@ -113,9 +111,7 @@ describe 'FormWithField after parsing input containing newlines' do
113
111
  end
114
112
  end
115
113
 
116
- class FormWithOptionalField
117
- Formeze.setup(self)
118
-
114
+ class FormWithOptionalField < Formeze::Form
119
115
  field :title, required: false
120
116
  end
121
117
 
@@ -132,9 +128,7 @@ describe 'FormWithOptionalField after parsing blank input' do
132
128
  end
133
129
  end
134
130
 
135
- class FormWithFieldThatCanHaveMultipleLines
136
- Formeze.setup(self)
137
-
131
+ class FormWithFieldThatCanHaveMultipleLines < Formeze::Form
138
132
  field :description, multiline: true
139
133
  end
140
134
 
@@ -151,9 +145,7 @@ describe 'FormWithFieldThatCanHaveMultipleLines after parsing input containing n
151
145
  end
152
146
  end
153
147
 
154
- class FormWithCharacterLimitedField
155
- Formeze.setup(self)
156
-
148
+ class FormWithCharacterLimitedField < Formeze::Form
157
149
  field :title, char_limit: 16
158
150
  end
159
151
 
@@ -170,9 +162,7 @@ describe 'FormWithCharacterLimitedField after parsing input with too many charac
170
162
  end
171
163
  end
172
164
 
173
- class FormWithWordLimitedField
174
- Formeze.setup(self)
175
-
165
+ class FormWithWordLimitedField < Formeze::Form
176
166
  field :title, word_limit: 2
177
167
  end
178
168
 
@@ -189,9 +179,7 @@ describe 'FormWithWordLimitedField after parsing input with too many words' do
189
179
  end
190
180
  end
191
181
 
192
- class FormWithFieldThatMustMatchPattern
193
- Formeze.setup(self)
194
-
182
+ class FormWithFieldThatMustMatchPattern < Formeze::Form
195
183
  field :number, pattern: /\A\d+\z/
196
184
  end
197
185
 
@@ -221,9 +209,7 @@ describe 'FormWithFieldThatMustMatchPattern after parsing input that does not ma
221
209
  end
222
210
  end
223
211
 
224
- class FormWithFieldThatCanHaveMultipleValues
225
- Formeze.setup(self)
226
-
212
+ class FormWithFieldThatCanHaveMultipleValues < Formeze::Form
227
213
  field :colour, multiple: true
228
214
  end
229
215
 
@@ -303,9 +289,7 @@ describe 'FormWithFieldThatCanHaveMultipleValues after parsing input with no val
303
289
  end
304
290
  end
305
291
 
306
- class FormWithFieldThatCanOnlyHaveSpecifiedValues
307
- Formeze.setup(self)
308
-
292
+ class FormWithFieldThatCanOnlyHaveSpecifiedValues < Formeze::Form
309
293
  field :answer, values: %w(yes no)
310
294
  end
311
295
 
@@ -322,9 +306,7 @@ describe 'FormWithFieldThatCanOnlyHaveSpecifiedValues after parsing input with a
322
306
  end
323
307
  end
324
308
 
325
- class FormWithGuardCondition
326
- Formeze.setup(self)
327
-
309
+ class FormWithGuardCondition < Formeze::Form
328
310
  field :account_name
329
311
  field :account_vat_number, defined_if: proc { @business_account }
330
312
 
@@ -370,9 +352,7 @@ describe 'FormWithGuardCondition with business_account set to false after parsin
370
352
  end
371
353
  end
372
354
 
373
- class FormWithHaltingCondition
374
- Formeze.setup(self)
375
-
355
+ class FormWithHaltingCondition < Formeze::Form
376
356
  field :delivery_address
377
357
  field :same_address, values: %w(yes no)
378
358
  field :billing_address, defined_unless: :same_address?
@@ -407,9 +387,7 @@ describe 'FormWithHaltingCondition after parsing input with same_address set and
407
387
  end
408
388
  end
409
389
 
410
- class FormWithCustomValidation
411
- Formeze.setup(self)
412
-
390
+ class FormWithCustomValidation < Formeze::Form
413
391
  field :email
414
392
 
415
393
  check { email.include?(?@) }
@@ -429,9 +407,7 @@ describe 'FormWithCustomValidation after parsing invalid input' do
429
407
  end
430
408
  end
431
409
 
432
- class FormWithOptionalKey
433
- Formeze.setup(self)
434
-
410
+ class FormWithOptionalKey < Formeze::Form
435
411
  field :accept_terms, values: %w(true), key_required: false
436
412
  end
437
413
 
@@ -448,9 +424,7 @@ describe 'FormWithOptionalKey after parsing input without the key' do
448
424
  end
449
425
  end
450
426
 
451
- class FormWithOptionalFieldThatCanOnlyHaveSpecifiedValues
452
- Formeze.setup(self)
453
-
427
+ class FormWithOptionalFieldThatCanOnlyHaveSpecifiedValues < Formeze::Form
454
428
  field :size, required: false, values: %w(S M L XL)
455
429
  end
456
430
 
@@ -469,9 +443,7 @@ end
469
443
 
470
444
  Rails = Object.new
471
445
 
472
- class RailsForm
473
- Formeze.setup(self)
474
-
446
+ class RailsForm < Formeze::Form
475
447
  field :title
476
448
  end
477
449
 
@@ -520,9 +492,7 @@ describe 'I18n integration' do
520
492
  end
521
493
  end
522
494
 
523
- class FormWithScrubbedFields
524
- Formeze.setup(self)
525
-
495
+ class FormWithScrubbedFields < Formeze::Form
526
496
  field :postcode, scrub: [:strip, :squeeze, :upcase], pattern: /\A[A-Z0-9]{2,4} [A-Z0-9]{3}\z/
527
497
  field :bio, scrub: [:strip, :squeeze_lines], multiline: true
528
498
  end
@@ -546,3 +516,14 @@ describe 'Formeze' do
546
516
  end
547
517
  end
548
518
  end
519
+
520
+ class FormClassWithExplicitSetupCall
521
+ Formeze.setup(self)
522
+ end
523
+
524
+ describe 'FormClassWithExplicitSetupCall' do
525
+ it 'includes the formeze class methods and instance methods' do
526
+ FormClassWithExplicitSetupCall.singleton_class.must_include(Formeze::ClassMethods)
527
+ FormClassWithExplicitSetupCall.must_include(Formeze::InstanceMethods)
528
+ end
529
+ 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.0
4
+ version: 1.5.1
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-08 00:00:00.000000000 Z
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n