formeze 4.2.1 → 4.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39b1043817638b51294f4ca2da81e2e2fa1829f7209eb0026bedffca84c7b430
4
- data.tar.gz: 0e4d59c83e428005f01522430e740795accb24831dcc226cd1ce30eda28afada
3
+ metadata.gz: 65adcff0153dbcbf39d46bbdb6ad7a60765e57e8329b89d3003ed1e4c9c9bcf5
4
+ data.tar.gz: 99f871b6b7228ec472abaf8a2dbe820ea33a7cc5b3f3ef6dcd83f95a18410bda
5
5
  SHA512:
6
- metadata.gz: 865f278fd2c8456a1def21e04d2150d6487727eceda9bc6d1ce375cba906f4a177e76cb158eea0cbe40d1cd1c6da785efb512c301647ebbcc8155d2aae8a7061
7
- data.tar.gz: e20a37f37ebbb28932174a148cedd37fd251f97fa05986b0302ddf4e7f7897638d3f722af071060fca0704d0f97265e273478cd01721a1383b72feabb1279896
6
+ metadata.gz: d1a01576d7f38d324e8aa7c33279fd25f36227d49e124b8f53f6f56605cd632478e88616cf13c9e4c37994472905bea37cc7fe211f85ac6e98aca5dbe2bf37c1
7
+ data.tar.gz: 12564e8c34d417f605c886fe43202fdde129aea2370699ee8411cb3e4703f99fbeb4ba5a4ad054a295ae8387708da3fe6db80454a5de3f779c69852e3c7777db
data/CHANGES.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 4.3.0
2
+
3
+ * Added dependency on cgi gem
4
+
5
+ * Added support for lambda conditions without arguments
6
+
7
+ * Fixed custom validation for fields with multiple values
8
+
1
9
  # 4.2.1
2
10
 
3
11
  * Fixed regression in v4.2.0 when mime-types gem is not loaded
data/README.md CHANGED
@@ -174,7 +174,7 @@ the `defined_if` or `defined_unless` options with a proc. Here's an example
174
174
  of using the defined_if option:
175
175
 
176
176
  ```ruby
177
- field :business_name, defined_if: proc { @account.business? }
177
+ field :business_name, defined_if: ->{ @account.business? }
178
178
  ```
179
179
 
180
180
  In this example the `business_name` field will only be defined and validated
@@ -185,7 +185,7 @@ Here's an example of using the defined_unless option:
185
185
  ```ruby
186
186
  field :same_address, values: %w(true), key_required: false
187
187
 
188
- field :billing_address_line_one, defined_unless: proc { same_address? }
188
+ field :billing_address_line_one, defined_unless: ->{ same_address? }
189
189
 
190
190
  def same_address?
191
191
  same_address == 'true'
data/formeze.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'formeze'
3
- s.version = '4.2.1'
3
+ s.version = '4.3.0'
4
4
  s.license = 'LGPL-3.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ['Tim Craft']
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
  'bug_tracker_uri' => 'https://github.com/readysteady/formeze/issues',
18
18
  'changelog_uri' => 'https://github.com/readysteady/formeze/blob/main/CHANGES.md'
19
19
  }
20
+ s.add_dependency 'cgi'
20
21
  end
@@ -0,0 +1,11 @@
1
+ module Formeze::Condition
2
+ def self.evaluate(instance, block)
3
+ block = block.to_proc
4
+
5
+ if block.arity.zero?
6
+ instance.instance_exec(&block)
7
+ else
8
+ instance.instance_eval(&block)
9
+ end
10
+ end
11
+ end
data/lib/formeze/field.rb CHANGED
@@ -14,12 +14,16 @@ class Formeze::Field
14
14
 
15
15
  values.each do |value|
16
16
  if String === value
17
- validate(value, form)
17
+ value = validate(value, form)
18
18
  else
19
- validate_file(value, form)
19
+ form.add_error(self, :not_accepted, 'is not an accepted file type') unless acceptable_file?(value)
20
20
 
21
21
  size += value.size
22
22
  end
23
+
24
+ value = Array(form.send(name)).push(value) if multiple?
25
+
26
+ form.send(:"#{name}=", value)
23
27
  end
24
28
 
25
29
  form.add_error(self, :too_large, 'is too large') if maxsize? && size > maxsize
@@ -44,19 +48,7 @@ class Formeze::Field
44
48
  form.add_error(self, :bad_value, 'is invalid') if values? && !values.include?(value)
45
49
  end
46
50
 
47
- value = Array(form.send(name)).push(value) if multiple?
48
-
49
- form.send(:"#{name}=", value)
50
- end
51
-
52
- def validate_file(object, form)
53
- unless acceptable_file?(object)
54
- form.add_error(self, :not_accepted, 'is not an accepted file type')
55
- end
56
-
57
- object = Array(form.send(name)).push(object) if multiple?
58
-
59
- form.send(:"#{name}=", object)
51
+ value
60
52
  end
61
53
 
62
54
  def acceptable_file?(object)
@@ -1,11 +1,14 @@
1
1
  module Formeze::Presence
2
2
  REGEXP = /\S/
3
3
 
4
- def present?(string)
5
- string =~ REGEXP
4
+ def present?(value)
5
+ return false if value.nil?
6
+ return false if value.respond_to?(:empty?) && value.empty?
7
+ return false if value.is_a?(String) && value !~ REGEXP
8
+ return true
6
9
  end
7
10
 
8
- def blank?(string)
9
- string !~ REGEXP
11
+ def blank?(value)
12
+ !present?(value)
10
13
  end
11
14
  end
@@ -14,7 +14,7 @@ class Formeze::Validation
14
14
  end
15
15
 
16
16
  def validates?(form)
17
- @precondition ? form.instance_eval(&@precondition) : true
17
+ @precondition ? Formeze::Condition.evaluate(form, @precondition) : true
18
18
  end
19
19
 
20
20
  def field_value?(form)
data/lib/formeze.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formeze
4
+ autoload :Condition, 'formeze/condition'
4
5
  autoload :Errors, 'formeze/errors'
5
6
  autoload :Field, 'formeze/field'
6
7
  autoload :Form, 'formeze/form'
@@ -134,9 +135,9 @@ module Formeze
134
135
 
135
136
  def field_defined?(field)
136
137
  if field.defined_if?
137
- instance_eval(&field.defined_if)
138
+ Formeze::Condition.evaluate(self, field.defined_if)
138
139
  elsif field.defined_unless?
139
- !instance_eval(&field.defined_unless)
140
+ !Formeze::Condition.evaluate(self, field.defined_unless)
140
141
  else
141
142
  true
142
143
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formeze
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-08 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cgi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Ruby gem for validating form data
14
28
  email:
15
29
  - mail@timcraft.com
@@ -22,6 +36,7 @@ files:
22
36
  - README.md
23
37
  - formeze.gemspec
24
38
  - lib/formeze.rb
39
+ - lib/formeze/condition.rb
25
40
  - lib/formeze/errors.rb
26
41
  - lib/formeze/field.rb
27
42
  - lib/formeze/form.rb