lotus-validations 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8038d2ee15fc8a3e5205d528f56db97024928714
4
- data.tar.gz: 9136c6849b93c7869c4dbea6d91b163cce353a3c
3
+ metadata.gz: 36c1599d9dc59f556a6041f459b41fe23a629e12
4
+ data.tar.gz: 16fade6ce990dfa2dc5f6bd477dae2bdf0160ab5
5
5
  SHA512:
6
- metadata.gz: 904a4cab5995195acfe618eb621ee6d48cd1a3746fb0546aa4f16ac983f67109c1969026bbfc0f48337006922eb9cd8391a8fecdf56afbfed77ba175f6a3d86c
7
- data.tar.gz: 5bb93ddc815abdb33028dcbf16896acf276cdb75bcf4eb414e61ff28932b7ad90c1782cb1de57bdcdd8d1c925e60eaf38417b27747a92c99b7ea2c727d8c5ec9
6
+ metadata.gz: b53e4dfe4cc649cd1f7818d00b5de4a8171d6a0ffded3b12e603497abdbec84a31d97d062b87ed38b130e314beef3b06408a4632f957ba257823b9a4bafb22dc
7
+ data.tar.gz: 32dfdd1ed995e28ce04b631c2f9ee367347c7dfdf05936e9771f2c1254ea0aad763a91c62e0921c4ad9433487f5259bfb71e24a1133f7a7011aed78c9fa604b2
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Lotus::Validations
2
2
  Validations mixin for Ruby objects
3
3
 
4
+ ## v0.2.1 - 2014-12-23
5
+ ### Added
6
+ - [Luca Guidi] Introduced `Validations::Errors#to_h` and `to_a`
7
+ - [Luca Guidi] Introduced `Validations::Errors#any?`
8
+
9
+ ### Fixed
10
+ - [Satoshi Amemiya] Made `Validations#valid?` idempotent
11
+
4
12
  ## v0.2.0 - 2014-11-23
5
13
  ### Added
6
14
  - [Luca Guidi] Skip attribute whitelisting when a validator does not define any attribute
@@ -20,23 +20,24 @@ module Lotus
20
20
  #
21
21
  # @see Lotus::Validations::Attribute#confirmation
22
22
  #
23
- # @since x.x.x
23
+ # @since 0.2.0
24
24
  # @api private
25
25
  CONFIRMATION_TEMPLATE = '%{name}_confirmation'.freeze
26
26
 
27
+ # @since 0.2.0
28
+ # @api private
29
+ BLANK_STRING_MATCHER = /\A[[:space:]]*\z/.freeze
30
+
27
31
  # Instantiate an attribute
28
32
  #
29
- # @param validator [Lotus::Validations] an object which included
30
- # Lotus::Validations module
33
+ # @param attributes [Hash] a set of attributes and values coming from the
34
+ # input
31
35
  # @param name [Symbol] the name of the attribute
32
- # @param options [Hash] the set of validations for the attribute
36
+ # @param value [Object,nil] the value coming from the input
37
+ # @param validations [Hash] a set of validation rules
33
38
  #
34
- # @since x.x.x
39
+ # @since 0.2.0
35
40
  # @api private
36
- # def initialize(validator, name, options)
37
- # @validator, @name, @options = validator, name, options
38
- # @value = _attribute(@name)
39
- # end
40
41
  def initialize(attributes, name, value, validations)
41
42
  @attributes = attributes
42
43
  @name = name
@@ -46,7 +47,7 @@ module Lotus
46
47
  end
47
48
 
48
49
  # @api private
49
- # @since x.x.x
50
+ # @since 0.2.0
50
51
  def validate
51
52
  _with_cleared_errors do
52
53
  presence
@@ -57,9 +58,10 @@ module Lotus
57
58
  end
58
59
 
59
60
  # @api private
60
- # @since x.x.x
61
+ # @since 0.2.0
61
62
  def value
62
63
  if (coercer = @validations[:type])
64
+ return nil if blank_value?
63
65
  Lotus::Validations::Coercions.coerce(coercer, @value)
64
66
  else
65
67
  @value
@@ -76,7 +78,7 @@ module Lotus
76
78
  # @see Lotus::Validations::ClassMethods#attribute
77
79
  # @see Lotus::Validations::Attribute#nil_value?
78
80
  #
79
- # @since x.x.x
81
+ # @since 0.2.0
80
82
  # @api private
81
83
  def presence
82
84
  _validate(__method__) { !blank_value? }
@@ -92,7 +94,7 @@ module Lotus
92
94
  # @see Lotus::Validations::ClassMethods#attribute
93
95
  # @see http://www.rubydoc.info/gems/lotus-utils/Lotus/Utils/Kernel#Boolean-class_method
94
96
  #
95
- # @since x.x.x
97
+ # @since 0.2.0
96
98
  # @api private
97
99
  def acceptance
98
100
  _validate(__method__) { Lotus::Utils::Kernel.Boolean(@value) }
@@ -105,7 +107,7 @@ module Lotus
105
107
  #
106
108
  # @see Lotus::Validations::ClassMethods#attribute
107
109
  #
108
- # @since x.x.x
110
+ # @since 0.2.0
109
111
  # @api private
110
112
  def format
111
113
  _validate(__method__) {|matcher| @value.to_s.match(matcher) }
@@ -117,10 +119,10 @@ module Lotus
117
119
  #
118
120
  # @see Lotus::Validations::ClassMethods#attribute
119
121
  #
120
- # @since x.x.x
122
+ # @since 0.2.0
121
123
  # @api private
122
124
  def inclusion
123
- _validate(__method__) {|collection| collection.include?(@value) }
125
+ _validate(__method__) {|collection| collection.include?(value) }
124
126
  end
125
127
 
126
128
  # Validates exclusion of the value in the defined collection.
@@ -129,10 +131,10 @@ module Lotus
129
131
  #
130
132
  # @see Lotus::Validations::ClassMethods#attribute
131
133
  #
132
- # @since x.x.x
134
+ # @since 0.2.0
133
135
  # @api private
134
136
  def exclusion
135
- _validate(__method__) {|collection| !collection.include?(@value) }
137
+ _validate(__method__) {|collection| !collection.include?(value) }
136
138
  end
137
139
 
138
140
  # Validates confirmation of the value with another corresponding value.
@@ -143,7 +145,7 @@ module Lotus
143
145
  # @see Lotus::Validations::ClassMethods#attribute
144
146
  # @see Lotus::Validations::Attribute::CONFIRMATION_TEMPLATE
145
147
  #
146
- # @since x.x.x
148
+ # @since 0.2.0
147
149
  # @api private
148
150
  def confirmation
149
151
  _validate(__method__) do
@@ -180,15 +182,15 @@ module Lotus
180
182
  #
181
183
  # @see Lotus::Validations::ClassMethods#attribute
182
184
  #
183
- # @since x.x.x
185
+ # @since 0.2.0
184
186
  # @api private
185
187
  def size
186
188
  _validate(__method__) do |validator|
187
189
  case validator
188
190
  when Numeric, ->(v) { v.respond_to?(:to_int) }
189
- @value.size == validator.to_int
191
+ value.size == validator.to_int
190
192
  when Range
191
- validator.include?(@value.size)
193
+ validator.include?(value.size)
192
194
  else
193
195
  raise ArgumentError.new("Size validator must be a number or a range, it was: #{ validator }")
194
196
  end
@@ -221,18 +223,18 @@ module Lotus
221
223
  # @see Lotus::Validations::ClassMethods#attribute
222
224
  # @see Lotus::Validations::Coercions
223
225
  #
224
- # @since x.x.x
226
+ # @since 0.2.0
225
227
  # @api private
226
228
  def coerce
227
229
  _validate(:type) do |coercer|
228
- @value = Lotus::Validations::Coercions.coerce(coercer, @value)
230
+ Lotus::Validations::Coercions.coerce(coercer, @value)
229
231
  true
230
232
  end
231
233
  end
232
234
 
233
235
  # Checks if the value is `nil`.
234
236
  #
235
- # @since x.x.x
237
+ # @since 0.2.0
236
238
  # @api private
237
239
  def nil_value?
238
240
  @value.nil?
@@ -244,15 +246,27 @@ module Lotus
244
246
  #
245
247
  # @see Lotus::Validations::Attribute#presence
246
248
  #
247
- # @since x.x.x
249
+ # @since 0.2.0
248
250
  # @api private
249
251
  def blank_value?
250
- nil_value? || (@value.respond_to?(:empty?) && @value.empty?)
252
+ nil_value? || _blank_string? || _empty_value?
253
+ end
254
+
255
+ # @since 0.2.0
256
+ # @api private
257
+ def _blank_string?
258
+ (@value.respond_to?(:match) and @value.match(BLANK_STRING_MATCHER))
259
+ end
260
+
261
+ # @since 0.2.0
262
+ # @api private
263
+ def _empty_value?
264
+ (@value.respond_to?(:empty?) and @value.empty?)
251
265
  end
252
266
 
253
267
  # Run the defined validations
254
268
  #
255
- # @since x.x.x
269
+ # @since 0.2.0
256
270
  # @api private
257
271
  def _run_validations
258
272
  return if skip?
@@ -266,7 +280,7 @@ module Lotus
266
280
  end
267
281
 
268
282
  # @api private
269
- # @since x.x.x
283
+ # @since 0.2.0
270
284
  def _with_cleared_errors
271
285
  @errors.clear
272
286
  yield
@@ -275,7 +289,7 @@ module Lotus
275
289
 
276
290
  # Reads an attribute from the validator.
277
291
  #
278
- # @since x.x.x
292
+ # @since 0.2.0
279
293
  # @api private
280
294
  def _attribute(name = @name)
281
295
  @attributes[name.to_sym]
@@ -283,7 +297,7 @@ module Lotus
283
297
 
284
298
  # Run a single validation and collects the results.
285
299
  #
286
- # @since x.x.x
300
+ # @since 0.2.0
287
301
  # @api private
288
302
  def _validate(validation)
289
303
  if (validator = @validations[validation]) && !(yield validator)
@@ -34,7 +34,7 @@ class AttributeSet
34
34
  # @raise [ArgumentError] if at least one of the validations are not
35
35
  # recognized
36
36
  #
37
- # @since x.x.x
37
+ # @since 0.2.0
38
38
  # @api private
39
39
  def validate_options!(name, options)
40
40
  if (unknown = (options.keys - VALIDATIONS)) && unknown.any?
@@ -23,10 +23,23 @@ module Lotus
23
23
  # @return [TrueClass,FalseClass] the result of the check
24
24
  #
25
25
  # @since 0.1.0
26
+ #
27
+ # @see Lotus::Validations::Errors#any?
26
28
  def empty?
27
29
  @errors.empty?
28
30
  end
29
31
 
32
+ # Check if the set has any entry
33
+ #
34
+ # @return [TrueClass,FalseClass] the result of the check
35
+ #
36
+ # @since 0.2.0
37
+ #
38
+ # @see Lotus::Validations::Errors#empty?
39
+ def any?
40
+ @errors.any?
41
+ end
42
+
30
43
  # Returns how many validations have failed
31
44
  #
32
45
  # @return [Fixnum] the count
@@ -74,12 +87,12 @@ module Lotus
74
87
  # Add an error to the set
75
88
  #
76
89
  # @param attribute [Symbol] the name of the attribute
77
- # @param validation [Symbol] the name of the validation
78
- # @param expected [Object] the expected value
79
- # @param actual [Object] the actual value
90
+ # @param errors [Array] a collection of errors
80
91
  #
81
92
  # @since 0.1.0
82
93
  # @api private
94
+ #
95
+ # @see Lotus::Validations::Error
83
96
  def add(attribute, *errors)
84
97
  @errors[attribute].push(*errors) if errors.any?
85
98
  end
@@ -108,6 +121,24 @@ module Lotus
108
121
 
109
122
  alias_method :eql?, :==
110
123
 
124
+ # Return a serializable Hash representation of the errors.
125
+ #
126
+ # @return [Lotus::Utils::Hash] the Hash
127
+ #
128
+ # @since 0.2.1
129
+ def to_h
130
+ Utils::Hash.new(@errors).deep_dup
131
+ end
132
+
133
+ # Return a flat collection of errors.
134
+ #
135
+ # @return [Array]
136
+ #
137
+ # @since 0.2.1
138
+ def to_a
139
+ errors.dup
140
+ end
141
+
111
142
  protected
112
143
  # A flatten set of errors for all the attributes
113
144
  #
@@ -1,6 +1,6 @@
1
1
  module Lotus
2
2
  module Validations
3
3
  # @since 0.1.0
4
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.2.1'.freeze
5
5
  end
6
6
  end
@@ -32,7 +32,7 @@ module Lotus
32
32
  #
33
33
  # @param base [Class] the target action
34
34
  #
35
- # @since x.x.x
35
+ # @since 0.1.0
36
36
  # @api private
37
37
  #
38
38
  # @see http://www.ruby-doc.org/core/Module.html#method-i-included
@@ -384,7 +384,7 @@ module Lotus
384
384
  # @yieldparam attribute [Symbol] the name of the attribute
385
385
  # @yieldparam value [Object,nil] the value of the attribute
386
386
  #
387
- # @since x.x.x
387
+ # @since 0.2.0
388
388
  def each(&blk)
389
389
  to_h.each(&blk)
390
390
  end
@@ -394,7 +394,7 @@ module Lotus
394
394
  #
395
395
  # @return [Hash]
396
396
  #
397
- # @since x.x.x
397
+ # @since 0.1.0
398
398
  def to_h
399
399
  @attributes.dup
400
400
  end
@@ -402,7 +402,7 @@ module Lotus
402
402
  private
403
403
  # The set of user defined attributes.
404
404
  #
405
- # @since x.x.x
405
+ # @since 0.1.0
406
406
  # @api private
407
407
  #
408
408
  # @see Lotus::Validations::ClassMethods#attributes
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Lotus::Validations::VERSION
9
9
  spec.authors = ['Luca Guidi']
10
10
  spec.email = ['me@lucaguidi.com']
11
- spec.summary = %q{Validations for Lotus}
12
- spec.description = %q{Validate attributes on a class and manage error messages accordingly}
11
+ spec.summary = %q{Validations mixin for Ruby objects}
12
+ spec.description = %q{Validations mixin for Ruby objects and support for Lotus}
13
13
  spec.homepage = 'http://lotusrb.org'
14
14
  spec.license = 'MIT'
15
15
 
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
  spec.required_ruby_version = '>= 2.0.0'
21
21
 
22
- spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.1'
22
+ spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.2'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.6'
25
25
  spec.add_development_dependency 'minitest', '~> 5'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-23 00:00:00.000000000 Z
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lotus-utils
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '0.3'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.3.1
22
+ version: 0.3.2
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '0.3'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.3.1
32
+ version: 0.3.2
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -72,7 +72,7 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '10'
75
- description: Validate attributes on a class and manage error messages accordingly
75
+ description: Validations mixin for Ruby objects and support for Lotus
76
76
  email:
77
77
  - me@lucaguidi.com
78
78
  executables: []
@@ -115,6 +115,6 @@ rubyforge_project:
115
115
  rubygems_version: 2.2.2
116
116
  signing_key:
117
117
  specification_version: 4
118
- summary: Validations for Lotus
118
+ summary: Validations mixin for Ruby objects
119
119
  test_files: []
120
120
  has_rdoc: