type_validator 0.6.0 → 0.7.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: bb4c476ec164e1a9b0de994b41e374307ef6dd2d3a893b8b18b905b60472dd8e
4
- data.tar.gz: e9be80d3aca12db0140cac027ffed3a5d3e2f3ab67c5e5267711cc5f7db6502e
3
+ metadata.gz: 16bbf2c1f264c32540b3a2a0b455dc14a5b808a85ff17ab2f2774070ac7a95a2
4
+ data.tar.gz: a7c6e27a77306aa5b79b9040c8823bb5da18d5bcd2e807b1de1b1a1f7f0c982a
5
5
  SHA512:
6
- metadata.gz: da1a3abe19d8fb978109034d3341dc654d590e218eed093c31dae86e61b46327e7a8cdecd0a7fd8f3bff98fc0ea8187064c49ea74c42414441738885a43aa14c
7
- data.tar.gz: 26b5661893d5699fff7ee08aff15e120dd57542b2147477c85467a5e3c796e749a2ed53913985d4cd3b6a345da782254ef16fe29c7d4c992b0d71f7f94398ee0
6
+ metadata.gz: 67949f5d4714cb33c9d2c6d759361993d0b28426644bacde350a86f597849e8313d13083d15abc046da4e03fe57226b9a6679dfc46b623be65f2f30ace13d672
7
+ data.tar.gz: 94af44dc82dbb1c52cbc2a7ea109e29d997db51a91852e409a7e2a5c1843aeb6e61fa353cc3a25e42f6305fed93b9f4646f2ac308aaacb45779f0a13777801d3
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.travis.yml CHANGED
@@ -18,12 +18,4 @@ before_install:
18
18
 
19
19
  install: bundle install --jobs=3 --retry=3
20
20
 
21
- before_script:
22
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
23
- - chmod +x ./cc-test-reporter
24
- - "./cc-test-reporter before-build"
25
-
26
21
  script: "./.travis.sh"
27
-
28
- after_success:
29
- - "./cc-test-reporter after-build -t simplecov"
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
- activemodel_version = ENV.fetch('ACTIVEMODEL_VERSION', '6.1')
5
+ activemodel_version = ENV.fetch('ACTIVEMODEL_VERSION', '6.0')
6
6
 
7
7
  activemodel = case activemodel_version
8
8
  when '3.2' then '3.2.22'
@@ -14,7 +14,7 @@ activemodel = case activemodel_version
14
14
  when '5.2' then '5.2.3'
15
15
  end
16
16
 
17
- if activemodel_version < '6.1'
17
+ if activemodel_version < '6.0'
18
18
  gem 'activemodel', activemodel, require: false
19
19
  gem 'activesupport', activemodel, require: false
20
20
  end
@@ -24,5 +24,9 @@ group :test do
24
24
  gem 'simplecov', require: false
25
25
  end
26
26
 
27
+ if RUBY_VERSION >= '2.6.0' && activemodel_version >= '6.0'
28
+ gem 'coveralls', require: false
29
+ end
30
+
27
31
  # Specify your gem's dependencies in type_validator.gemspec
28
32
  gemspec
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
1
  [![Build Status](https://travis-ci.com/serradura/type_validator.svg?branch=master)](https://travis-ci.com/serradura/type_validator)
2
+ [![Coverage Status](https://coveralls.io/repos/github/serradura/type_validator/badge.svg?branch=master)](https://coveralls.io/github/serradura/type_validator?branch=master)
2
3
 
3
4
  # TypeValidator
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/type_validator`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- TODO: Delete this and the text above, and describe your gem
6
+ Adds type validation for classes with [`ActiveModel::Validations >= 3.2`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html).
8
7
 
9
8
  ## Installation
10
9
 
@@ -24,7 +23,55 @@ Or install it yourself as:
24
23
 
25
24
  ## Usage
26
25
 
27
- TODO: Write usage instructions here
26
+ Use one or all of the type validations into your models/classes:
27
+
28
+ ```ruby
29
+ validates :name, type: { is_a: String }
30
+ # or
31
+ validates :name, type: { kind_of: String }
32
+
33
+ # Use an array to verify if the attribute is an instance of one of the classes
34
+
35
+ validates :status, type: { is_a: [String, Symbol]}
36
+ # or
37
+ validates :status, type: { kind_of: [String, Symbol]}
38
+ ```
39
+
40
+ ```ruby
41
+ validates :handler, type: { respond_to: :call }
42
+ ```
43
+
44
+ ```ruby
45
+ # Verifies if the attribute value is the class or a subclass.
46
+
47
+ validates :handler, type: { klass: Handler }
48
+ ```
49
+
50
+ ```ruby
51
+ validates :account_types, type: { array_of: String }
52
+
53
+ # or use an array to verify if the attribute is an instance of one of the classes
54
+
55
+ validates :account_types, type: { array_of: [String, Symbol] }
56
+ ```
57
+
58
+ ```ruby
59
+ # Verifies if the attribute value is an array with some or all the expected values.
60
+
61
+ validates :account_types, type: { array_with: ['foo', 'bar'] }
62
+ ```
63
+
64
+ **All the validations above accept:**
65
+ - `allow_nil` option. e.g:. e.g:
66
+ ```ruby
67
+ validates :name, type: { is_a: String }, allow_nil: true
68
+ ```
69
+ - `strict: true` option or the usage of `validates!`method. e.g:
70
+ ```ruby
71
+ validates :name, type: { is_a: String }, strict: true
72
+ #or
73
+ validates! :name, type: { is_a: String }
74
+ ```
28
75
 
29
76
  ## Development
30
77
 
@@ -34,7 +81,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
34
81
 
35
82
  ## Contributing
36
83
 
37
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/type_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/type_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
85
 
39
86
  ## License
40
87
 
@@ -42,4 +89,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
42
89
 
43
90
  ## Code of Conduct
44
91
 
45
- Everyone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/type_validator/blob/master/CODE_OF_CONDUCT.md).
92
+ Everyone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/type_validator/blob/master/CODE_OF_CONDUCT.md).
@@ -5,9 +5,8 @@ require 'active_model'
5
5
  class TypeValidator
6
6
  class ByArrayOf
7
7
  def self.invalid?(value, options)
8
- types, allow_nil = Array(options[:array_of]), options[:allow_nil]
8
+ types = Array(options[:array_of])
9
9
 
10
- return if (allow_nil && value.nil?)
11
10
  return if value.is_a?(Array) && !value.empty? && value.all? { |value| types.any? { |type| value.is_a?(type) } }
12
11
 
13
12
  "must be an array of: #{types.map { |klass| klass.name }.join(', ')}"
@@ -5,11 +5,10 @@ require 'active_model'
5
5
  class TypeValidator
6
6
  class ByArrayWith
7
7
  def self.invalid?(value, options)
8
- expected, allow_nil = options[:array_with], options[:allow_nil]
8
+ expected = options[:array_with]
9
9
 
10
10
  raise ArgumentError, "#{expected} must be an array" unless expected.is_a?(Array)
11
11
 
12
- return if (allow_nil && value.nil?)
13
12
  return if value.is_a?(Array) && !value.empty? && (value - expected).empty?
14
13
 
15
14
  "must be an array with: #{expected.join(', ')}"
@@ -6,9 +6,8 @@ class TypeValidator
6
6
  class ByKindOf
7
7
  def self.invalid?(value, options)
8
8
  types = Array(options[:is_a] || options[:kind_of])
9
- allow_nil = options[:allow_nil]
10
9
 
11
- return if (allow_nil && value.nil?) || types.any? { |type| value.is_a?(type) }
10
+ return if types.any? { |type| value.is_a?(type) }
12
11
 
13
12
  "must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
14
13
  end
@@ -5,12 +5,12 @@ require 'active_model'
5
5
  class TypeValidator
6
6
  class ByKlass
7
7
  def self.invalid?(value, options)
8
- klass, allow_nil = options[:klass], options[:allow_nil]
8
+ klass = options[:klass]
9
9
 
10
10
  require_a_class(value)
11
11
  require_a_class(klass)
12
12
 
13
- return if (allow_nil && value.nil?) || (value == klass || value < klass)
13
+ return if value == klass || value < klass
14
14
 
15
15
  "must be the or a subclass of `#{klass.name}`"
16
16
  end
@@ -5,9 +5,9 @@ require 'active_model'
5
5
  class TypeValidator
6
6
  class ByRespondTo
7
7
  def self.invalid?(value, options)
8
- method_name, allow_nil = options[:respond_to], options[:allow_nil]
8
+ method_name = options[:respond_to]
9
9
 
10
- return if (allow_nil && value.nil?) || value.respond_to?(method_name)
10
+ return if value.respond_to?(method_name)
11
11
 
12
12
  "must respond to the method `#{method_name}`"
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -8,6 +8,7 @@ class TypeValidator < ActiveModel::EachValidator
8
8
 
9
9
  raise Error::InvalidDefinition.new(attribute) unless strategy
10
10
 
11
+ return if options[:allow_nil] && value.nil?
11
12
  return unless error = strategy.invalid?(value, options)
12
13
 
13
14
  raise TypeError, "#{attribute} #{error}" if options[:strict]
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".coveralls.yml"
76
77
  - ".gitignore"
77
78
  - ".travis.sh"
78
79
  - ".travis.yml"