Class: Ddate::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ddate/formatter.rb

Constant Summary collapse

STTIBS_RE =
%r|%{(.*?)%}|
SPLIT_STOP =
'%%N'
DEFAULT_FORMAT =
'%{%A, %B the %e%}, YOLD %Y.%N Celebrate %H!'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = nil) ⇒ Formatter

Returns a new instance of Formatter.



12
13
14
15
16
# File 'lib/ddate/formatter.rb', line 12

def initialize(format = nil)
  format = DEFAULT_FORMAT if format.to_s.empty?
  @standard = create_format_string_from(format.gsub(STTIBS_RE){$1})
  @sttibs = create_format_string_from(format.gsub(STTIBS_RE, '%{st_tibs_day}'))
end

Instance Attribute Details

#standardObject (readonly)

Returns the value of attribute standard.



10
11
12
# File 'lib/ddate/formatter.rb', line 10

def standard
  @standard
end

#sttibsObject (readonly)

Returns the value of attribute sttibs.



10
11
12
# File 'lib/ddate/formatter.rb', line 10

def sttibs
  @sttibs
end

Instance Method Details

#create_format_string_from(format_string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ddate/formatter.rb', line 18

def create_format_string_from(format_string)
  format_string.gsub(/%[AaBbdeHNntY.X]/, {
      '%A' => '%{dayname}', '%a' => '%{dayname_short}',
      '%B' => '%{seasonname}', '%b' => '%{seasonname_short}',
      '%d' => '%{day}', '%e' => '%{dayth}',
      '%H' => '%{holiday}', '%N' => SPLIT_STOP,
      '%n' => "\n", '%t' => "\t",
      '%Y' => '%{year}', '%.' => '%{exclaim}',
      '%X' => '%{kill_bob}',
  } )

end

#split(key) ⇒ Object



31
32
33
# File 'lib/ddate/formatter.rb', line 31

def split(key)
  send(key).split(SPLIT_STOP, 2)
end