type_validator 0.2.0 → 0.3.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: '0219719c84dc08533f5acc5f67021eb3a0d997c6babb5416858040ef38676664'
4
- data.tar.gz: d5547e5c033dcfcf088d792a58909d16d954738429aaae5e8defc94189cebbcd
3
+ metadata.gz: 5d0fdadd3e8e604772c61edee9801aa9e91602de7ae75a1f98518dc943a815bf
4
+ data.tar.gz: ecff5f10b460a766f2a76dce3de6d95faa1505510f3e8e3cb8e574507ab18f65
5
5
  SHA512:
6
- metadata.gz: 40b7e79d54d1e52fe90029df692d0ade0fc087f589e24ba1a67fd7d02858e1d319675a123e051ef2c32fb66c4556e698e0b79ec62f804863a895aa84976e3ded
7
- data.tar.gz: faa022b0c23d701e94297d35e64d5580d3855edc86cde215bed126c0a3b3bd6aacc3f3c975854addd1305b704d1faf8d4653f393c81648174ec710742c5954d0
6
+ metadata.gz: 495858fff25f48ec3a9fab33a19e3ea0f11f8f9b461b4552620e8c74161cbb11b6f50641ff3b964ce21dd49826da670c07c8c8086a0fca743904b77f96fb5da9
7
+ data.tar.gz: 34e7a07708502948ab947bcaa2ec8e06a98ccc79a2762773527f995fdaa382d91c8f1575a9ef1002068f15a6ff862c87bce77deffe8879c14d2326e8ee05ef13
@@ -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 `:kind_of`'.freeze
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?(record, attribute, value, options)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -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?(record, attribute, value, options)
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.2.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