type_validator 0.8.0 → 0.9.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: 96d8db1bdc2cb03f7eddc41fb66025b36557fc83014845868a847b118f521d14
4
- data.tar.gz: f1d2f58f64b062855ccb490332064b7b9a0f82162b1e0246d48eaa534a305961
3
+ metadata.gz: 98d8d425b5e4434e03c1f6bbe6dc6e2c5597faa6fd9479a28bfcf689c4e4797e
4
+ data.tar.gz: 7131ce004a821ad619a524cb13076c8f5011e5939b9990261a7eb6b617b62f69
5
5
  SHA512:
6
- metadata.gz: 1f6812a3e4dda60eb1157b42aa5553b2b1b4edce6df26b261d85fecc27be4dbba1fd334dd9628a4a33a97f318ddbdffb48f9fd8ad35160b98087a3087a103661
7
- data.tar.gz: 4cbd56e7a43b563f9482176e24060842386d58d59d893c918042fe4cbc0df1417b8cc88317176e0abecb6fcf7b8878975dc5a93962fb83d1f403e19b3a15a6e7
6
+ metadata.gz: a7cd464b044de2061659dea89ee3f2b832842908e08505d7335038f7a124e3393dca4cb4c3175df9d694ff6337e1ca25704c0937a3d5df8baadb3fa47d569c4c
7
+ data.tar.gz: 97e84add9774959d8dea83eb9156d5f62c8a64d3b053709cae35315f40832b2f5713fe59255ed51c1f3191f0e85be6d101cdc57fd5c3633093581b07db9eee86
@@ -4,12 +4,9 @@ require 'active_model'
4
4
 
5
5
  class TypeValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, value)
7
- strategy = fetch_strategy(options)
8
-
9
- raise Error::InvalidDefinition.new(attribute) unless strategy
10
-
11
7
  return if options[:allow_nil] && value.nil?
12
- return unless error = strategy.invalid?(value, options)
8
+
9
+ return unless error = validate_type_of(attribute, value)
13
10
 
14
11
  raise TypeError, "#{attribute} #{error}" if options[:strict]
15
12
 
@@ -18,19 +15,60 @@ class TypeValidator < ActiveModel::EachValidator
18
15
 
19
16
  private
20
17
 
21
- def fetch_strategy(options)
22
- return ByKindOf if options.key?(:is_a) || options.key?(:kind_of)
23
- return ByRespondTo if options.key?(:respond_to)
24
- return ByKlass if options.key?(:klass)
25
- return ByArrayOf if options.key?(:array_of)
26
- return ByArrayWith if options.key?(:array_with)
18
+ def validate_type_of(attribute, value)
19
+ if expected = options[:is_a] ; return validate_kind_of(value, expected) ; end
20
+ if expected = options[:kind_of] ; return validate_kind_of(value, expected) ; end
21
+ if expected = options[:respond_to]; return validate_respond_to(value, expected); end
22
+ if expected = options[:klass] ; return validate_klass(value, expected) ; end
23
+ if expected = options[:array_of] ; return validate_array_of(value, expected) ; end
24
+ if expected = options[:array_with]; return validate_array_with(value, expected); end
25
+
26
+ raise Error::InvalidDefinition.new(attribute)
27
+ end
28
+
29
+ def validate_kind_of(value, expected)
30
+ types = Array(expected)
31
+
32
+ return if types.any? { |type| value.is_a?(type) }
33
+
34
+ "must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
35
+ end
36
+
37
+ def validate_respond_to(value, method_name)
38
+ return if value.respond_to?(method_name)
39
+
40
+ "must respond to the method `#{method_name}`"
41
+ end
42
+
43
+ def validate_klass(value, klass)
44
+ require_a_class(value)
45
+ require_a_class(klass)
46
+
47
+ return if value == klass || value < klass
48
+
49
+ "must be the or a subclass of `#{klass.name}`"
50
+ end
51
+
52
+ def require_a_class(arg)
53
+ raise ArgumentError, "#{arg} must be a class" unless arg.is_a?(Class)
54
+ end
55
+
56
+ def validate_array_of(value, expected)
57
+ types = Array(expected)
58
+
59
+ return if value.is_a?(Array) && !value.empty? && value.all? { |value| types.any? { |type| value.is_a?(type) } }
60
+
61
+ "must be an array of: #{types.map { |klass| klass.name }.join(', ')}"
62
+ end
63
+
64
+ def validate_array_with(value, expected)
65
+ raise ArgumentError, "#{expected} must be an array" unless expected.is_a?(Array)
66
+
67
+ return if value.is_a?(Array) && !value.empty? && (value - expected).empty?
68
+
69
+ "must be an array with: #{expected.join(', ')}"
27
70
  end
28
71
  end
29
72
 
30
73
  require 'type_validator/version'
31
74
  require 'type_validator/error'
32
- require 'type_validator/by_klass'
33
- require 'type_validator/by_kind_of'
34
- require 'type_validator/by_array_of'
35
- require 'type_validator/by_array_with'
36
- require 'type_validator/by_respond_to'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-18 00:00:00.000000000 Z
11
+ date: 2019-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -85,11 +85,6 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - lib/type_validator.rb
88
- - lib/type_validator/by_array_of.rb
89
- - lib/type_validator/by_array_with.rb
90
- - lib/type_validator/by_kind_of.rb
91
- - lib/type_validator/by_klass.rb
92
- - lib/type_validator/by_respond_to.rb
93
88
  - lib/type_validator/error.rb
94
89
  - lib/type_validator/version.rb
95
90
  - test.sh
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_model'
4
-
5
- class TypeValidator
6
- module ByArrayOf
7
- def self.invalid?(value, options)
8
- types = Array(options[:array_of])
9
-
10
- return if value.is_a?(Array) && !value.empty? && value.all? { |value| types.any? { |type| value.is_a?(type) } }
11
-
12
- "must be an array of: #{types.map { |klass| klass.name }.join(', ')}"
13
- end
14
- end
15
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_model'
4
-
5
- class TypeValidator
6
- module ByArrayWith
7
- def self.invalid?(value, options)
8
- expected = options[:array_with]
9
-
10
- raise ArgumentError, "#{expected} must be an array" unless expected.is_a?(Array)
11
-
12
- return if value.is_a?(Array) && !value.empty? && (value - expected).empty?
13
-
14
- "must be an array with: #{expected.join(', ')}"
15
- end
16
- end
17
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_model'
4
-
5
- class TypeValidator
6
- module ByKindOf
7
- def self.invalid?(value, options)
8
- types = Array(options[:is_a] || options[:kind_of])
9
-
10
- return if types.any? { |type| value.is_a?(type) }
11
-
12
- "must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
13
- end
14
- end
15
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_model'
4
-
5
- class TypeValidator
6
- module ByKlass
7
- def self.invalid?(value, options)
8
- klass = options[:klass]
9
-
10
- require_a_class(value)
11
- require_a_class(klass)
12
-
13
- return if value == klass || value < klass
14
-
15
- "must be the or a subclass of `#{klass.name}`"
16
- end
17
-
18
- def self.require_a_class(arg)
19
- raise ArgumentError, "#{arg} must be a class" unless arg.is_a?(Class)
20
- end
21
- end
22
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_model'
4
-
5
- class TypeValidator
6
- module ByRespondTo
7
- def self.invalid?(value, options)
8
- method_name = options[:respond_to]
9
-
10
- return if value.respond_to?(method_name)
11
-
12
- "must respond to the method `#{method_name}`"
13
- end
14
- end
15
- end