Class: TimeEntry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/timesheet/time_entry.rb

Direct Known Subclasses

ReportItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, start_time, end_time, comment = nil) ⇒ TimeEntry

Returns a new instance of TimeEntry.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/timesheet/time_entry.rb', line 5

def initialize(project, start_time, end_time, comment = nil)

  raise ArgumentError.new("Start time must come before end time") if start_time > end_time

  @project = project
  @start_time = start_time
  @end_time = end_time
  @comment = comment
  @record_number = nil

end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



20
21
22
# File 'lib/timesheet/time_entry.rb', line 20

def comment
  @comment
end

#end_timeObject

Returns the value of attribute end_time.



19
20
21
# File 'lib/timesheet/time_entry.rb', line 19

def end_time
  @end_time
end

#projectObject

Returns the value of attribute project.



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

def project
  @project
end

#record_numberObject

Returns the value of attribute record_number.



21
22
23
# File 'lib/timesheet/time_entry.rb', line 21

def record_number
  @record_number
end

#start_timeObject

Returns the value of attribute start_time.



18
19
20
# File 'lib/timesheet/time_entry.rb', line 18

def start_time
  @start_time
end

Instance Method Details

#<=>(other_time_entry) ⇒ Object



42
43
44
# File 'lib/timesheet/time_entry.rb', line 42

def <=>(other_time_entry)
  self.start_time <=> other_time_entry.start_time
end

#conflict?(other_entry) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/timesheet/time_entry.rb', line 23

def conflict?(other_entry)
  to_range.overlap? other_entry.to_range
end

#durationObject



37
38
39
40
# File 'lib/timesheet/time_entry.rb', line 37

def duration
  return 0 if @start_time == nil || @end_time == nil
  RichUnits::Duration.new(@end_time - @start_time)
end

#to_rangeObject



46
47
48
# File 'lib/timesheet/time_entry.rb', line 46

def to_range
  Range.new(@start_time, @end_time, true)
end

#to_sObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/timesheet/time_entry.rb', line 50

def to_s
  label_width = 10
  str = ""
  str << "#{"class:".ljust(label_width)}#{self.class}\n"
  str << "#{"record:".ljust(label_width)}#{record_number}\n"
  str << "#{"project:".ljust(label_width)}#{project}\n"
  str << "#{"start:".ljust(label_width)}#{start_time.strftime("%m/%d/%Y at %I:%M %p")}\n"
  str << "#{"end:".ljust(label_width)}#{end_time.strftime("%m/%d/%Y at %I:%M %p")}\n"
  str << "#{"duration:".ljust(label_width)}#{duration}\n"
  str << "#{"comment:".ljust(label_width)}#{comment}\n"
  
  str
end