simple_validate 1.2.3 → 2.0.1

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: 4a55b4307e3f7b02f9caf048c5e892bf4230100557460d58df45b28dab196a68
4
- data.tar.gz: 54faeafbe20dc26a0ec96e6d087fa04195ea0efdf6593fcd28a356fc4decc8b7
3
+ metadata.gz: 215eb056ff7677f32a10c721a53879a52cf3c08f2b8567ba63b86a6aff81d544
4
+ data.tar.gz: 4340fa2cfb67c41853f9e5aa4f2d6d1ff8303916d648916bceb799503461dc59
5
5
  SHA512:
6
- metadata.gz: 1dab29ca30408fd05ca35e81fe8e7fc7db0029b5622369a7bbc89d9a3b8a85c92947a5c423ac0d2494c52f933569bc4ae96328cf3ed0a9ae87a422627719b400
7
- data.tar.gz: 3c78f456e17d56a3649e4bde7475a32a084de618676bb0318c230f0572e6a727271f9afefcbc8d3f47beeda49bf7bdf16a10f1dd4d1efb43563db2330b0b1cfb
6
+ metadata.gz: a0a854c7b4fd9005eb7e86b035aa585c709d25d33c2b784948ab9d824804c0fdffb04b6e29703bd330f69037d8f9043edf168ca6ede3055a91da5a543ff44db7
7
+ data.tar.gz: 67c997ae9cc465d5b54e68b11b5c8438d04cc6b138c981953958c7b8761ba77c472f4a09d9af6a1178ba96271ba2bb8b0bb9cdf81d968b17594f61998a20f68f
data/.rspec_status ADDED
@@ -0,0 +1,5 @@
1
+ example_id | status | run_time |
2
+ ------------------------------------- | ------ | --------------- |
3
+ ./spec/simple_validate_spec.rb[1:1:1] | passed | 0.00127 seconds |
4
+ ./spec/simple_validate_spec.rb[1:2:1] | passed | 0.0001 seconds |
5
+ ./spec/simple_validate_spec.rb[1:3:1] | passed | 0.00106 seconds |
data/.rubocop.yml CHANGED
@@ -1,3 +1,10 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
1
8
  Metrics/BlockLength:
2
9
  Enabled: false
3
10
 
@@ -9,3 +16,6 @@ Metrics/AbcSize:
9
16
 
10
17
  Style/DoubleNegation:
11
18
  Enabled: false
19
+
20
+ Lint/ConstantDefinitionInBlock:
21
+ Enabled: false
data/Gemfile CHANGED
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source 'https://rubygems.org'
3
+ source "https://rubygems.org"
4
4
 
5
- gem 'rake'
5
+ gem "rake"
6
6
 
7
7
  group :development do
8
- gem 'pry'
8
+ gem "pry"
9
9
  end
10
10
 
11
11
  group :test do
12
- gem 'rspec'
13
- gem 'rubocop'
12
+ gem "rspec"
13
+ gem "rubocop"
14
14
  end
15
15
 
16
16
  gemspec
data/README.md CHANGED
@@ -1,29 +1,15 @@
1
1
  # Simple Validate
2
2
 
3
- [![Build Status](https://travis-ci.org/nikkypx/simple_validate.svg?branch=master)](https://travis-ci.org/nikkypx/simple_validate)
4
-
5
- > Borrowing ideas from [validatable](https://github.com/jnunemaker/validatable) and Rails validations, this is a library for validations for any ruby object.
3
+ > PORO validation mixin with no deps
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
10
-
11
7
  ```ruby
12
8
  gem 'simple_validate'
13
9
  ```
14
10
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install simple_validate
22
-
23
11
  ## Usage
24
12
 
25
- ### Example
26
-
27
13
  ```ruby
28
14
  require 'simple_validate'
29
15
 
@@ -54,10 +40,10 @@ end
54
40
  validates_presence_of :attribute
55
41
  ```
56
42
 
57
- ### Numericality
43
+ ### Type
58
44
 
59
45
  ```ruby
60
- validates_numericality_of :attribute
46
+ validates_type_of :attribute, as: :string
61
47
  ```
62
48
 
63
49
  ### Format
@@ -101,18 +87,6 @@ end
101
87
  validates_presence_of :attribute, if: Proc.new { true }
102
88
  ```
103
89
 
104
-
105
- ## Development
106
-
107
- `$ rake` to run the specs
108
-
109
-
110
- ## Contributing
111
-
112
- Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/simple_validate.
113
-
114
-
115
90
  ## License
116
91
 
117
92
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
118
-
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
- require 'rubocop/rake_task'
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
6
 
7
7
  RSpec::Core::RakeTask.new(:spec)
8
8
  RuboCop::RakeTask.new
9
9
 
10
- task default: %i[spec rubocop:auto_correct]
10
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "simple_validate"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -20,6 +20,10 @@ module SimpleValidate
20
20
  @messages.fetch(key)
21
21
  end
22
22
 
23
+ def clear!
24
+ @messages = {}
25
+ end
26
+
23
27
  def empty?
24
28
  @messages.empty?
25
29
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleValidate
4
+ module Utils
5
+ def self.camelize(input) = input.to_s.split("_").map(&:capitalize).join
6
+ def self.extract_options!(args) = args.last.is_a?(Hash) ? args.pop : {}
7
+ def self.classify(input) = Object.const_get(camelize(input))
8
+
9
+ def self.article(input)
10
+ vowels = %w[a e i o u]
11
+ vowels.include?(input[0]) ? "an" : "a"
12
+ end
13
+ end
14
+ end
@@ -5,9 +5,9 @@ module SimpleValidate
5
5
  attr_accessor :regex
6
6
 
7
7
  def initialize(attribute, options)
8
- self.regex = options[:with]
8
+ @regex = options[:with]
9
9
  super(attribute, options[:message] ||
10
- 'is incorrect format', options[:if] || proc { true })
10
+ "is incorrect format", options[:if] || proc { true })
11
11
  end
12
12
 
13
13
  def valid?(instance)
@@ -15,15 +15,13 @@ module SimpleValidate
15
15
  end
16
16
 
17
17
  def message
18
- @message ||= begin
19
- case validator
20
- when :minimum
21
- 'is too short'
22
- when :maximum
23
- 'is too long'
24
- else
25
- 'is not the correct length'
26
- end
18
+ @message ||= case validator
19
+ when :minimum
20
+ "is too short"
21
+ when :maximum
22
+ "is too long"
23
+ else
24
+ "is not the correct length"
27
25
  end
28
26
  end
29
27
 
@@ -49,9 +47,7 @@ module SimpleValidate
49
47
  end
50
48
 
51
49
  def valid?(instance)
52
- if options.keys.size > 1
53
- raise ArgumentError, 'Only one length argument can be provided'
54
- end
50
+ raise ArgumentError, "Only one length argument can be provided" if options.keys.size > 1
55
51
 
56
52
  unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
57
53
  raise InvalidLengthOption, "Invalid length option given #{options.keys}"
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'set'
4
-
5
3
  module SimpleValidate
6
4
  class ValidatesSetBase < ValidatesBase
7
5
  attr_accessor :set, :options
@@ -10,7 +8,7 @@ module SimpleValidate
10
8
  self.options = options
11
9
  self.set = Set.new(Array(options[:in]).map(&:to_s))
12
10
  super(attribute, options[:message] ||
13
- 'breaks inclusion/exclusion rules', options[:if] || proc { true })
11
+ "breaks inclusion/exclusion rules", options[:if] || proc { true })
14
12
  end
15
13
  end
16
14
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleValidate
4
+ class ValidatesTypeOf < ValidatesBase
5
+ SUPPORTED_TYPES = %i[string integer float].freeze
6
+
7
+ def initialize(attribute, options)
8
+ @type = options[:as]
9
+
10
+ raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)
11
+
12
+ @klass = Utils.classify(options[:as])
13
+
14
+ super(attribute, options[:message] ||
15
+ "must be #{Utils.article(@type)} #{@type}", options[:if] || proc { true })
16
+ end
17
+
18
+ def valid?(instance)
19
+ instance.send(attribute).is_a?(@klass)
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- VERSION = '1.2.3'
4
+ VERSION = "2.0.1"
5
5
  end
@@ -3,7 +3,7 @@
3
3
  %w[base
4
4
  presence_of
5
5
  format_of
6
- numericality_of
6
+ type_of
7
7
  length_of
8
8
  set_base
9
9
  inclusion_of
@@ -11,9 +11,9 @@
11
11
  require "simple_validate/validates_#{validation}"
12
12
  end
13
13
 
14
- require 'simple_validate/errors'
15
- require 'simple_validate/version'
16
- require 'active_support/all'
14
+ require "simple_validate/errors"
15
+ require "simple_validate/version"
16
+ require "simple_validate/utils"
17
17
 
18
18
  module SimpleValidate
19
19
  def self.included(klass)
@@ -38,7 +38,7 @@ module SimpleValidate
38
38
  module ClassMethods
39
39
  def method_missing(method, *args, &block)
40
40
  if respond_to?(method)
41
- add_validations(args, const_get(method.to_s.classify))
41
+ add_validations(args, const_get(Utils.camelize(method)))
42
42
  else
43
43
  super
44
44
  end
@@ -48,7 +48,7 @@ module SimpleValidate
48
48
  method.to_s =~ /(validates_
49
49
  (format|
50
50
  presence|
51
- numericality|
51
+ type|
52
52
  inclusion|
53
53
  exclusion|
54
54
  length)_of)
@@ -56,7 +56,7 @@ module SimpleValidate
56
56
  end
57
57
 
58
58
  def add_validations(args, klass)
59
- options = args.extract_options!
59
+ options = Utils.extract_options! args
60
60
  args.each do |attr|
61
61
  validations << klass.new(attr, options)
62
62
  end
@@ -67,6 +67,8 @@ module SimpleValidate
67
67
  end
68
68
 
69
69
  def validate(instance)
70
+ instance.errors.clear!
71
+
70
72
  validations.each do |validation|
71
73
  if validation.condition.call && !validation.valid?(instance)
72
74
  instance.errors.add(validation.attribute, validation.message)
@@ -1,25 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path('lib', __dir__)
3
+ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'simple_validate/version'
5
+ require "simple_validate/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = 'simple_validate'
8
+ spec.name = "simple_validate"
9
9
  spec.version = SimpleValidate::VERSION
10
- spec.authors = ['Nick Palaniuk']
11
- spec.email = ['npalaniuk@gmail.com']
10
+ spec.authors = ["Nick Palaniuk"]
11
+ spec.email = ["npalaniuk@gmail.com"]
12
12
 
13
- spec.summary = 'Validations for any plain old ruby object'
14
- spec.description = 'Validations for any ruby object'
15
- spec.homepage = 'https://github.com/nikkypx/simple_validate'
16
- spec.license = 'MIT'
13
+ spec.summary = "PORO validation mixin with no deps"
14
+ spec.description = "PORO validation mixin with no deps"
15
+ spec.homepage = "https://github.com/nikkypx/simple_validate"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 3.1"
17
18
 
18
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
20
  f.match(%r{^(test|spec|features)/})
20
21
  end
21
22
 
22
- spec.require_paths = ['lib']
23
- spec.add_dependency 'activesupport'
24
- spec.add_development_dependency 'bundler'
23
+ spec.require_paths = ["lib"]
25
24
  end
metadata CHANGED
@@ -1,44 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-25 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- description: Validations for any ruby object
11
+ date: 2023-08-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: PORO validation mixin with no deps
42
14
  email:
43
15
  - npalaniuk@gmail.com
44
16
  executables: []
@@ -47,29 +19,32 @@ extra_rdoc_files: []
47
19
  files:
48
20
  - ".gitignore"
49
21
  - ".rspec"
22
+ - ".rspec_status"
50
23
  - ".rubocop.yml"
51
- - ".travis.yml"
52
24
  - Gemfile
53
25
  - LICENSE.txt
54
26
  - README.md
55
27
  - Rakefile
28
+ - bin/console
29
+ - bin/setup
56
30
  - lib/simple_validate.rb
57
31
  - lib/simple_validate/errors.rb
32
+ - lib/simple_validate/utils.rb
58
33
  - lib/simple_validate/validates_base.rb
59
34
  - lib/simple_validate/validates_exclusion_of.rb
60
35
  - lib/simple_validate/validates_format_of.rb
61
36
  - lib/simple_validate/validates_inclusion_of.rb
62
37
  - lib/simple_validate/validates_length_of.rb
63
- - lib/simple_validate/validates_numericality_of.rb
64
38
  - lib/simple_validate/validates_presence_of.rb
65
39
  - lib/simple_validate/validates_set_base.rb
40
+ - lib/simple_validate/validates_type_of.rb
66
41
  - lib/simple_validate/version.rb
67
42
  - simple_validate.gemspec
68
43
  homepage: https://github.com/nikkypx/simple_validate
69
44
  licenses:
70
45
  - MIT
71
46
  metadata: {}
72
- post_install_message:
47
+ post_install_message:
73
48
  rdoc_options: []
74
49
  require_paths:
75
50
  - lib
@@ -77,15 +52,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
52
  requirements:
78
53
  - - ">="
79
54
  - !ruby/object:Gem::Version
80
- version: '0'
55
+ version: '3.1'
81
56
  required_rubygems_version: !ruby/object:Gem::Requirement
82
57
  requirements:
83
58
  - - ">="
84
59
  - !ruby/object:Gem::Version
85
60
  version: '0'
86
61
  requirements: []
87
- rubygems_version: 3.0.3
88
- signing_key:
62
+ rubygems_version: 3.4.19
63
+ signing_key:
89
64
  specification_version: 4
90
- summary: Validations for any plain old ruby object
65
+ summary: PORO validation mixin with no deps
91
66
  test_files: []
data/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.2
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleValidate
4
- class ValidatesNumericalityOf < ValidatesBase
5
- def initialize(attribute, options)
6
- super(attribute, options[:message] ||
7
- 'must be a number', options[:if] || proc { true })
8
- end
9
-
10
- def valid?(instance)
11
- instance.send(attribute).is_a?(Numeric)
12
- end
13
- end
14
- end