Class: PrintableCalendar::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/printable_calendar/range.rb

Class Method Summary collapse

Class Method Details

.compute(period, startingFrom) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/printable_calendar/range.rb', line 22

def self.compute(period, startingFrom)
  t = startingFrom

  mapper = range_map[period.to_sym]

  abort "Unknown period #{period}. Use one of {#{supported_periods.join(", ")}}" unless mapper

  s, e = mapper.(t)
  [s.beginning_of_day, e.end_of_day]
end

.range_mapObject



8
9
10
11
12
13
14
15
16
# File 'lib/printable_calendar/range.rb', line 8

def self.range_map
  {
    workweek: ->(t){[t.beginning_of_week(:monday), t.beginning_of_week(:monday) + 4]},
    american_week: ->(t){[t.beginning_of_week(:sunday), t.end_of_week(:sunday)]},
    intl_week: ->(t){[t.beginning_of_week(:monday), t.end_of_week(:sunday)]},
    month: ->(t){[t.beginning_of_month, t.end_of_month]},
    day: ->(t){[t, t]}
  }
end

.supported_periodsObject



18
19
20
# File 'lib/printable_calendar/range.rb', line 18

def self.supported_periods
  range_map.keys.map{|k| k.to_s}
end