Class: MiniDefender::Rules::ExpiryYear

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

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?, make, #priority, #stops?, #with_message

Class Method Details

.signatureObject



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

def self.signature
  'expiry_year'
end

Instance Method Details

#coerce(value) ⇒ Object



8
9
10
11
12
# File 'lib/mini_defender/rules/expiry_year.rb', line 8

def coerce(value)
  value = value.to_i
  value += century if value < 100
  value.to_s
end

#message(attribute, value, validator) ⇒ Object



22
23
24
# File 'lib/mini_defender/rules/expiry_year.rb', line 22

def message(attribute, value, validator)
  'Invalid expiry year.'
end

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

Returns:



14
15
16
17
18
19
20
# File 'lib/mini_defender/rules/expiry_year.rb', line 14

def passes?(attribute, value, validator)
  return false unless value.to_s.match?(/^(\d{2}|\d{4})$/)

  value = value.to_i
  value += century if value < 100
  value >= 1900
end