Class: MiniDefender::Rules::ExpiryDate

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/expiry_date.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_date.rb', line 4

def self.signature
  'expiry_date'
end

Instance Method Details

#coerce(value) ⇒ Object



8
9
10
# File 'lib/mini_defender/rules/expiry_date.rb', line 8

def coerce(value)
  "#{@month}/#{@year}"
end

#message(attribute, value, validator) ⇒ Object



25
26
27
# File 'lib/mini_defender/rules/expiry_date.rb', line 25

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

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

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mini_defender/rules/expiry_date.rb', line 12

def passes?(attribute, value, validator)
  return false unless value.is_a?(String)

  matches = /(\d{2})\s*\/\s*(\d{2,4})/.match(value.strip)
  return false unless matches

  @month = matches[1].to_i
  @year = matches[2].to_i
  @year += 2000 if year < 100

  @month >= 1 && @month <= 12 && @year >= 1900
end