type_validator 0.3.0 → 0.4.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: 5d0fdadd3e8e604772c61edee9801aa9e91602de7ae75a1f98518dc943a815bf
4
- data.tar.gz: ecff5f10b460a766f2a76dce3de6d95faa1505510f3e8e3cb8e574507ab18f65
3
+ metadata.gz: e568fbe405d04fc6966703bb39be3314151caacb82941c81ea88049b99fb40e1
4
+ data.tar.gz: f8d9f3bad6af97d99192114b9278237e89af8876fa64c236328fd3f7cacdfe72
5
5
  SHA512:
6
- metadata.gz: 495858fff25f48ec3a9fab33a19e3ea0f11f8f9b461b4552620e8c74161cbb11b6f50641ff3b964ce21dd49826da670c07c8c8086a0fca743904b77f96fb5da9
7
- data.tar.gz: 34e7a07708502948ab947bcaa2ec8e06a98ccc79a2762773527f995fdaa382d91c8f1575a9ef1002068f15a6ff862c87bce77deffe8879c14d2326e8ee05ef13
6
+ metadata.gz: 9b7099d9533d4ae299c1a416ff3273e205af27dc12e391836159236956e91bb0a93db9c6a59ad322588c3d41a0d574ce4f8ab107cdf9211bc226ae0b19164c4e
7
+ data.tar.gz: f9a78a94ecde7e23915fc5ffc636eaa5d0683d3ecfaf2d7efa26668365c9fbf9bc2ae45080fa0e4462c42ea4479d67e05ba644ee06afd22f5e6367fd5ae8045a
@@ -3,7 +3,7 @@
3
3
  require 'active_model'
4
4
 
5
5
  class TypeValidator
6
- class KindOf
6
+ class ByKindOf
7
7
  def self.invalid?(value, options)
8
8
  types = Array(options[:is_a] || options[:kind_of])
9
9
  allow_nil = options[:allow_nil]
@@ -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 RespondTo
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
 
@@ -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` or :respond_to'.freeze
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}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -16,12 +16,14 @@ class TypeValidator < ActiveModel::EachValidator
16
16
  end
17
17
 
18
18
  def fetch_strategy(options)
19
- return KindOf if options.key?(:is_a) || options.key?(:kind_of)
20
- RespondTo if options.key?(:respond_to)
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.3.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