type_validator 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e568fbe405d04fc6966703bb39be3314151caacb82941c81ea88049b99fb40e1
4
- data.tar.gz: f8d9f3bad6af97d99192114b9278237e89af8876fa64c236328fd3f7cacdfe72
3
+ metadata.gz: ece9a123e1c83f525af3096c563de8a1efca129eb5e4a492c871203ea16aadcb
4
+ data.tar.gz: 10687d371e4fbdca39871dda7ef7c9511b29551e2c09b082f6199a6f82dd0293
5
5
  SHA512:
6
- metadata.gz: 9b7099d9533d4ae299c1a416ff3273e205af27dc12e391836159236956e91bb0a93db9c6a59ad322588c3d41a0d574ce4f8ab107cdf9211bc226ae0b19164c4e
7
- data.tar.gz: f9a78a94ecde7e23915fc5ffc636eaa5d0683d3ecfaf2d7efa26668365c9fbf9bc2ae45080fa0e4462c42ea4479d67e05ba644ee06afd22f5e6367fd5ae8045a
6
+ metadata.gz: a912cd368560070b58ccf17c71fabd64a2c1bd10e14847d5c667414e39749f7a9abce5d81fd7e4a54956dea1c91a208a03fe08d4c78ddd5c8c8e66b77ab536b9
7
+ data.tar.gz: 4b4748578919138334e9a6e3c01c9976cf186b631808643a3c39969b5e018a9660bd300197f9f4db95a9cbde2648b78a66abc56a3fc14f260cc17a85ee391e75
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ class TypeValidator
6
+ class ByArrayOf
7
+ def self.invalid?(value, options)
8
+ types, allow_nil = Array(options[:array_of]), options[:allow_nil]
9
+
10
+ return if (allow_nil && value.nil?)
11
+ return if value.is_a?(Array) && !value.empty? && value.all? { |value| types.any? { |type| value.is_a?(type) } }
12
+
13
+ "must be an array of: #{types.map { |klass| klass.name }.join(', ')}"
14
+ end
15
+ end
16
+ end
@@ -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 or :klass'.freeze
8
+ OPTIONS = 'Options to define one: `:is_a`/`:kind_of`, :respond_to, :klass or :array_of'.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.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -19,6 +19,7 @@ class TypeValidator < ActiveModel::EachValidator
19
19
  return ByKindOf if options.key?(:is_a) || options.key?(:kind_of)
20
20
  return ByRespondTo if options.key?(:respond_to)
21
21
  return ByKlass if options.key?(:klass)
22
+ return ByArrayOf if options.key?(:array_of)
22
23
  end
23
24
  end
24
25
 
@@ -26,4 +27,5 @@ require 'type_validator/version'
26
27
  require 'type_validator/error'
27
28
  require 'type_validator/by_klass'
28
29
  require 'type_validator/by_kind_of'
30
+ require 'type_validator/by_array_of'
29
31
  require 'type_validator/by_respond_to'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-17 00:00:00.000000000 Z
11
+ date: 2019-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -84,6 +84,7 @@ files:
84
84
  - bin/console
85
85
  - bin/setup
86
86
  - lib/type_validator.rb
87
+ - lib/type_validator/by_array_of.rb
87
88
  - lib/type_validator/by_kind_of.rb
88
89
  - lib/type_validator/by_klass.rb
89
90
  - lib/type_validator/by_respond_to.rb