Class: Worklog::DailyLog
Overview
DailyLog is a container for a day’s work log.
Instance Attribute Summary collapse
-
#date ⇒ Object
Represents a single day’s work log.
-
#entries ⇒ Object
Represents a single day’s work log.
Class Method Summary collapse
-
.from_hash(hash) ⇒ DailyLog
Create a DailyLog from a hash with symbolized keys.
Instance Method Summary collapse
-
#<<(entry) ⇒ Object
Add a log entry to the daily log in the entries array at the end.
-
#==(other) ⇒ Boolean
Equals method to compare two DailyLog objects.
-
#[](key) ⇒ LogEntry?
Access a log entry by its unique key.
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(params = {}) ⇒ DailyLog
constructor
A new instance of DailyLog.
- #key?(key) ⇒ Boolean
- #length ⇒ Object
-
#people ⇒ Hash<String, Integer>
Returns a hash of people mentioned in the log for the current day with the number of times they are mentioned.
-
#people? ⇒ Boolean
Returns true if there are people mentioned in any entry of the current day.
- #size ⇒ Object
-
#tags ⇒ Array<String>
Returns a sorted list of tags used in the entries for the current day.
Methods included from Hashify
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
#date ⇒ Object
Represents a single day’s work log.
12 13 14 |
# File 'lib/daily_log.rb', line 12 def date @date end |
#entries ⇒ Object
Represents a single day’s work log.
12 13 14 |
# File 'lib/daily_log.rb', line 12 def entries @entries end |
Class Method Details
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.
80 |
# File 'lib/daily_log.rb', line 80 def <<(entry) = @entries << entry |
#==(other) ⇒ Boolean
Equals method to compare two DailyLog objects.
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.
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 |
#clear ⇒ Object
85 |
# File 'lib/daily_log.rb', line 85 def clear = @entries.clear |
#empty? ⇒ Boolean
84 |
# File 'lib/daily_log.rb', line 84 def empty? = @entries.empty? |
#key?(key) ⇒ Boolean
86 |
# File 'lib/daily_log.rb', line 86 def key?(key) = @entries.any? { |entry| entry.key == key } |
#length ⇒ Object
83 |
# File 'lib/daily_log.rb', line 83 def length = @entries.length |
#people ⇒ Hash<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 ~.
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.
22 23 24 |
# File 'lib/daily_log.rb', line 22 def people? people.size.positive? end |
#size ⇒ Object
82 |
# File 'lib/daily_log.rb', line 82 def size = @entries.size |
#tags ⇒ Array<String>
Returns a sorted list of tags used in the entries for the current day.
43 44 45 |
# File 'lib/daily_log.rb', line 43 def entries.flat_map(&:tags).uniq.sort end |