usecasing_validations 0.6.0 → 0.7.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NGZlYjIzZGQ3NGY5ZmRhNmE2ZDA1OWI4ZWRlYzYyYjMxMTFkYWMwZA==
4
+ ZGE4NWNhYmNkOWUyZmUxNTRlNWIzZDJjMWJjMTdiN2NkZmQyYjU1NQ==
5
5
  data.tar.gz: !binary |-
6
- MTM2MThiMWI4ZWVlOTNiMjFmYzliZDNiM2E0YTNlMGZlYTkzMTJiZg==
6
+ MzFmZDVjMDA0NzZjNDQwZjY4ZjE1NTI5ZTRhZTkzZDFiODdkNThjMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MmU1OWQzZjAzMGRlNGEwYTE5NWZhYTM4MTI3YTc4OGQxNDU4OGJkOWM4MTc1
10
- NTQ5OGFhNTQwNDBmMWQ2ZjcxMGIzZTc3ZGRhZDc3YmRkMmMzMDU1MTQ4ODMw
11
- YTU3NTkyMGJjMWM0NzVhYzEzY2Y3ZmIyODgwN2I2ZDJhYTA4YjM=
9
+ YjYyYmExN2QwNjVjOTk4OTkwMmMwNDc2MDY2MTJhODRjNjVkNWVjNGUyMDQx
10
+ ZTBmYjU3N2JkM2JlM2NiMmYzNWQyOWQ2Y2RmODBiM2M0YzU4ODYwOWI2Yjhl
11
+ ZjdlMmI1Y2RlOTU0ZmNhZjU4NTBjMGU1NGU4OGFkZDE0ZjFhNzI=
12
12
  data.tar.gz: !binary |-
13
- Y2UyYTdkZjVmMzIxM2I3ZmQzNTMwNDcxOTY1NDlkZjU5NTdlNzJmMDQ3YTBm
14
- NjUxZGJlNTZlYzBlMmQyNWEwZWYwNGIzYjAzY2YxZWMwOWQ4NTJlMDM0MGRm
15
- YjEyZjRkNjE3MGViMDc4OWVhNDE2YmJjOTk0ZmRiOTlkZWU3M2Y=
13
+ ODA2YWFmNDcxYWRlZjZhZjE2NzNhMTgwZjc2OGEwZDkwYjRjMDBiY2VjZDM4
14
+ ZjkxNThmYmFmNTRjZTFkMzY1NjA2MmFhMzdhNDdkMTY4MGU3YjBlOGI5ODQ5
15
+ NmI2ZTczNjllMmIzYTE4YzExODk4NDJmNDUyMTQyMGZhOWQ4ZTI=
@@ -12,6 +12,7 @@ require "usecasing_validations/validations/format"
12
12
  require "usecasing_validations/validations/length"
13
13
  require "usecasing_validations/validations/presence"
14
14
  require "usecasing_validations/validations/uniqueness"
15
+ require "usecasing_validations/validations/numericality"
15
16
 
16
17
 
17
18
  module UseCase
@@ -61,6 +61,12 @@ module UseCaseValidations
61
61
  _hash
62
62
  end
63
63
 
64
+ def _slice(hash, *keys)
65
+ _hash = {}
66
+ keys.each { |key| _hash[key] = hash[key] }
67
+ _hash
68
+ end
69
+
64
70
  def _call_proc_or_method(base, proc_or_method, object = nil)
65
71
  if object.nil?
66
72
  proc_or_method.is_a?(Proc) ? base.instance_exec(&proc_or_method) : base.send(proc_or_method)
@@ -0,0 +1,89 @@
1
+ module UseCaseValidations
2
+ # == Active Model Numericality Validator
3
+ module Validations
4
+ class NumericalityValidator < EachValidator
5
+ CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=,
6
+ :equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
7
+ :odd => :odd?, :even => :even? }.freeze
8
+
9
+ RESERVED_OPTIONS = CHECKS.keys + [:only_integer]
10
+
11
+ def check_validity!
12
+ keys = CHECKS.keys - [:odd, :even]
13
+
14
+ Helpers._slice(options, *keys).each do |option, value|
15
+ next if value.nil? || value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol)
16
+ raise ArgumentError, ":#{option} must be a number, a symbol or a proc"
17
+ end
18
+ end
19
+
20
+ def validate_each(record, attr_name, value)
21
+ before_type_cast = "#{attr_name}_before_type_cast"
22
+
23
+ raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast.to_sym)
24
+ raw_value ||= value
25
+
26
+ return if options[:allow_nil] && raw_value.nil?
27
+
28
+ unless value = parse_raw_value_as_a_number(raw_value)
29
+ record.errors.add(attr_name, :not_a_number, filtered_options(raw_value))
30
+ return
31
+ end
32
+
33
+ if options[:only_integer]
34
+ unless value = parse_raw_value_as_an_integer(raw_value)
35
+ record.errors.add(attr_name, :not_an_integer, filtered_options(raw_value))
36
+ return
37
+ end
38
+ end
39
+
40
+ Helpers._slice(options, *CHECKS.keys).each do |option, option_value|
41
+ next if option_value.nil?
42
+
43
+ case option
44
+ when :odd, :even
45
+ unless value.to_i.send(CHECKS[option])
46
+ record.errors.add(attr_name, option, filtered_options(value))
47
+ end
48
+ else
49
+ option_value = option_value.call(record) if option_value.is_a?(Proc)
50
+ option_value = record.send(option_value) if option_value.is_a?(Symbol)
51
+
52
+ unless value.send(CHECKS[option], option_value)
53
+ record.errors.add(attr_name, option, filtered_options(value).merge(:count => option_value))
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ protected
60
+
61
+ def parse_raw_value_as_a_number(raw_value)
62
+ case raw_value
63
+ when /\A0[xX]/
64
+ nil
65
+ else
66
+ begin
67
+ Kernel.Float(raw_value)
68
+ rescue ArgumentError, TypeError
69
+ nil
70
+ end
71
+ end
72
+ end
73
+
74
+ def parse_raw_value_as_an_integer(raw_value)
75
+ raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/
76
+ end
77
+
78
+ def filtered_options(value)
79
+ Helpers._except(options, *RESERVED_OPTIONS).merge!(:value => value)
80
+ end
81
+ end
82
+
83
+ module HelperMethods
84
+ def validates_numericality_of(*attr_names)
85
+ validates_with NumericalityValidator, _merge_attributes(attr_names)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,3 +1,3 @@
1
1
  module UseCaseValidations
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -5,10 +5,11 @@ class ValidatePost < UseCase::Validator
5
5
  validates_presence_of :title, :body, message: "can't be blank!"
6
6
 
7
7
  validates_presence_of :phone_number, if: ->(post) { context.validate_phone_number }
8
-
8
+
9
9
  validates_format_of :phone_number, with: /\A[0-9 ]*\z/, message: "invalid format!", if: :validate_phone_number
10
10
 
11
-
11
+ validates_numericality_of :phone_number, greater_than: 10
12
+
12
13
  protected ###################### PROTECTED ####################
13
14
 
14
15
  def validate_phone_number(post)
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ValidatePostClearErrors do
3
+ describe ValidatePostClearErrors do
4
4
 
5
5
  it "#clean_Errors! will force the #valid? method to clear the target's errors at the start" do
6
6
 
@@ -28,19 +28,22 @@ describe ValidatePostClearErrors do
28
28
 
29
29
  it "By default a Validator Class should not clean the target's errors" do
30
30
 
31
- post = RubyPost.new(body: 'body')
31
+ post = RubyPost.new(body: 'body', phone_number: 10)
32
32
  context = ValidatePost.perform(post: post)
33
33
  context.success?.should == false
34
34
  post.errors.added?(:title, "can't be blank!").should == true
35
- post.errors.size.should == 1
35
+ post.errors.added?(:phone_number, :greater_than).should == true
36
+ post.errors.size.should == 2
36
37
 
37
38
  post.title = 'title'
38
39
  post.body = ''
40
+ post.phone_number = 11
39
41
  context = ValidatePost.perform(post: post)
40
42
  context.success?.should == false
41
43
  post.errors.added?(:title, "can't be blank!").should == true
42
44
  post.errors.added?(:body, "can't be blank!").should == true
43
- post.errors.size.should == 2
45
+ post.errors.added?(:phone_number, :greater_than).should == true
46
+ post.errors.size.should == 3
44
47
 
45
48
  end
46
49
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usecasing_validations
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
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-20 00:00:00.000000000 Z
11
+ date: 2015-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: usecasing
@@ -51,6 +51,7 @@ files:
51
51
  - lib/usecasing_validations/validations/format.rb
52
52
  - lib/usecasing_validations/validations/helper_methods.rb
53
53
  - lib/usecasing_validations/validations/length.rb
54
+ - lib/usecasing_validations/validations/numericality.rb
54
55
  - lib/usecasing_validations/validations/presence.rb
55
56
  - lib/usecasing_validations/validations/uniqueness.rb
56
57
  - lib/usecasing_validations/validator.rb