Class: Ptimelog::Entry

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

Overview

Dataclass to wrap an entry

Constant Summary collapse

BILLABLE =
1
NON_BILLABLE =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.instance) ⇒ Entry

Returns a new instance of Entry.



17
18
19
20
# File 'lib/ptimelog/entry.rb', line 17

def initialize(config = Configuration.instance)
  @config = config
  @script = Script.new(@config[:dir])
end

Instance Attribute Details

#accountObject (readonly)

allow to read everything else



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

def 
  @account
end

#billableObject (readonly)

allow to read everything else



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

def billable
  @billable
end

#dateObject

define only trivial writers, omit special and derived values



9
10
11
# File 'lib/ptimelog/entry.rb', line 9

def date
  @date
end

#descriptionObject

define only trivial writers, omit special and derived values



9
10
11
# File 'lib/ptimelog/entry.rb', line 9

def description
  @description
end

#finish_timeObject

allow to read everything else



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

def finish_time
  @finish_time
end

#start_timeObject

allow to read everything else



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

def start_time
  @start_time
end

#tagsObject

allow to read everything else



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

def tags
  @tags
end

#ticketObject

define only trivial writers, omit special and derived values



9
10
11
# File 'lib/ptimelog/entry.rb', line 9

def ticket
  @ticket
end

Class Method Details

.from_timelog(matched_line) ⇒ Object



23
24
25
26
27
# File 'lib/ptimelog/entry.rb', line 23

def from_timelog(matched_line)
  entry = new
  entry.from_timelog(matched_line)
  entry
end

Instance Method Details

#billable?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ptimelog/entry.rb', line 63

def billable?
  @billable == BILLABLE
end

#durationObject



74
75
76
# File 'lib/ptimelog/entry.rb', line 74

def duration
  (Time.parse(@finish_time) - Time.parse(@start_time)).to_i
end

#from_timelog(matched_line) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ptimelog/entry.rb', line 30

def from_timelog(matched_line)
  self.date        = matched_line[:date]
  self.ticket      = matched_line[:ticket]
  self.description = matched_line[:description]
  self.finish_time = matched_line[:time]
  self.tags        = matched_line[:tags]

  infer_ptime_settings
end

#hidden?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ptimelog/entry.rb', line 59

def hidden?
  @description.to_s.end_with?('**') # hide lunch and breaks
end

#infer_ptime_settingsObject



67
68
69
70
71
72
# File 'lib/ptimelog/entry.rb', line 67

def infer_ptime_settings
  return if hidden?
  return unless @script.inferer(script_name).exist?

  @account, @billable = 
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ptimelog/entry.rb', line 78

def to_s
  billable = billable? ? '($)' : nil
  tag_list = Array(@tags).compact

  tags = tag_list.join(' ') if tag_list.any?
  desc = [@ticket, @description].compact.join(': ')
  acc  = [@account, billable].compact.join(' ') if @account

  [
    @start_time, '-', @finish_time, '∴',
    [desc, tags, acc].compact.join(' ∴ '),
  ].compact.join(' ')
end

#valid?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ptimelog/entry.rb', line 55

def valid?
  @start_time && duration.positive? && !hidden?
end