Class: MiniDefender::Rules::Digits

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/digits.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(size) ⇒ Digits

Returns a new instance of Digits.

Raises:

  • (ArgumentError)


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

def initialize(size)
  raise ArgumentError, 'Size must be a positive integer' unless size.is_a?(Integer) && size > 0

  @size = size
end

Class Method Details

.make(args) ⇒ Object



14
15
16
# File 'lib/mini_defender/rules/digits.rb', line 14

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

.signatureObject



10
11
12
# File 'lib/mini_defender/rules/digits.rb', line 10

def self.signature
  'digits'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



23
24
25
# File 'lib/mini_defender/rules/digits.rb', line 23

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

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

Returns:



18
19
20
21
# File 'lib/mini_defender/rules/digits.rb', line 18

def passes?(attribute, value, validator)
  value = value&.to_s
  value.is_a?(String) && /\A[0-9]+\z/.match?(value) && value.length == @size
end