Class: LogEntry

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

Overview

A single log entry.

Constant Summary collapse

PERSON_REGEX =
/(?:\s|^)[~@](\w+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hashify

#to_hash

Constructor Details

#initialize(params = {}) ⇒ LogEntry

Returns a new instance of LogEntry.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/log_entry.rb', line 19

def initialize(params = {})
  @time = params[:time]
  # If tags are nil, set to empty array.
  # This is similar to the CLI default value.
  @tags = params[:tags] || []
  @ticket = params[:ticket]
  @url = params[:url] || ''
  @epic = params[:epic]
  @message = params[:message]
  @project = params[:project]

  # Back reference to the day
  @day = params[:day] || nil
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



17
18
19
# File 'lib/log_entry.rb', line 17

def day
  @day
end

#epicObject

Represents a single entry in the work log.



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

def epic
  @epic
end

#messageObject

Represents a single entry in the work log.



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

def message
  @message
end

#projectObject

Represents a single entry in the work log.



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

def project
  @project
end

#tagsObject

Represents a single entry in the work log.



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

def tags
  @tags
end

#ticketObject

Represents a single entry in the work log.



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

def ticket
  @ticket
end

#timeObject

Represents a single entry in the work log.



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

def time
  @time
end

#urlObject

Represents a single entry in the work log.



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

def url
  @url
end

Instance Method Details

#==(other) ⇒ Object



95
96
97
98
# File 'lib/log_entry.rb', line 95

def ==(other)
  time == other.time && tags == other.tags && ticket == other.ticket && url == other.url &&
    epic == other.epic && message == other.message
end

#epic?Boolean

Returns true if the entry is an epic, false otherwise.

Returns:

  • (Boolean)


35
36
37
# File 'lib/log_entry.rb', line 35

def epic?
  @epic == true
end

#message_string(known_people = nil) ⇒ Object

Returns the message string with formatting without the time.

Parameters:

  • known_people (defaults to: nil)

    Hash[String, Person] A hash of people with their handles as keys.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/log_entry.rb', line 41

def message_string(known_people = nil)
  # replace all mentions of people with their names.
  msg = @message.dup
  people.each do |person|
    next unless known_people && known_people[person]

    msg.gsub!(/[~@]#{person}/) do |match|
      s = ''
      s += ' ' if match[0] == ' '
      s += "#{Rainbow(known_people[person].name).underline} (~#{person})" if known_people && known_people[person]
      s
    end
  end

  s = ''

  s += if epic
         Rainbow("[EPIC] #{msg}").bg(:white).fg(:black)
       else
         msg
       end

  s += "  [#{Rainbow(@ticket).fg(:blue)}]" if @ticket

  # Add tags in brackets if defined.
  s += '  [' + @tags.map { |tag| "#{tag}" }.join(', ') + ']' if @tags && @tags.size > 0

  # Add URL in brackets if defined.
  s += "  [#{@url}]" if @url && @url != ''

  s += "  [#{@project}]" if @project && @project != ''

  s
end

#peopleObject



76
77
78
79
80
81
82
# File 'lib/log_entry.rb', line 76

def people
  # Return people that are mentioned in the entry. People are defined as character sequences
  # starting with @ or ~. Whitespaces are used to separate people. Punctuation is ignored.
  # Empty set if no people are mentioned.
  # @return [Set<String>]
  @message.scan(PERSON_REGEX).flatten.uniq.sort.to_set
end

#people?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/log_entry.rb', line 84

def people?
  # Return true if there are people in the entry.
  #
  # @return [Boolean]
  people.size.positive?
end

#to_yamlObject



91
92
93
# File 'lib/log_entry.rb', line 91

def to_yaml
  to_hash.to_yaml
end