type_validator 0.1.0 → 0.2.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: 6df3ff4224d400cec89fbe541cf3a90f0002869218b593f83f1545a35c83644a
4
- data.tar.gz: 5e3d0d583f647332a92b5dd9c5475f7dc4af7d550bf542b62a3a74e523fca7ca
3
+ metadata.gz: '0219719c84dc08533f5acc5f67021eb3a0d997c6babb5416858040ef38676664'
4
+ data.tar.gz: d5547e5c033dcfcf088d792a58909d16d954738429aaae5e8defc94189cebbcd
5
5
  SHA512:
6
- metadata.gz: 62ce5550ce7fb0aaed5ae4e291a92401bfe9bbb14539b8dfb58aeb473ef0332a7af8ad9e1efe6be9b4bfc8948d41b112c7428f8da08365b09a033209fe0814d2
7
- data.tar.gz: 411aa1da14f27a316ba42e257ce15095c88c8c020ba2e3ab79fef5225d5af60c3fa7fc140ee8c80cadae54921f95d30492132ae96ced88ea9495d1f9ccedb7c5
6
+ metadata.gz: 40b7e79d54d1e52fe90029df692d0ade0fc087f589e24ba1a67fd7d02858e1d319675a123e051ef2c32fb66c4556e698e0b79ec62f804863a895aa84976e3ded
7
+ data.tar.gz: faa022b0c23d701e94297d35e64d5580d3855edc86cde215bed126c0a3b3bd6aacc3f3c975854addd1305b704d1faf8d4653f393c81648174ec710742c5954d0
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ class TypeValidator
6
+ module Error
7
+ class InvalidDefinition < ArgumentError
8
+ OPTIONS = 'Options to define one: `:is_a` or `:kind_of`'.freeze
9
+
10
+ def initialize(attribute)
11
+ super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
12
+ end
13
+
14
+ private_constant :OPTIONS
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ require 'active_model'
5
+
6
+ class TypeValidator
7
+ class KindOf
8
+ def self.invalid?(record, attribute, value, options)
9
+ types = Array(options[:is_a] || options[:kind_of])
10
+ allow_nil = options[:allow_nil]
11
+
12
+ return if (allow_nil && value.nil?) || types.any? { |type| value.is_a?(type) }
13
+
14
+ "must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- TYPE_VALIDATOR_VERSION = '0.1.0'
3
+ class TypeValidator
4
+ VERSION = '0.2.0'
5
+ end
@@ -2,29 +2,24 @@
2
2
 
3
3
  require 'active_model'
4
4
 
5
- require 'type_validator/version'
6
-
7
5
  class TypeValidator < ActiveModel::EachValidator
8
- VERSION = TYPE_VALIDATOR_VERSION
9
-
10
- INVALID_DEFINITION = ArgumentError.new(
11
- 'invalid type definition. Options to define one: `:is_a` or `:kind_of`'
12
- )
13
-
14
6
  def validate_each(record, attribute, value)
15
- types = Array(options[:is_a] || options[:kind_of]).flatten
16
- allow_nil = options[:allow_nil]
7
+ strategy = fetch_strategy(options)
17
8
 
18
- raise INVALID_DEFINITION if types.empty?
9
+ raise Error::InvalidDefinition.new(attribute) unless strategy
19
10
 
20
- return if (allow_nil && value.nil?) || types.any? { |type| value.is_a?(type) }
11
+ return unless error = strategy.invalid?(record, attribute, value, options)
21
12
 
22
- message = "must be a kind of: #{types.map(&:name).join(', ')}"
13
+ raise TypeError, "#{attribute} #{error}" if options[:strict]
23
14
 
24
- if options[:strict]
25
- raise TypeError, "#{attribute} #{message}"
26
- else
27
- record.errors.add(attribute, message)
28
- end
15
+ record.errors.add(attribute, error)
16
+ end
17
+
18
+ def fetch_strategy(options)
19
+ KindOf if options.key?(:is_a) || options.key?(:kind_of)
29
20
  end
30
21
  end
22
+
23
+ require 'type_validator/error'
24
+ require 'type_validator/kind_of'
25
+ require 'type_validator/version'
@@ -1,16 +1,17 @@
1
1
 
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'type_validator/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = 'type_validator'
8
- spec.version = TYPE_VALIDATOR_VERSION
7
+ spec.version = File.readlines(File.join('.', 'lib', 'type_validator', 'version.rb'))
8
+ .find { |line| line =~ /VERSION/ }
9
+ .strip[/(\d\.?){3}/]
9
10
  spec.authors = ['Rodrigo Serradura']
10
11
  spec.email = ['rodrigo.serradura@gmail.com']
11
12
 
12
- spec.summary = %q{Adds type validation for ActiveModel.}
13
- spec.description = %q{Adds type validation for ActiveModel.}
13
+ spec.summary = %q{Adds type validation for classes with ActiveModel::Validations.}
14
+ spec.description = %q{Adds type validation for classes with ActiveModel::Validations.}
14
15
  spec.homepage = 'https://github.com/serradura/type_validator'
15
16
  spec.license = 'MIT'
16
17
 
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.1.0
4
+ version: 0.2.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-16 00:00:00.000000000 Z
11
+ date: 2019-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5.0'
69
- description: Adds type validation for ActiveModel.
69
+ description: Adds type validation for classes with ActiveModel::Validations.
70
70
  email:
71
71
  - rodrigo.serradura@gmail.com
72
72
  executables: []
@@ -84,6 +84,8 @@ files:
84
84
  - bin/console
85
85
  - bin/setup
86
86
  - lib/type_validator.rb
87
+ - lib/type_validator/error.rb
88
+ - lib/type_validator/kind_of.rb
87
89
  - lib/type_validator/version.rb
88
90
  - test.sh
89
91
  - type_validator.gemspec
@@ -111,5 +113,5 @@ requirements: []
111
113
  rubygems_version: 3.0.3
112
114
  signing_key:
113
115
  specification_version: 4
114
- summary: Adds type validation for ActiveModel.
116
+ summary: Adds type validation for classes with ActiveModel::Validations.
115
117
  test_files: []