be_valid 0.1.0 → 0.1.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: abbbdea456536973df02299690f700bab640ac727c65fd257048e6427a20964c
4
- data.tar.gz: 43bbbba7ed6a4ef05ca0a0f0a713108f17dc3aa97a4a981e6a65b1022015136b
3
+ metadata.gz: 2944b23a3a3083f820642b5ccfa51910377bcbecd55c43181918dd32fc7c9e19
4
+ data.tar.gz: 64231d1fd01a22f112787eb542d24ae9bdfa586abd5d2a9ee37d565e5657bddf
5
5
  SHA512:
6
- metadata.gz: 6389b8bf085014beb16d64fc0a559d945ebbef1f2e8d2f78900323c5650a69ba66abef5cc74158fd1093424031c1aefc3b915cb32b63036e2461219b4a36ac49
7
- data.tar.gz: e59092fc516be70996b024c886fb21562298d1022582b42b689fa27cf8ef1b20d3439856584a518687c36bb6d8deabbbc09d7d9e8185a11a06e7647c26893e07
6
+ metadata.gz: 953aa143b7b28fac560340cecdf45b79396af52a0356c2fa4e55037006acc4dbbb808576ef1d83d78be4ad38f0b2e3b6e6166e9e0279d7b33838015d2723fa01
7
+ data.tar.gz: e6b71a61a45cb30e5c45798d1fd2cdffb721a79fe07593da9003dae4a3065dd3da85c7a42e67489a64f39f317fd11b4a3980af269450b83fa407538891edde74
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,8 +1,8 @@
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
7
  'unable to loan be_valid validator'
8
8
  nil
@@ -1,3 +1,3 @@
1
1
  module BeValid
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,97 +1,103 @@
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 < ActiveModel::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] && options[:one_of].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.include?(record[field])
77
+ condition_errors << "#{field} = #{record[field]}"
78
+ when Symbol
79
+ return if !record.send(field)&.send(values)
80
+ condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
81
+ else
82
+ return if values != record[field]
83
+ condition_errors << "#{field} = #{record[field]}"
84
+ end
85
+ end
86
+ message << condition_errors.join(' and ')
87
+ end
88
+
89
+ if options[:one_of] && options.fetch(:show_values, true)
90
+ message << ". Valid values: #{options[:one_of].join(', ')}"
91
+ end
92
+ if options[:only_from] && options.fetch(:show_values, true)
93
+ message << ". Valid values: #{options[:only_from].join(', ')}"
94
+ end
95
+ if options[:not_any_of] && options.fetch(:show_values, true)
96
+ message << ". Invalid values: #{options[:not_any_of].join(', ')}"
97
+ end
94
98
 
95
- record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
99
+ record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
100
+ end
101
+ end
96
102
  end
97
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Stewart (johnsinco)
@@ -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
  - - ">="