Class: Predicates::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/predicates/number.rb

Overview

Describes an attribute as a number. You may specify boundaries for the number (min, max, or both), and also specify whether it must be an integer.

Options

  • :integer [boolean, default: false] - if the number is an integer (or float/decimal)

  • :above [integer, float] - when the number has a minimum

  • :below [integer, float] - when the number has a maximum

  • :range [range] - when the number has a minimum and a maximum

  • :inclusive [boolean, default: false] - if your maximum or minimum is also an allowed value. Does not work with :range.

  • :at_least [integer, float] - an easy way to say :above and :inclusive

  • :no_more_than [integer, float] - an easy way to say :below and :inclusive

Examples

field_is_a_number :integer => true
field_is_a_number :range => 1..5, :integer => true
field_is_a_number :above => 4.5
field_is_a_number :below => 4.5, :inclusive => true

Instance Attribute Summary collapse

Attributes inherited from Base

#full_message, #or_empty, #validate_if, #validate_on

Instance Method Summary collapse

Methods inherited from Base

#allow_empty?, #error, #initialize, #normalize, #to_human

Constructor Details

This class inherits a constructor from Predicates::Base

Instance Attribute Details

#aboveObject

when the number has a minimum value, but no maximum



22
23
24
# File 'lib/predicates/number.rb', line 22

def above
  @above
end

#belowObject

when the number has a maximum value, but no minimum



25
26
27
# File 'lib/predicates/number.rb', line 25

def below
  @below
end

#inclusiveObject

meant to be used with :above and :below, when you want the endpoint to be inclusive. with the :range option you can just specify inclusion using the standard Ruby range syntax.



32
33
34
# File 'lib/predicates/number.rb', line 32

def inclusive
  @inclusive
end

#integerObject

whether to require an integer value



19
20
21
# File 'lib/predicates/number.rb', line 19

def integer
  @integer
end

#rangeObject

when the number has both a maximum and a minimum value



28
29
30
# File 'lib/predicates/number.rb', line 28

def range
  @range
end

Instance Method Details

#at_least=(val) ⇒ Object



34
35
36
37
# File 'lib/predicates/number.rb', line 34

def at_least=(val)
  self.above = val
  self.inclusive = true
end

#error_bindsObject



48
49
50
51
52
# File 'lib/predicates/number.rb', line 48

def error_binds
  self.range ?
    {:min => self.range.first, :max => self.range.last} :
    {:min => self.above, :max => self.below}
end

#error_messageObject



44
45
46
# File 'lib/predicates/number.rb', line 44

def error_message
  @error_message || range_description
end

#no_more_than=(val) ⇒ Object



39
40
41
42
# File 'lib/predicates/number.rb', line 39

def no_more_than=(val)
  self.below = val
  self.inclusive = true
end

#validate(value, record) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/predicates/number.rb', line 54

def validate(value, record)
  # check data type
  valid = if self.integer
      # if it must be an integer, do a regexp check for digits only
      value.to_s.match(/\A[+-]?[0-9]+\Z/) unless value.is_a? Hash or value.is_a? Array
    else
      # if it can also be a float or decimal, then try a conversion
      begin
        Kernel.Float(value)
        true
      rescue ArgumentError, TypeError
        false
      end
    end

  # if it's the right data type, then also check boundaries
  valid &&= if self.range
      self.range.include? value
    elsif self.above
      operator = self.inclusive ? :>= : :>
      value.send operator, self.above
    elsif self.below
      operator = self.inclusive ? :<= : :<
      value.send operator, self.below
    else
      true
    end

  valid
end