simple_validate 2.1.2 → 2.2.2

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: 1ac8df574ea6749497a31f423b343ddbb5e7f444a3d5d8ab60d57a3faaa594cf
4
- data.tar.gz: 0122ded6d697df547de47dc9a47556f8ecffacfe79369675558ad9714c1565fc
3
+ metadata.gz: 628e641750b3b4b70e0b5c1e9dfe71d96a328f21ecc84120384d49c8e9016e4f
4
+ data.tar.gz: dbbbaf33a3f1e2597a2e621ae571715d7b02c20556aadc38c36330d24d458662
5
5
  SHA512:
6
- metadata.gz: 8391463f35b4ae5e04c7025941d8a85d0533b69eac371c3b85c8e5e437dd6cdcf7d8c7d7203cc54a8acf00bba2da126a3218fe4eb65642529c2c069fe21254a6
7
- data.tar.gz: 405ea7f377be0e0f561f8c597dd7c5dc6ac1712663421999014f810a521e85b21224458386154e7e8e9529dc97ba198cd2a463105553ad5e335561bce0a52e35
6
+ metadata.gz: c27471e667d574519a90093a454a154908298f2a3667bac826122453582e1d3401e466401e10b7308e436f4e47c8b8de408fb69290cf77b2d1acb0658a0c752d
7
+ data.tar.gz: d4613f09b11db76d5c1406a012b8073ac384a9121f6fa99208ef809bffcfb5dabc60589b4fcf17d9d5070867e618eb18c34ad5427ec045d93d2300c76645300b
data/.rspec_status CHANGED
@@ -1,8 +1,28 @@
1
- example_id | status | run_time |
2
- ------------------------------------- | ------ | --------------- |
3
- ./spec/simple_validate_spec.rb[1:1:1] | passed | 0.00132 seconds |
4
- ./spec/simple_validate_spec.rb[1:2:1] | passed | 0.00012 seconds |
5
- ./spec/simple_validate_spec.rb[1:3:1] | passed | 0.00108 seconds |
6
- ./spec/simple_validate_spec.rb[1:4:1] | passed | 0.00007 seconds |
7
- ./spec/simple_validate_spec.rb[1:5:1] | passed | 0.00006 seconds |
8
- ./spec/simple_validate_spec.rb[1:6:1] | passed | 0.00007 seconds |
1
+ example_id | status | run_time |
2
+ -------------------------------------------- | ------ | --------------- |
3
+ ./spec/simple_validate_spec.rb[1:1:1] | passed | 0.00128 seconds |
4
+ ./spec/simple_validate_spec.rb[1:2:1] | passed | 0.00008 seconds |
5
+ ./spec/validators/exclusion_spec.rb[1:1:1] | passed | 0.00005 seconds |
6
+ ./spec/validators/exclusion_spec.rb[1:2:1:1] | passed | 0.00006 seconds |
7
+ ./spec/validators/exclusion_spec.rb[1:2:2:1] | passed | 0.00005 seconds |
8
+ ./spec/validators/inclusion_spec.rb[1:1:1] | passed | 0.00004 seconds |
9
+ ./spec/validators/inclusion_spec.rb[1:2:1:1] | passed | 0.00005 seconds |
10
+ ./spec/validators/inclusion_spec.rb[1:2:2:1] | passed | 0.00005 seconds |
11
+ ./spec/validators/length_spec.rb[1:1:1] | passed | 0.00007 seconds |
12
+ ./spec/validators/length_spec.rb[1:2:1] | passed | 0.00007 seconds |
13
+ ./spec/validators/length_spec.rb[1:3:1] | passed | 0.00007 seconds |
14
+ ./spec/validators/length_spec.rb[1:4:1:1] | passed | 0.00008 seconds |
15
+ ./spec/validators/length_spec.rb[1:4:2:1] | passed | 0.00007 seconds |
16
+ ./spec/validators/length_spec.rb[1:5:1:1] | passed | 0.00005 seconds |
17
+ ./spec/validators/length_spec.rb[1:5:2:1] | passed | 0.00006 seconds |
18
+ ./spec/validators/length_spec.rb[1:6:1] | passed | 0.00005 seconds |
19
+ ./spec/validators/presence_spec.rb[1:1:1] | passed | 0.00006 seconds |
20
+ ./spec/validators/type_spec.rb[1:1:1] | passed | 0.00005 seconds |
21
+ ./spec/validators/type_spec.rb[1:2:1:1] | passed | 0.00006 seconds |
22
+ ./spec/validators/type_spec.rb[1:2:2:1] | passed | 0.00006 seconds |
23
+ ./spec/validators/type_spec.rb[1:3:1:1] | passed | 0.00005 seconds |
24
+ ./spec/validators/type_spec.rb[1:3:2:1] | passed | 0.00006 seconds |
25
+ ./spec/validators/type_spec.rb[1:4:1:1] | passed | 0.00006 seconds |
26
+ ./spec/validators/type_spec.rb[1:4:2:1] | passed | 0.00006 seconds |
27
+ ./spec/validators/type_spec.rb[1:5:1:1] | passed | 0.00005 seconds |
28
+ ./spec/validators/type_spec.rb[1:5:2:1] | passed | 0.00006 seconds |
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- class ValidatesExclusionOf < ValidatesSetBase
4
+ class ValidatesExclusionOf < ValidatesBase
5
+ def initialize(attribute, options)
6
+ @set = options[:in]
7
+ @allow_nil = options[:allow_nil]
8
+
9
+ raise ArgumentError unless [Array, Range].include?(@set.class)
10
+
11
+ super(attribute, options[:message] ||
12
+ "breaks exclusion rules", options[:if] || proc { true })
13
+ end
14
+
5
15
  def valid?(instance)
6
- raise ArgumentError if set.empty? || !options.fetch(:in).is_a?(Array)
16
+ val = instance.send(attribute)
17
+
18
+ return true if val.nil? && @allow_nil == true
7
19
 
8
- !set.include?(instance.send(attribute).to_s)
20
+ !@set.to_a.include?(val)
9
21
  end
10
22
  end
11
23
  end
@@ -5,15 +5,22 @@ module SimpleValidate
5
5
  attr_accessor :regex
6
6
 
7
7
  def initialize(attribute, options)
8
+ @allow_nil = options[:allow_nil]
8
9
  @regex = options[:with]
9
- super(attribute, options[:message] ||
10
- "is incorrect format", options[:if] || proc { true })
10
+ super(
11
+ attribute,
12
+ options[:message] || "is incorrect format",
13
+ options[:if] || proc { true }
14
+ )
11
15
  end
12
16
 
13
17
  def valid?(instance)
18
+ val = instance.send(attribute)
19
+
20
+ return true if val.nil? && @allow_nil == true
14
21
  raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)
15
22
 
16
- !!(instance.send(attribute) =~ regex)
23
+ !!(val =~ regex)
17
24
  end
18
25
  end
19
26
  end
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- class ValidatesInclusionOf < ValidatesSetBase
4
+ class ValidatesInclusionOf < ValidatesBase
5
+ def initialize(attribute, options)
6
+ @set = options[:in]
7
+ @allow_nil = options[:allow_nil]
8
+
9
+ raise ArgumentError unless [Array, Range].include?(@set.class)
10
+
11
+ super(attribute, options[:message] ||
12
+ "breaks inclusion rules", options[:if] || proc { true })
13
+ end
14
+
5
15
  def valid?(instance)
6
- raise ArgumentError if set.empty? || !options.fetch(:in).is_a?(Array)
16
+ val = instance.send(attribute)
17
+
18
+ return true if val.nil? && @allow_nil == true
7
19
 
8
- set.include?(instance.send(attribute).to_s)
20
+ @set.to_a.include?(val)
9
21
  end
10
22
  end
11
23
  end
@@ -2,58 +2,49 @@
2
2
 
3
3
  module SimpleValidate
4
4
  class ValidatesLengthOf < ValidatesBase
5
- class InvalidLengthOption < ArgumentError; end
6
- attr_reader :attribute
7
- attr_accessor :options
8
-
9
- VALID_LENGTH_OPTIONS = %i[maximum minimum in is].freeze
5
+ VALIDATORS = %i[maximum minimum in is].freeze
10
6
 
11
7
  def initialize(attribute, options)
12
- super(attribute, options.delete(:message), options.delete(:if) ||
13
- proc { true })
14
- self.options = options
15
- end
8
+ @validator = options.keys.first
16
9
 
17
- def message
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"
25
- end
26
- end
10
+ raise ArgumentError, "Invalid length option" unless VALIDATORS.include?(@validator)
27
11
 
28
- def validator
29
- options.keys.first
12
+ @valid_length = options[@validator]
13
+ @allow_nil = options[:allow_nil]
14
+
15
+ super(attribute, options[:message], options[:if] || proc { true })
30
16
  end
31
17
 
32
- def valid_length
33
- options.values.first
18
+ def message
19
+ @message = case @validator
20
+ when :minimum
21
+ "is too short"
22
+ when :maximum
23
+ "is too long"
24
+ else
25
+ "is not valid length"
26
+ end
34
27
  end
35
28
 
36
29
  def valid_length?(actual_length)
37
- case validator
30
+ case @validator
38
31
  when :minimum
39
- actual_length >= valid_length
32
+ actual_length >= @valid_length
40
33
  when :maximum
41
- actual_length <= valid_length
34
+ actual_length <= @valid_length
42
35
  when :in
43
- valid_length.member?(actual_length)
36
+ @valid_length.member?(actual_length)
44
37
  when :is
45
- actual_length == valid_length
38
+ actual_length == @valid_length
46
39
  end
47
40
  end
48
41
 
49
42
  def valid?(instance)
50
- raise ArgumentError, "Only one length argument can be provided" if options.keys.size > 1
43
+ val = instance.send(attribute)
51
44
 
52
- unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
53
- raise InvalidLengthOption, "Invalid length option given #{options.keys}"
54
- end
45
+ return true if val.nil? && @allow_nil == true
55
46
 
56
- actual_length = instance.send(attribute)&.length
47
+ actual_length = val&.length
57
48
  valid_length?(actual_length)
58
49
  end
59
50
  end
@@ -5,6 +5,7 @@ module SimpleValidate
5
5
  SUPPORTED_TYPES = %i[string integer float boolean].freeze
6
6
 
7
7
  def initialize(attribute, options)
8
+ @allow_nil = options[:allow_nil]
8
9
  @type = options[:as]
9
10
 
10
11
  raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)
@@ -14,11 +15,15 @@ module SimpleValidate
14
15
  end
15
16
 
16
17
  def valid?(instance)
18
+ val = instance.send(attribute)
19
+
20
+ return true if val.nil? && @allow_nil == true
21
+
17
22
  if @type == :boolean
18
- [true, false].include? instance.send(attribute)
23
+ [true, false].include? val
19
24
  else
20
25
  klass = Utils.classify(@type)
21
- instance.send(attribute).is_a?(klass)
26
+ val.is_a?(klass)
22
27
  end
23
28
  end
24
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- VERSION = "2.1.2"
4
+ VERSION = "2.2.2"
5
5
  end
@@ -5,7 +5,6 @@
5
5
  format_of
6
6
  type_of
7
7
  length_of
8
- set_base
9
8
  inclusion_of
10
9
  exclusion_of].each do |validation|
11
10
  require "simple_validate/validates_#{validation}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
@@ -36,7 +36,6 @@ files:
36
36
  - lib/simple_validate/validates_inclusion_of.rb
37
37
  - lib/simple_validate/validates_length_of.rb
38
38
  - lib/simple_validate/validates_presence_of.rb
39
- - lib/simple_validate/validates_set_base.rb
40
39
  - lib/simple_validate/validates_type_of.rb
41
40
  - lib/simple_validate/version.rb
42
41
  - simple_validate.gemspec
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleValidate
4
- class ValidatesSetBase < ValidatesBase
5
- attr_accessor :set, :options
6
-
7
- def initialize(attribute, options)
8
- self.options = options
9
- self.set = Set.new(Array(options[:in]).map(&:to_s))
10
- super(attribute, options[:message] ||
11
- "breaks inclusion/exclusion rules", options[:if] || proc { true })
12
- end
13
- end
14
- end