Class: Aequitas::Rule::Numericalness::NonInteger

Inherits:
Aequitas::Rule::Numericalness show all
Defined in:
lib/aequitas/rule/numericalness/non_integer.rb

Instance Attribute Summary collapse

Attributes inherited from Aequitas::Rule

#attribute_name, #custom_message, #guard, #skip_condition

Attributes included from ValueObject

#equalizer

Instance Method Summary collapse

Methods inherited from Aequitas::Rule::Numericalness

rules_for, #valid?, #valid_numericalness?, #value_as_string

Methods inherited from Aequitas::Rule

#attribute_value, #execute?, rules_for, #skip?, #validate, #violation_data, #violation_info, #violation_values

Methods included from ValueObject

#equalize_on

Constructor Details

#initialize(attribute_name, options) ⇒ NonInteger

Returns a new instance of NonInteger.



13
14
15
16
17
18
19
20
21
22
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 13

def initialize(attribute_name, options)
  super

  @precision = options.fetch(:precision, nil)
  @scale     = options.fetch(:scale,     nil)

  unless expected # validate precision and scale attrs
    raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{attribute_name}"
  end
end

Instance Attribute Details

#precisionObject (readonly)

Returns the value of attribute precision.



10
11
12
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 10

def precision
  @precision
end

#scaleObject (readonly)

Returns the value of attribute scale.



11
12
13
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 11

def scale
  @scale
end

Instance Method Details

#expected(precision = @precision, scale = @scale) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 24

def expected(precision = @precision, scale = @scale)
  if precision && scale
    if precision > scale && scale == 0
      /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
    elsif precision > scale
      delta = precision - scale
      /\A[+-]?(?:\d{1,#{delta}}|\d{0,#{delta}}\.\d{1,#{scale}})\z/
    elsif precision == scale
      /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
    else
      nil
    end
  else
    /\A[+-]?(?:\d+|\d*\.\d+)\z/
  end
end

#violation_type(resource) ⇒ Object



41
42
43
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 41

def violation_type(resource)
  :not_a_number
end