class Ptimelog::Entry

Dataclass to wrap an entry

Constants

BILLABLE
NON_BILLABLE

Attributes

account[R]

allow to read everything else

billable[R]

allow to read everything else

date[RW]

define only trivial writers, omit special and derived values

description[RW]

define only trivial writers, omit special and derived values

finish_time[R]

allow to read everything else

start_time[R]

allow to read everything else

tags[R]

allow to read everything else

ticket[RW]

define only trivial writers, omit special and derived values

Public Class Methods

from_timelog(matched_line) click to toggle source
# File lib/ptimelog/entry.rb, line 23
def from_timelog(matched_line)
  entry = new
  entry.from_timelog(matched_line)
  entry
end
new(config = Configuration.instance) click to toggle source
# File lib/ptimelog/entry.rb, line 17
def initialize(config = Configuration.instance)
  @config = config
  @script = Script.new(@config[:dir])
end

Public Instance Methods

billable?() click to toggle source
# File lib/ptimelog/entry.rb, line 63
def billable?
  @billable == BILLABLE
end
duration() click to toggle source
# File lib/ptimelog/entry.rb, line 74
def duration
  (Time.parse(@finish_time) - Time.parse(@start_time)).to_i
end
finish_time=(time) click to toggle source
# File lib/ptimelog/entry.rb, line 44
def finish_time=(time)
  @finish_time = round_time(time, @config[:rounding])
end
from_timelog(matched_line) click to toggle source
# 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?() click to toggle source
# File lib/ptimelog/entry.rb, line 59
def hidden?
  @description.to_s.end_with?('**') # hide lunch and breaks
end
infer_ptime_settings() click to toggle source
# File lib/ptimelog/entry.rb, line 67
def infer_ptime_settings
  return if hidden?
  return unless @script.inferer(script_name).exist?

  @account, @billable = infer_account_and_billable
end
start_time=(time) click to toggle source
# File lib/ptimelog/entry.rb, line 40
def start_time=(time)
  @start_time = round_time(time, @config[:rounding])
end
tags=(tags) click to toggle source
# File lib/ptimelog/entry.rb, line 48
def tags=(tags)
  @tags = case tags
          when '', nil then nil
          else tags.split.compact
          end
end
to_s() click to toggle source
# 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?() click to toggle source
# File lib/ptimelog/entry.rb, line 55
def valid?
  @start_time && duration.positive? && !hidden?
end

Private Instance Methods

infer_account_and_billable() click to toggle source
# File lib/ptimelog/entry.rb, line 118
def infer_account_and_billable
  script = @script.inferer(script_name)

  cmd = %(#{script} "#{@ticket}" "#{@description}" #{script_args})

  account, billable = `#{cmd}`.chomp.split

  [account, (billable == 'true' ? BILLABLE : NON_BILLABLE)]
end
round_time(time, interval) click to toggle source

make sortable/def <=>

# File lib/ptimelog/entry.rb, line 96
def round_time(time, interval)
  return time unless interval
  return unless time.to_s =~ /\d\d:\d\d/

  hour, minute = time.split(':')
  minute = (minute.to_i / interval.to_f).round * interval.to_i

  if minute == 60
    [hour.succ, 0]
  else
    [hour, minute]
  end.map { |part| part.to_s.rjust(2, '0') }.join(':')
end
script_args() click to toggle source
# File lib/ptimelog/entry.rb, line 114
def script_args
  @script_args ||= @tags.to_a[1..].to_a.map(&:inspect).join(' ')
end
script_name() click to toggle source
# File lib/ptimelog/entry.rb, line 110
def script_name
  @script_name ||= @tags.to_a.first.to_s
end