Class: MiniDefender::Rules::Integer

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/integer.rb

Constant Summary collapse

DIGIT_MAP =
{
  # Arabic-Indic Digits
  "\u0660" => '0',
  "\u0661" => '1',
  "\u0662" => '2',
  "\u0663" => '3',
  "\u0664" => '4',
  "\u0665" => '5',
  "\u0666" => '6',
  "\u0667" => '7',
  "\u0668" => '8',
  "\u0669" => '9',

  # Extended Arabic-Indic Digits
  "\u06F0" => '0',
  "\u06F1" => '1',
  "\u06F2" => '2',
  "\u06F3" => '3',
  "\u06F4" => '4',
  "\u06F5" => '5',
  "\u06F6" => '6',
  "\u06F7" => '7',
  "\u06F8" => '8',
  "\u06F9" => '9',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(mode = 'strict') ⇒ Integer

Returns a new instance of Integer.



33
34
35
# File 'lib/mini_defender/rules/integer.rb', line 33

def initialize(mode = 'strict')
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/mini_defender/rules/integer.rb', line 4

def mode
  @mode
end

#parsedObject (readonly)

Returns the value of attribute parsed.



5
6
7
# File 'lib/mini_defender/rules/integer.rb', line 5

def parsed
  @parsed
end

Class Method Details

.make(args) ⇒ Object



41
42
43
# File 'lib/mini_defender/rules/integer.rb', line 41

def self.make(args)
  new(args[0] || 'strict')
end

.signatureObject



37
38
39
# File 'lib/mini_defender/rules/integer.rb', line 37

def self.signature
  'integer'
end

Instance Method Details

#coerce(value) ⇒ Object



45
46
47
# File 'lib/mini_defender/rules/integer.rb', line 45

def coerce(value)
  @parsed
end

#message(attribute, value, validator) ⇒ Object



71
72
73
# File 'lib/mini_defender/rules/integer.rb', line 71

def message(attribute, value, validator)
  "The value must be an integer."
end

#normalize_digits(data) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mini_defender/rules/integer.rb', line 75

def normalize_digits(data)
  # Check if arabic digits exist or avoid expensive string creation operation
  unless data.match?(/[\u0660-\u0669\u06F0-\u06F9]/)
    return data
  end

  DIGIT_MAP.each do |k, v|
    data = data.gsub(k, v)
  end

  data
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mini_defender/rules/integer.rb', line 49

def passes?(attribute, value, validator)
  # Avoid converting integers to string and back
  if value.is_a?(Integer)
    @parsed = value
    return true
  end

  # Remove leading zero so Integer will not treat it as octal
  # Handle leading zeros while preserving both + and - signs
  value = value
    .to_s
    .gsub(/^([+-])?0+(?=\d)/, '\1')

  if @mode == 'relaxed'
    value = normalize_digits(value)
  end

  @parsed = Integer(value)
rescue
  false
end