Class: Printer

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

Overview

Printer for work log entries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(people = nil) ⇒ Printer

Initializes the printer with a list of people.

Parameters:

  • people (Array<Person>) (defaults to: nil)

    An array of Person objects.



11
12
13
# File 'lib/printer.rb', line 11

def initialize(people = nil)
  @people = people || {}
end

Instance Attribute Details

#peopleObject (readonly)

Returns the value of attribute people.



7
8
9
# File 'lib/printer.rb', line 7

def people
  @people
end

Instance Method Details

#no_entries(start_date, end_date) ⇒ void

This method returns an undefined value.

Print a message when no entries are found.

Parameters:

  • start_date (Date)
  • end_date (Date)


45
46
47
48
49
50
51
52
53
54
# File 'lib/printer.rb', line 45

def no_entries(start_date, end_date)
  if start_date == end_date
    date_string = start_date.strftime('%a, %B %-d, %Y')
    puts "No entries found for #{Rainbow(date_string).gold}."
  else
    start_date_string = start_date.strftime('%a, %B %-d, %Y')
    end_date_string = end_date.strftime('%a, %B %-d, %Y')
    puts "No entries found between #{Rainbow(start_date_string).gold} and #{Rainbow(end_date_string).gold}."
  end
end

This method returns an undefined value.

Prints a whole day of work log entries. If date_inline is true, the date is printed inline with the time. If epics_only is true, only epic entries are printed.

Examples:

printer.print_day(daily_log, date_inline: true, epics_only: false, project: 'xyz')
printer.print_day(daily_log, date_inline: false, epics_only: true)

Parameters:

  • daily_log (DailyLog)

    The daily log containing entries.

  • date_inline (Boolean) (defaults to: false)

    If true, the date is printed inline with the time.

  • epics_only (Boolean) (defaults to: false)

    If true, only epic entries are printed.

  • filter (Hash) (defaults to: {})

    A hash of filters to apply to the entries.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/printer.rb', line 26

def print_day(daily_log, date_inline = false, epics_only = false, filter = {})
  daily_log.date = Date.strptime(daily_log.date, '%Y-%m-%d') unless daily_log.date.respond_to?(:strftime)

  date_string = daily_log.date.strftime('%a, %B %-d, %Y')
  puts "Work log for #{Rainbow(date_string).gold}" unless date_inline

  daily_log.entries.each do |entry|
    next if epics_only && !entry.epic?

    next unless filter.compact.all? { |k, v| entry.send(k) == v }

    print_entry(daily_log, entry, date_inline)
  end
end

#print_entry(daily_log, entry, date_inline = false) ⇒ Object

Prints a single entry, formats the date and time.

Parameters:

  • daily_log (DailyLog)
  • entry (LogEntry)
  • date_inline (Boolean) (defaults to: false)

    If true, the date is printed inline with the time.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/printer.rb', line 60

def print_entry(daily_log, entry, date_inline = false)
  entry.time = DateTime.strptime(entry.time, '%H:%M:%S') unless entry.time.respond_to?(:strftime)

  time_string = if date_inline
                  "#{daily_log.date.strftime('%a, %Y-%m-%d')} #{entry.time.strftime('%H:%M')}"
                else
                  entry.time.strftime('%H:%M')
                end

  puts "#{Rainbow(time_string).gold} #{entry.message_string(@people)}"
end