Class: Toggl::Worktime::Calendar
- Inherits:
-
Object
- Object
- Toggl::Worktime::Calendar
- Defined in:
- lib/toggl/worktime/calendar.rb
Overview
Worktime Calendar
Defined Under Namespace
Classes: UnknownWeekdayError
Constant Summary collapse
- WEEK =
i[Sun Mon Tue Wed Thu Fri Sat].freeze
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(driver, zone_offset, week_begin, year, month) ⇒ Calendar
constructor
A new instance of Calendar.
- #week ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(driver, zone_offset, week_begin, year, month) ⇒ Calendar
Returns a new instance of Calendar.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/toggl/worktime/calendar.rb', line 11 def initialize(driver, zone_offset, week_begin, year, month) @driver = driver @zone_offset = zone_offset @year = year @month = month today = ::Time.new @days_in_month = Date.new(year, month, -1).day @last_fetch_day = @days_in_month @last_fetch_day = today.day if today.year == year && today.month == month && today.day < @days_in_month @data = nil @week_begin_day = week_begin.to_sym raise UnknownWeekdayError if WEEK.index(@week_begin_day).nil? end |
Instance Method Details
#fetch ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/toggl/worktime/calendar.rb', line 58 def fetch @day_data = [] (1..@days_in_month).each do |day| dateobj = Date.new(@year, @month, day) day_datum = nil if day <= @last_fetch_day @driver.merge!(@year, @month, day) time = @driver.total_time begin_at = @driver.work_time.first[0]&.getlocal(@zone_offset)&.strftime('%T') end_at = @driver.work_time.last[1]&.getlocal(@zone_offset)&.strftime('%T') day_datum = Toggl::Worktime::Day.new(dateobj, time, begin_at, end_at) else day_datum = Toggl::Worktime::Day.new(dateobj, 0, '', '') end @day_data << day_datum end end |
#week ⇒ Object
25 26 27 28 |
# File 'lib/toggl/worktime/calendar.rb', line 25 def week begin_index = WEEK.index(@week_begin_day) WEEK.rotate(begin_index) end |
#write ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/toggl/worktime/calendar.rb', line 30 def write fetch if @data.nil? rotation = week.index(:Sun) table = TTY::Table.new header: week week_data = [] @day_data.each do |datum| wday = datum.day.wday # wday may be rotated wday_index = (wday + rotation) % 7 week_data[wday_index] = datum.format if wday_index == 6 table << week_data week_data = [] end end # last week data may exist unless week_data.length.zero? # Padding (7 - week_data.length).times do week_data << nil end table << week_data end multi_renderer = TTY::Table::Renderer::Unicode.new(table, multiline: true) multi_renderer.border.separator = :each_row puts multi_renderer.render end |