type_validator 0.2.0 → 0.3.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/error.rb +1 -1
- data/lib/type_validator/kind_of.rb +1 -2
- data/lib/type_validator/respond_to.rb +16 -0
- data/lib/type_validator/version.rb +1 -1
- data/lib/type_validator.rb +4 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d0fdadd3e8e604772c61edee9801aa9e91602de7ae75a1f98518dc943a815bf
|
4
|
+
data.tar.gz: ecff5f10b460a766f2a76dce3de6d95faa1505510f3e8e3cb8e574507ab18f65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 495858fff25f48ec3a9fab33a19e3ea0f11f8f9b461b4552620e8c74161cbb11b6f50641ff3b964ce21dd49826da670c07c8c8086a0fca743904b77f96fb5da9
|
7
|
+
data.tar.gz: 34e7a07708502948ab947bcaa2ec8e06a98ccc79a2762773527f995fdaa382d91c8f1575a9ef1002068f15a6ff862c87bce77deffe8879c14d2326e8ee05ef13
|
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` or
|
8
|
+
OPTIONS = 'Options to define one: `:is_a`/`:kind_of` or :respond_to'.freeze
|
9
9
|
|
10
10
|
def initialize(attribute)
|
11
11
|
super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
|
@@ -1,11 +1,10 @@
|
|
1
|
-
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require 'active_model'
|
5
4
|
|
6
5
|
class TypeValidator
|
7
6
|
class KindOf
|
8
|
-
def self.invalid?(
|
7
|
+
def self.invalid?(value, options)
|
9
8
|
types = Array(options[:is_a] || options[:kind_of])
|
10
9
|
allow_nil = options[:allow_nil]
|
11
10
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class TypeValidator
|
6
|
+
class RespondTo
|
7
|
+
def self.invalid?(value, options)
|
8
|
+
method_name = options[:respond_to]
|
9
|
+
allow_nil = options[:allow_nil]
|
10
|
+
|
11
|
+
return if (allow_nil && value.nil?) || value.respond_to?(method_name)
|
12
|
+
|
13
|
+
"must respond to the method `#{method_name}`"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/type_validator.rb
CHANGED
@@ -8,7 +8,7 @@ class TypeValidator < ActiveModel::EachValidator
|
|
8
8
|
|
9
9
|
raise Error::InvalidDefinition.new(attribute) unless strategy
|
10
10
|
|
11
|
-
return unless error = strategy.invalid?(
|
11
|
+
return unless error = strategy.invalid?(value, options)
|
12
12
|
|
13
13
|
raise TypeError, "#{attribute} #{error}" if options[:strict]
|
14
14
|
|
@@ -16,10 +16,12 @@ class TypeValidator < ActiveModel::EachValidator
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def fetch_strategy(options)
|
19
|
-
KindOf if options.key?(:is_a) || options.key?(:kind_of)
|
19
|
+
return KindOf if options.key?(:is_a) || options.key?(:kind_of)
|
20
|
+
RespondTo if options.key?(:respond_to)
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
require 'type_validator/error'
|
24
25
|
require 'type_validator/kind_of'
|
26
|
+
require 'type_validator/respond_to'
|
25
27
|
require 'type_validator/version'
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- lib/type_validator.rb
|
87
87
|
- lib/type_validator/error.rb
|
88
88
|
- lib/type_validator/kind_of.rb
|
89
|
+
- lib/type_validator/respond_to.rb
|
89
90
|
- lib/type_validator/version.rb
|
90
91
|
- test.sh
|
91
92
|
- type_validator.gemspec
|