Class: MiniDefender::Rules::DigitsBetween

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

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

Constructor Details

#initialize(min, max) ⇒ DigitsBetween

Returns a new instance of DigitsBetween.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'lib/mini_defender/rules/digits_between.rb', line 4

def initialize(min, max)
  raise ArgumentError, 'Min must be a positive integer' unless min.is_a?(Integer) && min > 0
  raise ArgumentError, 'Max must be a positive integer' unless max.is_a?(Integer) && max > 0

  @min = min
  @max = max
end

Class Method Details

.make(args) ⇒ Object



16
17
18
# File 'lib/mini_defender/rules/digits_between.rb', line 16

def self.make(args)
  self.new(args[0].to_i, args[1].to_i)
end

.signatureObject



12
13
14
# File 'lib/mini_defender/rules/digits_between.rb', line 12

def self.signature
  'digits_between'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



26
27
28
# File 'lib/mini_defender/rules/digits_between.rb', line 26

def message(attribute, value, validator)
  "The field must contain digits between #{@min} and #{@max}."
end

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

Returns:



20
21
22
23
24
# File 'lib/mini_defender/rules/digits_between.rb', line 20

def passes?(attribute, value, validator)
  value = value&.to_s

  value.is_a?(String) && /\A[0-9]+\z/.match?(value) && value.length >= @min && value.length <= @max
end