soulless 1.1.1 → 1.2.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
  SHA1:
3
- metadata.gz: 63319a4d6809bc1e9c9306a05bc5e4d7afa76b2c
4
- data.tar.gz: 11e52c992cf8d311f84570d2393036cabed70bd1
3
+ metadata.gz: b1989bab6516b12bd424b892e317bf99c12df4ee
4
+ data.tar.gz: 33a9ae2c3165ee947351ba3a68ce539926bebb4c
5
5
  SHA512:
6
- metadata.gz: 0c16b1e626087651a9d61aa18af101019c2f7caf18020edd77fb1ac0a1f2fe2e06d524a625ec75fe012e2abdde56d489cd854fb1aa106e59d75dc23d35244431
7
- data.tar.gz: 8d8b9cf8a767b82cc907d39c799c77ecce304d281af55fd75056294c7ab9dba46fac9b305f087b0a1661c4fdc630cd1b812490d950bf0a4bab5c180e3f650a4c
6
+ metadata.gz: 7bbdfa08258b566f77da8fb3a772db5485bd920fa0f5ea6fb05513bfc686f3ade294ea8bf8c97eddac419c6aafb5fb20ea7fa57879584e7f0d9d68c82990868a
7
+ data.tar.gz: 8758ca6e635e814fe7542ae675525452dd7c7c3a9221b8b660890c0fa382dbabf9139ec858e3a97b283c84310c397351fb6f8cb0d91c5dccf6746b512f848cad
@@ -34,5 +34,9 @@ module Soulless
34
34
  def persisted?
35
35
  false
36
36
  end
37
+
38
+ def ==(o)
39
+ self.to_h == o.to_h
40
+ end
37
41
  end
38
42
  end
@@ -0,0 +1,45 @@
1
+ module Soulless
2
+ module Validations
3
+ class UsingValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ check_options
6
+ return if should_not_continue?(value)
7
+
8
+ if value.is_a?(Array)
9
+ value.each { |v| perform(record, attribute, v) }
10
+ else
11
+ perform(record, attribute, value)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def check_options
18
+ fail(ArgumentError, 'The `:model` option is required for the `using` validator') unless options[:model]
19
+ fail(ArgumentError, 'The `:model` option should contain a class that responds to `valid?` for the `using` validator') unless options[:model].respond_to?(:new) && options[:model].method_defined?(:valid?)
20
+ end
21
+
22
+ def should_not_continue?(value)
23
+ (options[:allow_nil] && value.nil?) ||
24
+ (options[:allow_empty] && value.empty?)
25
+ end
26
+
27
+ def perform(record, attribute, value)
28
+ model = options[:model].new(value)
29
+
30
+ return if model.valid?
31
+
32
+ reference_value = model[options[:reference]] if options[:reference]
33
+
34
+ if !reference_value.blank?
35
+ errors = {}
36
+ errors[reference_value] = model.errors
37
+
38
+ record.errors[attribute] << errors
39
+ else
40
+ record.errors[attribute] << model.errors
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Soulless
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soulless
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Smith
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -113,6 +113,7 @@ files:
113
113
  - lib/soulless/serialization.rb
114
114
  - lib/soulless/validations.rb
115
115
  - lib/soulless/validations/uniqueness_validator.rb
116
+ - lib/soulless/validations/using_validator.rb
116
117
  - lib/soulless/version.rb
117
118
  - soulless.gemspec
118
119
  homepage: https://github.com/anthonator/soulless