Class: Wikimovia::Formatters::DateFormatter

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/wikimovia/formatters/date_formatter.rb

Constant Summary collapse

FILM_DATE_PATTERN =
/\{\{film date\|(.*)\}\}/m
YEAR_PATTERN =
/^\d{4}$/

Instance Method Summary collapse

Methods included from Utilities

#extract_pattern, template_regex

Instance Method Details

#format(string) ⇒ Object

Formats a date string based on the following conditions:

  • If it is of the format date|year|month|day, it will return a Date matching those values.

  • If it is a year, it will return the year.

  • Otherwise it returns the string as it was.



14
15
16
17
18
19
20
21
22
23
# File 'lib/wikimovia/formatters/date_formatter.rb', line 14

def format(string)
  case string
  when YEAR_PATTERN then string.to_i
  when FILM_DATE_PATTERN
    string  = extract_pattern(FILM_DATE_PATTERN, string)
    numbers = string.split('|').map(&:to_i)
    Date.new(numbers[0], numbers[1], numbers[2])
  else string
  end
end