Class: Worklog::DailyLog

Inherits:
Object
  • Object
show all
Includes:
Hashify
Defined in:
lib/daily_log.rb

Overview

DailyLog is a container for a day’s work log.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hashify

#to_hash

Constructor Details

#initialize(params = {}) ⇒ DailyLog

Returns a new instance of DailyLog.



14
15
16
17
# File 'lib/daily_log.rb', line 14

def initialize(params = {})
  @date = params[:date]
  @entries = params[:entries] || []
end

Instance Attribute Details

#dateObject

Represents a single day’s work log.



12
13
14
# File 'lib/daily_log.rb', line 12

def date
  @date
end

#entriesObject

Represents a single day’s work log.



12
13
14
# File 'lib/daily_log.rb', line 12

def entries
  @entries
end

Class Method Details

.from_hash(hash) ⇒ DailyLog

Create a DailyLog from a hash with symbolized keys

Parameters:

  • hash (Hash)

    the hash to convert

Returns:

  • (DailyLog)

    the created DailyLog object



51
52
53
54
55
56
# File 'lib/daily_log.rb', line 51

def self.from_hash(hash)
  new(
    date: hash[:date],
    entries: hash[:entries].map { |entry| LogEntry.from_hash(entry) }
  )
end

Instance Method Details

#<<(entry) ⇒ Object

Add a log entry to the daily log in the entries array at the end. At this point, the log is not checked for duplicates and not sorted.

Parameters:

  • entry (LogEntry)

    the log entry to add



80
# File 'lib/daily_log.rb', line 80

def <<(entry) = @entries << entry

#==(other) ⇒ Boolean

Equals method to compare two DailyLog objects.

Parameters:

  • other (DailyLog)

    the other DailyLog object to compare with

Returns:

  • (Boolean)

    true if both DailyLog objects have the same date and entries, false otherwise



62
63
64
# File 'lib/daily_log.rb', line 62

def ==(other)
  date == other.date && entries == other.entries
end

#[](key) ⇒ LogEntry?

Access a log entry by its unique key. To safeguard against multiple entries with the same key, and future-proofing, an empty string will return nil.

Parameters:

  • key (String)

    the unique key of the log entry

Returns:

  • (LogEntry, nil)

    the log entry with the specified key, or nil if not found



71
72
73
74
75
# File 'lib/daily_log.rb', line 71

def [](key)
  return nil if key.nil? || key.empty?

  @entries.find { |entry| entry.key == key }
end

#clearObject



85
# File 'lib/daily_log.rb', line 85

def clear = @entries.clear

#empty?Boolean

Returns:

  • (Boolean)


84
# File 'lib/daily_log.rb', line 84

def empty? = @entries.empty?

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
# File 'lib/daily_log.rb', line 86

def key?(key) = @entries.any? { |entry| entry.key == key }

#lengthObject



83
# File 'lib/daily_log.rb', line 83

def length = @entries.length

#peopleHash<String, Integer>

Returns a hash of people mentioned in the log for the current day with the number of times they are mentioned. People are defined as words starting with @ or ~.

Returns:

  • (Hash<String, Integer>)


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

def people
  entries.map { |entry| entry.people.to_a }.flatten.tally
end

#people?Boolean

Returns true if there are people mentioned in any entry of the current day.

Returns:

  • (Boolean)

    true if there are people mentioned, false otherwise.



22
23
24
# File 'lib/daily_log.rb', line 22

def people?
  people.size.positive?
end

#sizeObject



82
# File 'lib/daily_log.rb', line 82

def size = @entries.size

#tagsArray<String>

Returns a sorted list of tags used in the entries for the current day.

Examples:

log = DailyLog.new(date: Date.today,
                   entries: [LogEntry.new(message: "Work on something", tags: ['work', 'project'])])
log.tags # => ["project", "work"]

Returns:

  • (Array<String>)


43
44
45
# File 'lib/daily_log.rb', line 43

def tags
  entries.flat_map(&:tags).uniq.sort
end