type_validator 0.9.0 → 0.10.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: 98d8d425b5e4434e03c1f6bbe6dc6e2c5597faa6fd9479a28bfcf689c4e4797e
4
- data.tar.gz: 7131ce004a821ad619a524cb13076c8f5011e5939b9990261a7eb6b617b62f69
3
+ metadata.gz: c6d336fb78cd054bc714f18871e7fdf708526d29beb9c7d19ec6156f20e627e7
4
+ data.tar.gz: 2ae7196178b31af7ee65bdf7c545869a84c2895a6ca6e7064d486b84fd5817fb
5
5
  SHA512:
6
- metadata.gz: a7cd464b044de2061659dea89ee3f2b832842908e08505d7335038f7a124e3393dca4cb4c3175df9d694ff6337e1ca25704c0937a3d5df8baadb3fa47d569c4c
7
- data.tar.gz: 97e84add9774959d8dea83eb9156d5f62c8a64d3b053709cae35315f40832b2f5713fe59255ed51c1f3191f0e85be6d101cdc57fd5c3633093581b07db9eee86
6
+ metadata.gz: e867c62076ccf8355855e5b4e92753c6f7cc2121de1eb66a2c2474fcd650272072b1429c37428336efe0c579c35ce302c9224a8fec302bf763de4c1e14c3fdc2
7
+ data.tar.gz: 396af9fbb4eef0a2a2d0c51a43b23c6eedfe86cdf2a9ccc287d53e93ed3cedae51a0dee3711f5e6d55738cab7d26155b22bdee4938824caacba8a24e9dc69d0b
data/README.md CHANGED
@@ -25,18 +25,34 @@ Or install it yourself as:
25
25
 
26
26
  Use one or all of the type validations into your models/classes:
27
27
 
28
+ **[Object#instance_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-instance_of-3F)**
29
+
30
+ ```ruby
31
+ validates :name, type: { instance_of: String }
32
+
33
+ # or use an array to verify if the attribute
34
+ # is an instance of one of the classes
35
+
36
+ validates :name, type: { instance_of: [String, Symbol] }
37
+ ```
38
+
39
+ **[Object#kind_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-kind_of-3F)**
40
+
28
41
  ```ruby
29
42
  validates :name, type: { is_a: String }
30
43
  # or
31
44
  validates :name, type: { kind_of: String }
32
45
 
33
- # Use an array to verify if the attribute is an instance of one of the classes
46
+ # Use an array to verify if the attribute
47
+ # is an instance of one of the classes
34
48
 
35
49
  validates :status, type: { is_a: [String, Symbol]}
36
50
  # or
37
51
  validates :status, type: { kind_of: [String, Symbol]}
38
52
  ```
39
53
 
54
+ **[Object#respond_to?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-respond_to-3F)**
55
+
40
56
  ```ruby
41
57
  validates :handler, type: { respond_to: :call }
42
58
  ```
@@ -50,13 +66,15 @@ validates :handler, type: { klass: Handler }
50
66
  ```ruby
51
67
  validates :account_types, type: { array_of: String }
52
68
 
53
- # or use an array to verify if the attribute is an instance of one of the classes
69
+ # or use an array to verify if the attribute
70
+ # is an instance of one of the classes
54
71
 
55
72
  validates :account_types, type: { array_of: [String, Symbol] }
56
73
  ```
57
74
 
58
75
  ```ruby
59
- # Verifies if the attribute value is an array with some or all the expected values.
76
+ # Verifies if the attribute value
77
+ # is an array with some or all the expected values.
60
78
 
61
79
  validates :account_types, type: { array_with: ['foo', 'bar'] }
62
80
  ```
@@ -5,7 +5,7 @@ require 'active_model'
5
5
  class TypeValidator
6
6
  module Error
7
7
  class InvalidDefinition < ArgumentError
8
- OPTIONS = 'Options to define one: `:is_a`/`:kind_of`, :respond_to, :klass, :array_of or :array_with'.freeze
8
+ OPTIONS = 'Options to define one: :instance_of, :is_a/:kind_of, :respond_to, :klass, :array_of or :array_with'.freeze
9
9
 
10
10
  def initialize(attribute)
11
11
  super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.9.0'
4
+ VERSION = '0.10.0'
5
5
  end
@@ -16,16 +16,25 @@ class TypeValidator < ActiveModel::EachValidator
16
16
  private
17
17
 
18
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
19
+ if expected = options[:instance_of]; return validate_instance_of(value, expected); end
20
+ if expected = options[:is_a] ; return validate_kind_of(value, expected) ; end
21
+ if expected = options[:kind_of] ; return validate_kind_of(value, expected) ; end
22
+ if expected = options[:klass] ; return validate_klass(value, expected) ; end
23
+ if expected = options[:respond_to] ; return validate_respond_to(value, expected) ; end
24
+ if expected = options[:array_of] ; return validate_array_of(value, expected) ; end
25
+ if expected = options[:array_with] ; return validate_array_with(value, expected) ; end
25
26
 
26
27
  raise Error::InvalidDefinition.new(attribute)
27
28
  end
28
29
 
30
+ def validate_instance_of(value, expected)
31
+ types = Array(expected)
32
+
33
+ return if types.any? { |type| value.instance_of?(type) }
34
+
35
+ "must be an instance of: #{types.map { |klass| klass.name }.join(', ')}"
36
+ end
37
+
29
38
  def validate_kind_of(value, expected)
30
39
  types = Array(expected)
31
40
 
@@ -34,10 +43,13 @@ class TypeValidator < ActiveModel::EachValidator
34
43
  "must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
35
44
  end
36
45
 
37
- def validate_respond_to(value, method_name)
38
- return if value.respond_to?(method_name)
46
+ def validate_klass(value, klass)
47
+ require_a_class(value)
48
+ require_a_class(klass)
39
49
 
40
- "must respond to the method `#{method_name}`"
50
+ return if value == klass || value < klass
51
+
52
+ "must be the or a subclass of `#{klass.name}`"
41
53
  end
42
54
 
43
55
  def validate_klass(value, klass)
@@ -53,6 +65,12 @@ class TypeValidator < ActiveModel::EachValidator
53
65
  raise ArgumentError, "#{arg} must be a class" unless arg.is_a?(Class)
54
66
  end
55
67
 
68
+ def validate_respond_to(value, method_name)
69
+ return if value.respond_to?(method_name)
70
+
71
+ "must respond to the method `#{method_name}`"
72
+ end
73
+
56
74
  def validate_array_of(value, expected)
57
75
  types = Array(expected)
58
76
 
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
6
6
  spec.name = 'type_validator'
7
7
  spec.version = File.readlines(File.join('.', 'lib', 'type_validator', 'version.rb'))
8
8
  .find { |line| line =~ /VERSION/ }
9
- .strip[/(\d\.?){3}/]
9
+ .strip[/(\d\d?\.?){3}/]
10
10
  spec.authors = ['Rodrigo Serradura']
11
11
  spec.email = ['rodrigo.serradura@gmail.com']
12
12
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura