lotus-validations 0.2.2 → 0.2.3

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: 0249c7c704d709f9579693a8f8b8faac3c1ca338
4
- data.tar.gz: e2913b58194002b58ec6da05154982e46d30efd5
3
+ metadata.gz: 5c28b2bd092a3123b3449bd57ce7aa6bf641aef7
4
+ data.tar.gz: 6def7642d5acb0049d3f9656ccb22617a7a0a97c
5
5
  SHA512:
6
- metadata.gz: e82fe5c215943d1fdfd11068c1b5639dcec94062a1e1cce5dafee5ee643e09dde7b05b826f1c32d7d9c10e33a0b618587cdae1a393c21f0361388f8162798afa
7
- data.tar.gz: b92988e71b73a27e7319acf7957e001042035ce9091c3f2a1e55d83f7f9d4afb45f6c41c9742d1ba663e4eca107977c714685e53577143b7baf5e51bc11ad4b7
6
+ metadata.gz: 721ec4289267c55b2ac39e132568568023c3a51ba3ecf1faca3627d7a1a60a351985aa0dfb778808fbfa19f3dec305fd1830e179a1f5f8d1ae4252676130a81b
7
+ data.tar.gz: 695f77817307e158789652f4a584bad04af0cf05e5f65d48068389a6db9e1153b613f827e45c25a5a3051c355eee28cc32889488988435798625a3b1f6d1a780
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Lotus::Validations
2
2
  Validations mixin for Ruby objects
3
3
 
4
+ ## v0.2.3 - 2015-01-12
5
+ ### Added
6
+ - [Luca Guidi] Compatibility with Lotus::Entity
7
+
8
+ ### Fixed
9
+ - [Luca Guidi] Ensure `.validates` usage to not raise `ArgumentError` when `:type` option is passed
10
+ - [Luca Guidi] Ensure to assign attributes when only `.validates` is used
11
+
4
12
  ## v0.2.2 - 2015-01-08
5
13
  ### Added
6
14
  - [Steve Hodgkiss] Introduced `Validations.validates`. It defines validations, for already existing attributes.
data/README.md CHANGED
@@ -471,6 +471,29 @@ signup.errors
471
471
  # }>
472
472
  ```
473
473
 
474
+ ### Lotus::Entity
475
+
476
+ Integration with [`Lotus::Entity`](https://github.com/lotus/model) is activated automatically.
477
+
478
+ ```ruby
479
+ require 'lotus/model'
480
+ require 'lotus/validations'
481
+
482
+ class Product
483
+ include Lotus::Entity
484
+ include Lotus::Validations
485
+
486
+ attribute :name, type: String, presence: true
487
+ attribute :price, type: Integer, presence: true
488
+ end
489
+
490
+ product = Product.new(name: 'Book', price: '100')
491
+ product.valid? # => true
492
+
493
+ product.name # => "Book"
494
+ product.price # => 100
495
+ ```
496
+
474
497
  ## Contributing
475
498
 
476
499
  1. Fork it ( https://github.com/lotus/lotus-validations/fork )
@@ -481,4 +504,4 @@ signup.errors
481
504
 
482
505
  ## Copyright
483
506
 
484
- Copyright 2014 Luca Guidi – Released under MIT License
507
+ Copyright 2014-2015 Luca Guidi – Released under MIT License
@@ -8,6 +8,14 @@ module Lotus
8
8
  # @since 0.2.2
9
9
  # @api private
10
10
  module AttributeDefiner
11
+ # @since 0.2.3
12
+ # @api private
13
+ LOTUS_ENTITY_CLASS_NAME = 'Lotus::Entity'.freeze
14
+
15
+ # @since 0.2.3
16
+ # @api private
17
+ LOTUS_ENTITY_ID = 'id'.freeze
18
+
11
19
  # Override Ruby's hook for modules.
12
20
  #
13
21
  # @param base [Class] the target class
@@ -18,6 +26,19 @@ module Lotus
18
26
  # @see http://www.ruby-doc.org/core/Module.html#method-i-included
19
27
  def self.included(base)
20
28
  base.extend ClassMethods
29
+ base.extend EntityAttributeDefiner if lotus_entity?(base)
30
+ end
31
+
32
+ # Decide if enable the support for `Lotus::Entity`.
33
+ #
34
+ # @param base [Class]
35
+ #
36
+ # @since 0.2.3
37
+ # @api private
38
+ def self.lotus_entity?(base)
39
+ base.included_modules.any? do |m|
40
+ m.to_s == LOTUS_ENTITY_CLASS_NAME
41
+ end
21
42
  end
22
43
 
23
44
  # @since 0.2.2
@@ -237,8 +258,14 @@ module Lotus
237
258
  validates(name, options)
238
259
  end
239
260
 
261
+ # Set of user defined attributes
262
+ #
263
+ # @return [Array<String>]
264
+ #
265
+ # @since 0.2.2
266
+ # @api private
240
267
  def defined_attributes
241
- @defined_attributes ||= Set.new
268
+ @defined_attributes ||= Set.new(super)
242
269
  end
243
270
 
244
271
  private
@@ -246,7 +273,8 @@ module Lotus
246
273
  # @since 0.2.2
247
274
  # @api private
248
275
  def define_attribute(name, options)
249
- type = options.delete(:type)
276
+ type = options.fetch(:type) { nil }
277
+
250
278
  define_accessor(name, type)
251
279
  defined_attributes.add(name.to_s)
252
280
 
@@ -294,6 +322,73 @@ module Lotus
294
322
  end
295
323
  end
296
324
 
325
+ # Support for `Lotus::Entity`
326
+ #
327
+ # @since 0.2.3
328
+ # @api private
329
+ #
330
+ # @example
331
+ # require 'lotus/model'
332
+ # require 'lotus/validations'
333
+ #
334
+ # class Product
335
+ # include Lotus::Entity
336
+ # include Lotus::Validations
337
+ #
338
+ # attribute :name, type: String, presence: true
339
+ # attribute :price, type: Integer, presence: true
340
+ # end
341
+ #
342
+ # product = Product.new(name: 'Computer', price: '100')
343
+ #
344
+ # product.name # => "Computer"
345
+ # product.price # => 100
346
+ # product.valid? # => true
347
+ module EntityAttributeDefiner
348
+ # Override for Module#extend
349
+ #
350
+ # @since 0.2.3
351
+ # @api private
352
+ #
353
+ # @see http://ruby-doc.org/Module.html#method-i-extended
354
+ def self.extended(base)
355
+ base.class_eval do
356
+ include EntityAttributeDefiner::InstanceMethods
357
+ end
358
+ end
359
+
360
+ # @since 0.2.3
361
+ # @api private
362
+ #
363
+ # @see Lotus::Validations::AttributeDefiner#attribute
364
+ def attribute(name, options = {})
365
+ super
366
+ attributes name
367
+ end
368
+
369
+ # @since 0.2.3
370
+ # @api private
371
+ #
372
+ # @see Lotus::Validations::ClassMethods#validates
373
+ def validates(name, options = {})
374
+ super
375
+ define_attribute(name, options)
376
+ end
377
+
378
+ # @since 0.2.3
379
+ # @api private
380
+ module InstanceMethods
381
+ private
382
+ # @since 0.2.3
383
+ # @api private
384
+ #
385
+ # @see Lotus::Validations::AttributeDefiner#assign_attribute?
386
+ def assign_attribute?(attr)
387
+ super || attr.to_s == LOTUS_ENTITY_ID
388
+ end
389
+ end
390
+ end
391
+
297
392
  # Create a new instance with the given attributes
298
393
  #
299
394
  # @param attributes [#to_h] an Hash like object which contains the
@@ -335,7 +430,7 @@ module Lotus
335
430
  # signup = Signup.new(params)
336
431
  #
337
432
  # signup.name # => "Luca"
338
- def initialize(attributes)
433
+ def initialize(attributes = {})
339
434
  @attributes ||= Utils::Attributes.new
340
435
 
341
436
  attributes.to_h.each do |key, value|
@@ -16,7 +16,8 @@ module Lotus
16
16
  :inclusion,
17
17
  :exclusion,
18
18
  :confirmation,
19
- :size
19
+ :size,
20
+ :type
20
21
  ].freeze
21
22
 
22
23
  # @since 0.2.2
@@ -45,6 +46,12 @@ module Lotus
45
46
  @validations.each_key(&blk)
46
47
  end
47
48
 
49
+ # @since 0.2.3
50
+ # @api private
51
+ def names
52
+ @validations.keys
53
+ end
54
+
48
55
  private
49
56
  # Checks at the loading time if the user defined validations are recognized
50
57
  #
@@ -1,6 +1,6 @@
1
1
  module Lotus
2
2
  module Validations
3
3
  # @since 0.1.0
4
- VERSION = '0.2.2'.freeze
4
+ VERSION = '0.2.3'.freeze
5
5
  end
6
6
  end
@@ -126,6 +126,16 @@ module Lotus
126
126
  @validations ||= ValidationSet.new
127
127
  end
128
128
 
129
+ # Set of user defined attributes
130
+ #
131
+ # @return [Array<String>]
132
+ #
133
+ # @since 0.2.3
134
+ # @api private
135
+ def defined_attributes
136
+ validations.names.map(&:to_s)
137
+ end
138
+
129
139
  private
130
140
 
131
141
  # Transfers attributes to a base class
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.2.2
4
+ version: 0.2.3
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-01-08 00:00:00.000000000 Z
12
+ date: 2015-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lotus-utils