Class: TimeBoss::Calendar::Support::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/timeboss/calendar/support/formatter.rb

Overview

The formatter is responsible for the implementation of name formatting for a unit.

Constant Summary collapse

PERIODS =
Translatable::PERIODS.reverse.map(&:to_sym).drop(1)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, periods) ⇒ Formatter



15
16
17
18
19
# File 'lib/timeboss/calendar/support/formatter.rb', line 15

def initialize(unit, periods)
  @unit = unit
  @periods = PERIODS & periods.map(&:to_sym).push(unit.class.type.to_sym)
  @periods -= [:week] unless unit.calendar.supports_weeks?
end

Instance Attribute Details

#periodsObject (readonly)

Returns the value of attribute periods.



13
14
15
# File 'lib/timeboss/calendar/support/formatter.rb', line 13

def periods
  @periods
end

#unitObject (readonly)

Returns the value of attribute unit.



13
14
15
# File 'lib/timeboss/calendar/support/formatter.rb', line 13

def unit
  @unit
end

Instance Method Details

#to_sObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/timeboss/calendar/support/formatter.rb', line 21

def to_s
  base, text = "year", unit.year.name
  periods.each do |period|
    (sub = unit.public_send(period)) || break
    index = sub.public_send("in_#{base}")
    text += "#{period[0].upcase}#{index}"
    base = period
  end
  text
end