type_validator 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/type_validator/{kind_of.rb → by_kind_of.rb} +1 -1
- data/lib/type_validator/by_klass.rb +22 -0
- data/lib/type_validator/{respond_to.rb → by_respond_to.rb} +2 -3
- data/lib/type_validator/error.rb +1 -1
- data/lib/type_validator/version.rb +1 -1
- data/lib/type_validator.rb +7 -5
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e568fbe405d04fc6966703bb39be3314151caacb82941c81ea88049b99fb40e1
|
4
|
+
data.tar.gz: f8d9f3bad6af97d99192114b9278237e89af8876fa64c236328fd3f7cacdfe72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b7099d9533d4ae299c1a416ff3273e205af27dc12e391836159236956e91bb0a93db9c6a59ad322588c3d41a0d574ce4f8ab107cdf9211bc226ae0b19164c4e
|
7
|
+
data.tar.gz: f9a78a94ecde7e23915fc5ffc636eaa5d0683d3ecfaf2d7efa26668365c9fbf9bc2ae45080fa0e4462c42ea4479d67e05ba644ee06afd22f5e6367fd5ae8045a
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class TypeValidator
|
6
|
+
class ByKlass
|
7
|
+
def self.invalid?(value, options)
|
8
|
+
klass, allow_nil = options[:klass], options[:allow_nil]
|
9
|
+
|
10
|
+
require_a_class(value)
|
11
|
+
require_a_class(klass)
|
12
|
+
|
13
|
+
return if (allow_nil && value.nil?) || (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
|
@@ -3,10 +3,9 @@
|
|
3
3
|
require 'active_model'
|
4
4
|
|
5
5
|
class TypeValidator
|
6
|
-
class
|
6
|
+
class ByRespondTo
|
7
7
|
def self.invalid?(value, options)
|
8
|
-
method_name = options[:respond_to]
|
9
|
-
allow_nil = options[:allow_nil]
|
8
|
+
method_name, allow_nil = options[:respond_to], options[:allow_nil]
|
10
9
|
|
11
10
|
return if (allow_nil && value.nil?) || value.respond_to?(method_name)
|
12
11
|
|
data/lib/type_validator/error.rb
CHANGED
@@ -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
|
8
|
+
OPTIONS = 'Options to define one: `:is_a`/`:kind_of`, :respond_to or :klass'.freeze
|
9
9
|
|
10
10
|
def initialize(attribute)
|
11
11
|
super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
|
data/lib/type_validator.rb
CHANGED
@@ -16,12 +16,14 @@ class TypeValidator < ActiveModel::EachValidator
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def fetch_strategy(options)
|
19
|
-
return
|
20
|
-
|
19
|
+
return ByKindOf if options.key?(:is_a) || options.key?(:kind_of)
|
20
|
+
return ByRespondTo if options.key?(:respond_to)
|
21
|
+
return ByKlass if options.key?(:klass)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
|
-
require 'type_validator/error'
|
25
|
-
require 'type_validator/kind_of'
|
26
|
-
require 'type_validator/respond_to'
|
27
25
|
require 'type_validator/version'
|
26
|
+
require 'type_validator/error'
|
27
|
+
require 'type_validator/by_klass'
|
28
|
+
require 'type_validator/by_kind_of'
|
29
|
+
require 'type_validator/by_respond_to'
|
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -84,9 +84,10 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
86
|
- lib/type_validator.rb
|
87
|
+
- lib/type_validator/by_kind_of.rb
|
88
|
+
- lib/type_validator/by_klass.rb
|
89
|
+
- lib/type_validator/by_respond_to.rb
|
87
90
|
- lib/type_validator/error.rb
|
88
|
-
- lib/type_validator/kind_of.rb
|
89
|
-
- lib/type_validator/respond_to.rb
|
90
91
|
- lib/type_validator/version.rb
|
91
92
|
- test.sh
|
92
93
|
- type_validator.gemspec
|