Class: Kiss::Format::MonthYear

Inherits:
Date show all
Defined in:
lib/kiss/format.rb

Class Method Summary collapse

Methods inherited from DateTime

convert_value_local_to_utc, convert_value_utc_to_local, validate

Methods inherited from Kiss::Format

inherited, lookup, validate

Class Method Details

.parse(value, context = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/kiss/format.rb', line 180

def parse(value, context = {})
  month, year = value.sub(/\A\s*/, '').sub(/\s*\Z/, '').split(/\D+/)
  # convert two-digit years to four-digit years
  year = year.to_i
  if year < 100
    year += 1900
    year += 100 if year < ::Time.now.year - 95
  end
  begin
    ::Time.parse("#{month}/#{year}")
  rescue ArgumentError => e
    raise Kiss::Format::ValidateError, e.message
  end
end

.value_to_s(value, context = {}) ⇒ Object



195
196
197
# File 'lib/kiss/format.rb', line 195

def value_to_s(value, context = {})
  value.strftime("%m/%Y")
end