lotus-validations 0.3.0 → 0.3.1

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
  SHA1:
3
- metadata.gz: 4749d31a52499ff7d0c98b64b6320385710088e3
4
- data.tar.gz: 6c59dcc59fedca2f7b96292694c33dc1f9266a86
3
+ metadata.gz: 0698ed51ac049cae199e98abad4addfbb485845f
4
+ data.tar.gz: 3c76854dfa9d49e51d794e0d42c13912316e2271
5
5
  SHA512:
6
- metadata.gz: 238e5d8ef149a598764242f04ff4edc97643bae2f34da0de797aecba58ed55907ff06f21e71588ac38404a39f61d3eb07b62fb2ed322b70ee1c55c5443c728cc
7
- data.tar.gz: 49fb9594fb7512596587733f6cb946ecc2c1d67a9a815b26b5659755c9e2413eb15535c1a0036d9af8f5f02213c583f881536dd992c3cadc18872a6cad9135d2
6
+ metadata.gz: 2be7bcf9a481123a449ae177dc8e3bf1becb08678e460b7ac4cb08c21dd784401a5e34be547ea9c537d11fa12bef586dac9407705d5713f26c0443998cbf7b87
7
+ data.tar.gz: 3080435f881d64ae783f145c06f9005e5a3cd4a1825aaf49fbb3bd49de0c44224a29eefd2407b802e00a5e3186ed5c2b022d2bc3a3395c6cc10f412329f0ab30
@@ -1,6 +1,11 @@
1
1
  # Lotus::Validations
2
2
  Validations mixin for Ruby objects
3
3
 
4
+ ## v0.3.1 - 2015-05-15
5
+ ### Fixed
6
+ - [Luca Guidi] Fixed Hash serialization for nested validations. It always return nested `::Hash` structure.
7
+ - [Alfonso Uceda Pompa & Dmitry Tymchuk] Fixed Hash serialization when `Lotus::Entity` is included in the same class.
8
+
4
9
  ## v0.3.0 - 2015-03-23
5
10
 
6
11
  ## v0.2.4 - 2015-01-30
data/README.md CHANGED
@@ -197,7 +197,7 @@ signup.valid? # => false
197
197
  An attribute is valid if it's value and the value of a corresponding attribute
198
198
  is valid.
199
199
 
200
- By convention, if you have a `password` attribute, the validation looks for `password_validation`.
200
+ By convention, if you have a `password` attribute, the validation looks for `password_confirmation`.
201
201
 
202
202
  ```ruby
203
203
  require 'lotus/validations'
@@ -386,7 +386,7 @@ validator.valid? # => false
386
386
  ```
387
387
 
388
388
  Bulk operations on errors are guaranteed by `#each`.
389
- This method yields a **flatten collection of errors**.
389
+ This method yields a **flattened collection of errors**.
390
390
 
391
391
  ```ruby
392
392
  validator.errors.each do |error|
@@ -483,9 +483,9 @@ end
483
483
 
484
484
  ### Errors
485
485
 
486
- When you invoke `#valid?`, validations errors are available at `#errors`.
486
+ When you invoke `#valid?`, validation errors are available at `#errors`.
487
487
  It's a set of errors grouped by attribute. Each error contains the name of the
488
- invalid attribute, the failed validation, the expected value and the current one.
488
+ invalid attribute, the failed validation, the expected value, and the current one.
489
489
 
490
490
  ```ruby
491
491
  require 'lotus/validations'
@@ -518,7 +518,7 @@ signup.errors
518
518
 
519
519
  ### Lotus::Entity
520
520
 
521
- Integration with [`Lotus::Entity`](https://github.com/lotus/model) is activated automatically.
521
+ Integration with [`Lotus::Entity`](https://github.com/lotus/model) is straight forward.
522
522
 
523
523
  ```ruby
524
524
  require 'lotus/model'
@@ -244,7 +244,10 @@ module Lotus
244
244
  #
245
245
  # @since 0.1.0
246
246
  def to_h
247
- Utils::Hash.new(read_attributes).deep_dup
247
+ # TODO remove this symbolization when we'll support Ruby 2.2+ only
248
+ Utils::Hash.new(
249
+ @attributes
250
+ ).deep_dup.symbolize!.to_h
248
251
  end
249
252
 
250
253
  private
@@ -1,5 +1,6 @@
1
1
  require 'set'
2
2
  require 'lotus/utils/attributes'
3
+ require 'lotus/validations/nested_attributes'
3
4
 
4
5
  module Lotus
5
6
  module Validations
@@ -254,6 +255,16 @@ module Lotus
254
255
  # signup = Signup.new(password: 'short')
255
256
  # signup.valid? # => false
256
257
  def attribute(name, options = {}, &block)
258
+ _attribute(name, options, &block)
259
+ end
260
+
261
+ # Define an attribute
262
+ #
263
+ # @see Lotus::Validations::AttributeDefiner#attribute
264
+ #
265
+ # @since 0.3.1
266
+ # @api private
267
+ def _attribute(name, options = {}, &block)
257
268
  if block_given?
258
269
  define_nested_attribute(name, options, &block)
259
270
  validates(name, {})
@@ -357,12 +368,8 @@ module Lotus
357
368
  #
358
369
  # @since 0.2.4
359
370
  # @api private
360
- def build_validation_class(&block)
361
- kls = Class.new do
362
- include Lotus::Validations
363
- end
364
- kls.class_eval(&block)
365
- kls
371
+ def build_validation_class(&blk)
372
+ NestedAttributes.fabricate(&blk)
366
373
  end
367
374
  end
368
375
 
@@ -401,13 +408,33 @@ module Lotus
401
408
  end
402
409
  end
403
410
 
411
+ # @return [Array<String>]
412
+ #
413
+ # @since 0.3.1
414
+ # @api private
415
+ def defined_attributes
416
+ super
417
+ @defined_attributes.merge(attributes.map(&:to_s))
418
+ end
419
+
420
+ # Override attribute accessors function.
421
+ #
422
+ # @since 0.3.1
423
+ #
424
+ # @api private
425
+ # @see Lotus::Model::Entity#define_attr_accessor
426
+ def define_attr_accessor(attr)
427
+ _attribute(attr)
428
+ super
429
+ end
430
+
404
431
  # @since 0.2.3
405
432
  # @api private
406
433
  #
407
434
  # @see Lotus::Validations::AttributeDefiner#attribute
408
435
  def attribute(name, options = {})
409
- super
410
436
  attributes name
437
+ super
411
438
  end
412
439
 
413
440
  # @since 0.2.3
@@ -430,6 +457,12 @@ module Lotus
430
457
  def assign_attribute?(attr)
431
458
  super || attr.to_s == LOTUS_ENTITY_ID
432
459
  end
460
+
461
+ def initialize(attributes = {})
462
+ super
463
+ @attributes.set(LOTUS_ENTITY_ID, id)
464
+ self.class.attribute(LOTUS_ENTITY_ID)
465
+ end
433
466
  end
434
467
  end
435
468
 
@@ -0,0 +1,22 @@
1
+ module Lotus
2
+ module Validations
3
+ # @since 0.3.1
4
+ # @api private
5
+ class NestedAttributes
6
+ # @since 0.3.1
7
+ # @api private
8
+ def self.fabricate(&blk)
9
+ dup.tap do |klass|
10
+ klass.class_eval { include Lotus::Validations }
11
+ klass.class_eval(&blk)
12
+ end
13
+ end
14
+
15
+ # @since 0.3.1
16
+ # @api private
17
+ def lotus_nested_attributes?
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,6 +1,6 @@
1
1
  module Lotus
2
2
  module Validations
3
3
  # @since 0.1.0
4
- VERSION = '0.3.0'.freeze
4
+ VERSION = '0.3.1'.freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-23 00:00:00.000000000 Z
12
+ date: 2015-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lotus-utils
@@ -86,6 +86,7 @@ files:
86
86
  - lib/lotus/validations/coercions.rb
87
87
  - lib/lotus/validations/error.rb
88
88
  - lib/lotus/validations/errors.rb
89
+ - lib/lotus/validations/nested_attributes.rb
89
90
  - lib/lotus/validations/validation_set.rb
90
91
  - lib/lotus/validations/validator.rb
91
92
  - lib/lotus/validations/version.rb