class TimeSheet::Time::Entry

Attributes

data[RW]
exception[RW]
next[RW]
prev[RW]

Public Class Methods

attrib_matches_any?(value, patterns) click to toggle source
# File lib/time_sheet/time/entry.rb, line 179
def self.attrib_matches_any?(value, patterns)
  return true if !patterns

  patterns.split(/\s*,\s*/).any? do |pattern|
    value.match(pattern)
  end
end
new(data) click to toggle source
# File lib/time_sheet/time/entry.rb, line 6
def initialize(data)
  @data = data
end
now() click to toggle source
# File lib/time_sheet/time/entry.rb, line 2
def self.now
  @now ||= Time.now
end
parse_tags(string) click to toggle source
# File lib/time_sheet/time/entry.rb, line 187
def self.parse_tags(string)
  (string || '').to_s.downcase.split(/\s*,\s*/).map{|t| t.strip}
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/time_sheet/time/entry.rb, line 175
def <=>(other)
  (self.date <=> other.date) || self.start <=> other.start
end
activity() click to toggle source
# File lib/time_sheet/time/entry.rb, line 16
def activity
  @data['activity'] ||= self.prev.activity
end
date() click to toggle source
# File lib/time_sheet/time/entry.rb, line 24
def date
  @date ||= @data['date'] || self.prev.date
end
description() click to toggle source
# File lib/time_sheet/time/entry.rb, line 20
def description
  @data['description'] ||= self.prev.description
end
duration() click to toggle source

def end_zone

@end_zone ||= if v = @data['end_zone']
  # allow a name prefixing the value
  v.split(/\s/).last
elsif self.prev && v = self.prev.end_zone
  v
else
  # self.class.now.getlocal.utc_offset
  # use this process' timezone
  nil
end

end

# File lib/time_sheet/time/entry.rb, line 76
def duration
  (self.end - self.start) / 60
end
employee() click to toggle source
# File lib/time_sheet/time/entry.rb, line 44
def employee
  @employee ||= @data['employee'] || (self.prev ? self.prev.employee : 'Me')
end
end() click to toggle source
# File lib/time_sheet/time/entry.rb, line 35
def end
  ends_at = @data['end'] || (self.next ? self.next.start : self.class.now)

  @end ||= Time.mktime(
    date.year, date.month, date.day,
    ends_at.hour, ends_at.min
  )
end
has_tags?(tags) click to toggle source
# File lib/time_sheet/time/entry.rb, line 104
def has_tags?(tags)
  return true if tags.empty?

  tags.all? do |tag|
    self.tags.include?(tag)
  end
end
matches?(filters) click to toggle source
# File lib/time_sheet/time/entry.rb, line 88
def matches?(filters)
  from = (filters[:from] ? filters[:from] : nil)
  from = from.to_time if from.is_a?(Date)
  to = (filters[:to] ? filters[:to] : nil)
  to = (to + 1).to_time if to.is_a?(Date)
  tags = self.class.parse_tags(filters[:tags])

  has_tags?(tags) &&
  self.class.attrib_matches_any?(employee, filters[:employee]) &&
  self.class.attrib_matches_any?(description, filters[:description]) &&
  self.class.attrib_matches_any?(project, filters[:project]) &&
  self.class.attrib_matches_any?(activity, filters[:activity]) &&
  (!from || from <= self.start) &&
  (!to || to >= self.end)
end
project() click to toggle source
# File lib/time_sheet/time/entry.rb, line 12
def project
  @data['project'] ||= self.prev.project
end
start() click to toggle source
# File lib/time_sheet/time/entry.rb, line 28
def start
  @start ||= Time.mktime(
    date.year, date.month, date.day,
    @data['start'].hour, @data['start'].min
  )
end
tags() click to toggle source
# File lib/time_sheet/time/entry.rb, line 80
def tags
  self.class.parse_tags(@data['tags'])
end
to_hash() click to toggle source
# File lib/time_sheet/time/entry.rb, line 161
def to_hash
  return {
    'employee' => employee,
    'date' => date,
    'start' => start,
    'end' => self.end,
    'duration' => duration,
    'project' => project,
    'activity' => activity,
    'description' => description,
    'tags' => tags
  }
end
to_row() click to toggle source
# File lib/time_sheet/time/entry.rb, line 141
def to_row
  [
    employee, date, start, self.end, duration.to_i, project, activity,
    description
  ]
end
to_s() click to toggle source
# File lib/time_sheet/time/entry.rb, line 148
def to_s
  values = [
    employee,
    date.strftime('%Y-%m-%d'),
    start.strftime('%H:%M'),
    self.end.strftime('%H:%M'),
    duration.to_i.to_s.rjust(4),
    project,
    activity,
    description
  ].join(' | ')
end
valid!() click to toggle source
# File lib/time_sheet/time/entry.rb, line 123
def valid!
  if !@data['start']
    raise TimeSheet::Time::Exception.new('time entry has no start')
  end

  if duration <= 0
    raise TimeSheet::Time::Exception.new('time entry duration is 0 or less')
  end

  if (self.start >= self.end) && self.next
    raise TimeSheet::Time::Exception.new('time entry has no end')
  end

  if !employee
    raise TimeSheet::Time::Exception.new('no employee set')
  end
end
valid?() click to toggle source
# File lib/time_sheet/time/entry.rb, line 112
def valid?
  valid!
  true
rescue TimeSheet::Time::Exception => e
  self.exception = e
  false
rescue StandardError => e
  binding.pry if Timesheet.options[:debug]
  false
end
working_day?() click to toggle source
# File lib/time_sheet/time/entry.rb, line 84
def working_day?
  !date.saturday? && !date.sunday? && !tags.include?('holiday')
end