Class: ExampleFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/date_by_example/example_formatter.rb

Constant Summary collapse

FORMATS =
{
".000000000" => ".9N",
"-07:00:00" => "%::z",
".000000" => ".%6N",
"January" => "%B",
"JANUARY" => "%^B",
"Monday" => "%A",
"MONDAY" => "%^A",
"-07:00" => "%:z",
"-7000" => "%z",
"2006" => "%Y",
".000" => ".%L",
"002" => "%j",
"Jan" => "%b",
"JAN" => "%^b",
"Mon" => "%a",
"MON" => "%^a",
"MST" => "%Z",
"15" => "%H",
"06" => "%y",
"01" => "%m",
"02" => "%d",
"03" => "%I",
"PM" => "%p",
"pm" => "%P",
"04" => "%M",
"05" => "%S",
"1" => "%-m",
"2" => "%-e"}.freeze
FORMAT_MATCHER =
Regexp.union(FORMATS.keys)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference) ⇒ ExampleFormatter

Returns a new instance of ExampleFormatter.



4
5
6
7
# File 'lib/date_by_example/example_formatter.rb', line 4

def initialize(reference)
  @reference = reference
  @format_string = nil
end

Instance Attribute Details

#referenceObject

Returns the value of attribute reference.



2
3
4
# File 'lib/date_by_example/example_formatter.rb', line 2

def reference
  @reference
end

Instance Method Details

#format(date) ⇒ Object



45
46
47
# File 'lib/date_by_example/example_formatter.rb', line 45

def format(date)
  date.strftime(format_string)
end

#format_stringObject



41
42
43
# File 'lib/date_by_example/example_formatter.rb', line 41

def format_string
  @format_string ||= reference.gsub(FORMAT_MATCHER, FORMATS)
end