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

#==(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

#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

#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