be_valid 0.1.0 → 0.1.5

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: abbbdea456536973df02299690f700bab640ac727c65fd257048e6427a20964c
4
- data.tar.gz: 43bbbba7ed6a4ef05ca0a0f0a713108f17dc3aa97a4a981e6a65b1022015136b
3
+ metadata.gz: 321b08decb577f0a5e7ceded71bf6c05829cd877a28f2b377f606e2f56cf6e61
4
+ data.tar.gz: c68cb42238a0480b4c2768b159fe7dca5bb690004f43027df728bdeca71b0f23
5
5
  SHA512:
6
- metadata.gz: 6389b8bf085014beb16d64fc0a559d945ebbef1f2e8d2f78900323c5650a69ba66abef5cc74158fd1093424031c1aefc3b915cb32b63036e2461219b4a36ac49
7
- data.tar.gz: e59092fc516be70996b024c886fb21562298d1022582b42b689fa27cf8ef1b20d3439856584a518687c36bb6d8deabbbc09d7d9e8185a11a06e7647c26893e07
6
+ metadata.gz: f085944be0b2681549ef91f534d2fb3a888d95be819f5bbd2e7143ac8d03817322229d5bbd1f8b46705dfe11c447006049f10c60fd301b7b4a6fa76f64c62e1e
7
+ data.tar.gz: c9f38ad043a959f56386d785b747197e0e6936867532a315348b47227858615040e8a0c3c20c7dca654f8292c815257f868d0eea9fd829072330219169f18f0b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- be_valid (0.1.0)
4
+ be_valid (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/be_valid.gemspec CHANGED
@@ -7,7 +7,6 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "be_valid"
8
8
  spec.version = BeValid::VERSION
9
9
  spec.authors = ["John Stewart (johnsinco)"]
10
- spec.email = ["johnstewartco@outlook.com"]
11
10
 
12
11
  spec.summary = %q{Custom Ruby on Rails Validation library supporting 'conditional' validation}
13
12
  spec.description = %q{Provides more advanced and flexible 'if then then that' style conditional validation. Validate fields based on the values of other fields in your model.}
@@ -24,6 +23,8 @@ Gem::Specification.new do |spec|
24
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
24
  spec.require_paths = ["lib"]
26
25
 
26
+ spec.required_ruby_version = '>= 2.3'
27
+
27
28
  spec.add_development_dependency "bundler", "~> 1.17"
28
29
  spec.add_development_dependency "rake", "~> 10.0"
29
30
  spec.add_development_dependency "minitest", "~> 5.0"
data/lib/be_valid.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'be_valid/be_valid'
2
2
 
3
3
  begin
4
- require 'validators/must_be_validator'
5
4
  require 'validators/date_validator'
5
+ require 'validators/must_be_validator'
6
6
  rescue
7
- 'unable to loan be_valid validator'
7
+ 'unable to load be_valid validator'
8
8
  nil
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module BeValid
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -1,97 +1,109 @@
1
- class MustBeValidator < ActiveModel::EachValidator
2
- MATCHERS = {
3
- equal_to: :==,
4
- greater_than: :>,
5
- greater_or_equal_to: :>=,
6
- less_than: :<,
7
- less_or_equal_to: :<=,
8
- not_equal_to: :!=,
9
- matching: :=~,
10
- # one_of: :in?
11
- }.freeze
1
+ require 'active_record'
12
2
 
13
- MESSAGE_PREFIX = "must be"
14
- ERRORS_METHOD = :errors
3
+ module ActiveModel
4
+ module Validations
5
+ class MustBeValidator < EachValidator
6
+ MATCHERS = {
7
+ equal_to: :==,
8
+ greater_than: :>,
9
+ greater_or_equal_to: :>=,
10
+ less_than: :<,
11
+ less_or_equal_to: :<=,
12
+ not_equal_to: :!=,
13
+ matching: :=~,
14
+ # one_of: :in?
15
+ }.freeze
15
16
 
16
- def validate_each(record, attribute, value)
17
- raise "must_be_validator requires at least one comparison operator for attribute." unless options.slice(MATCHERS.keys + [:blank, :present])
17
+ MESSAGE_PREFIX = "must be"
18
+ ERRORS_METHOD = :errors
18
19
 
19
- original_value = record.read_attribute_before_type_cast( attribute )
20
+ def validate_each(record, attribute, value)
21
+ raise "must_be_validator requires at least one comparison operator for attribute." unless options.slice(MATCHERS.keys + [:blank, :present])
20
22
 
21
- message = self.class::MESSAGE_PREFIX.dup
23
+ original_value = record.read_attribute_before_type_cast( attribute )
22
24
 
23
- return if options[:blank] && value.blank?
24
- message << " blank" if options[:blank]
25
+ message = self.class::MESSAGE_PREFIX.dup
25
26
 
26
- return if options[:present] && value.present?
27
- message << " present" if options[:present]
27
+ return if options[:blank] && value.blank?
28
+ message << " blank" if options[:blank]
28
29
 
29
- return if options[:one_of] && options[:one_of].include?(value)
30
- message = ": '#{value}' is not a valid value" if options[:one_of]
30
+ return if options[:present] && value.present?
31
+ message << " present" if options[:present]
31
32
 
32
- return if options[:not_any_of] && !(options[:not_any_of].include?(value))
33
- message = ": '#{value}' is not a valid value" if options[:not_any_of]
33
+ return if options[:one_of] && Array(options[:one_of]).flatten.include?(value)
34
+ message = ": '#{value}' is not a valid value" if options[:one_of]
34
35
 
35
- return if options[:only_from] && (options[:only_from] & Array(value) == Array(value))
36
- message = ": #{Array(value).join(",").gsub('"', '\'')} is not a valid value" if options[:only_from]
36
+ return if options[:not_any_of] && !(options[:not_any_of].include?(value))
37
+ message = ": '#{value}' is not a valid value" if options[:not_any_of]
37
38
 
38
- # handle before and after date comparisons using date validator
39
- if options[:before]
40
- before_resp = DateValidator.new(options.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value)
41
- return if before_resp == true
42
- message = before_resp
43
- end
44
- if options[:after]
45
- after_resp = DateValidator.new(options.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value)
46
- return if after_resp == true
47
- message = after_resp
48
- end
39
+ return if options[:only_from] && (options[:only_from] & Array(value) == Array(value))
40
+ message = ": #{Array(value).join(",").gsub('"', '\'')} is not a valid value" if options[:only_from]
49
41
 
50
- options.slice(*MATCHERS.keys).each do |key, operand|
51
- operand_msg = operand.is_a?(Regexp) ? operand.inspect : operand.to_s
52
- operand = record.send(operand) if operand.is_a? Symbol
53
- return if operand.nil?
54
- return if value&.send(MATCHERS[key], operand)
55
- return if key == :not_equal_to && value != operand
56
- message << " #{key.to_s.humanize(capitalize: false)} #{operand_msg}"
57
- end
42
+ # handle before and after date comparisons using date validator
43
+ if options[:before]
44
+ before_resp = DateValidator.new(options.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value)
45
+ return if before_resp == true
46
+ message = before_resp
47
+ end
48
+ if options[:after]
49
+ after_resp = DateValidator.new(options.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value)
50
+ return if after_resp == true
51
+ message = after_resp
52
+ end
58
53
 
59
- if options[:when].present?
60
- # check if conditions to validate satisfied
61
- conditions = options[:when]
62
- raise "Invalid :when option provided to must_be, must be a Hash or method" unless (conditions.is_a? Hash || conditions.is_a?(Method))
63
- # add error message with predicate info
64
- message << " when "
65
- condition_errors = []
66
- conditions.each do |field, values|
67
- case values
68
- when Regexp
69
- return if !values.match?(record[field])
70
- condition_errors << "#{field} = #{record[field]}"
71
- when Array
72
- return if !values.include?(record[field])
73
- condition_errors << "#{field} = #{record[field]}"
74
- when Symbol
75
- return if !record.send(field)&.send(values)
76
- condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
77
- else
78
- return if values != record[field]
79
- condition_errors << "#{field} = #{record[field]}"
54
+ options.slice(*MATCHERS.keys).each do |key, operand|
55
+ operand_msg = operand.is_a?(Regexp) ? operand.inspect : operand.to_s
56
+ operand = record.send(operand) if operand.is_a? Symbol
57
+ return if operand.nil?
58
+ return if value&.send(MATCHERS[key], operand)
59
+ return if key == :not_equal_to && value != operand
60
+ message << " #{key.to_s.humanize(capitalize: false)} #{operand_msg}"
80
61
  end
81
- end
82
- message << condition_errors.join(' and ')
83
- end
84
62
 
85
- if options[:one_of] && options.fetch(:show_values, true)
86
- message << ". Valid values: #{options[:one_of].join(', ')}"
87
- end
88
- if options[:only_from] && options.fetch(:show_values, true)
89
- message << ". Valid values: #{options[:only_from].join(', ')}"
90
- end
91
- if options[:not_any_of] && options.fetch(:show_values, true)
92
- message << ". Invalid values: #{options[:not_any_of].join(', ')}"
93
- end
63
+ if options[:when].present?
64
+ # check if conditions to validate satisfied
65
+ conditions = options[:when]
66
+ raise "Invalid :when option provided to must_be, must be a Hash or method" unless (conditions.is_a? Hash || conditions.is_a?(Method))
67
+ # add error message with predicate info
68
+ message << " when "
69
+ condition_errors = []
70
+ conditions.each do |field, values|
71
+ case values
72
+ when Regexp
73
+ return if !values.match?(record[field])
74
+ condition_errors << "#{field} = #{record[field]}"
75
+ when Array
76
+ return if !values.flatten.include?(record[field])
77
+ condition_errors << "#{field} = #{record[field]}"
78
+ when :blank
79
+ return unless record.send(field).blank?
80
+ condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
81
+ when :present
82
+ return unless record.send(field).present?
83
+ condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
84
+ when Symbol
85
+ return if !record.send(field)&.send(values)
86
+ condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
87
+ else
88
+ return if values != record[field]
89
+ condition_errors << "#{field} = #{record[field]}"
90
+ end
91
+ end
92
+ message << condition_errors.join(' and ')
93
+ end
94
+
95
+ if options[:one_of] && options.fetch(:show_values, true)
96
+ message << ". Valid values: #{options[:one_of].join(', ')}"
97
+ end
98
+ if options[:only_from] && options.fetch(:show_values, true)
99
+ message << ". Valid values: #{options[:only_from].join(', ')}"
100
+ end
101
+ if options[:not_any_of] && options.fetch(:show_values, true)
102
+ message << ". Invalid values: #{options[:not_any_of].join(', ')}"
103
+ end
94
104
 
95
- record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
105
+ record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
106
+ end
107
+ end
96
108
  end
97
109
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: be_valid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Stewart (johnsinco)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-19 00:00:00.000000000 Z
11
+ date: 2021-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,7 +83,6 @@ dependencies:
83
83
  description: Provides more advanced and flexible 'if then then that' style conditional
84
84
  validation. Validate fields based on the values of other fields in your model.
85
85
  email:
86
- - johnstewartco@outlook.com
87
86
  executables: []
88
87
  extensions: []
89
88
  extra_rdoc_files: []
@@ -114,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
113
  requirements:
115
114
  - - ">="
116
115
  - !ruby/object:Gem::Version
117
- version: '0'
116
+ version: '2.3'
118
117
  required_rubygems_version: !ruby/object:Gem::Requirement
119
118
  requirements:
120
119
  - - ">="