Class: Melitta::Coercers::MonthYear

Inherits:
Struct
  • Object
show all
Defined in:
lib/melitta/coercers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inputObject

Returns the value of attribute input

Returns:

  • (Object)

    the current value of input



95
96
97
# File 'lib/melitta/coercers.rb', line 95

def input
  @input
end

Instance Method Details

#outputObject



103
104
105
106
107
108
# File 'lib/melitta/coercers.rb', line 103

def output
  month = input.scan(/\b(?:0?[1-9]|1[012])\b/)[0].to_i
  year  = input.scan(/\b\d{4}\b/)[0].to_i

  ::Date.new(year, month)
end

#valid?Boolean

valid formats are monthyear or yearmonth year has to be 4 digits to force unambiguity

Returns:



99
100
101
# File 'lib/melitta/coercers.rb', line 99

def valid?
  !! /\A((?:\d{4}[-.\/](0?[1-9]|1[012]))|(?:(0?[1-9]|1[012])[-.\/]\d{4}))\z/.match(input)
end