Class: Timelog::Entry

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/timelog/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Returns a new instance of Entry.



10
11
12
13
# File 'lib/timelog/entry.rb', line 10

def initialize
  @pauses = {}
  @archived = false
end

Instance Attribute Details

#archivedObject Also known as: archived?

Returns the value of attribute archived.



6
7
8
# File 'lib/timelog/entry.rb', line 6

def archived
  @archived
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/timelog/entry.rb', line 5

def client
  @client
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/timelog/entry.rb', line 5

def description
  @description
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/timelog/entry.rb', line 5

def id
  @id
end

#pausesObject (readonly)

Returns the value of attribute pauses.



6
7
8
# File 'lib/timelog/entry.rb', line 6

def pauses
  @pauses
end

#projectObject

Returns the value of attribute project.



5
6
7
# File 'lib/timelog/entry.rb', line 5

def project
  @project
end

#startedObject

Returns the value of attribute started.



6
7
8
# File 'lib/timelog/entry.rb', line 6

def started
  @started
end

#stoppedObject

Returns the value of attribute stopped.



6
7
8
# File 'lib/timelog/entry.rb', line 6

def stopped
  @stopped
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/timelog/entry.rb', line 87

def active?
  !stopped?
end

#active_secondsObject



50
51
52
# File 'lib/timelog/entry.rb', line 50

def active_seconds
  total_seconds - paused_seconds
end

#active_timeObject



65
66
67
# File 'lib/timelog/entry.rb', line 65

def active_time
  parse_seconds(active_seconds)
end

#dateObject



46
47
48
# File 'lib/timelog/entry.rb', line 46

def date
  started.try(:to_date)
end

#pause(at = Time.now) ⇒ Object

Raises:

  • (EntryAlreadyPaused)


32
33
34
35
# File 'lib/timelog/entry.rb', line 32

def pause(at = Time.now)
  raise EntryAlreadyPaused if paused?
  @pauses[at] = nil
end

#paused?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/timelog/entry.rb', line 42

def paused?
  @pauses.length > 0 and @pauses.values.last.nil?
end

#paused_secondsObject



54
55
56
57
58
# File 'lib/timelog/entry.rb', line 54

def paused_seconds
  pauses.inject(0) do |seconds, pause|
    seconds + ((pause[1] || Time.now) - pause[0])
  end.to_i
end

#paused_timeObject



69
70
71
# File 'lib/timelog/entry.rb', line 69

def paused_time
  parse_seconds(paused_seconds)
end

#resume(at = Time.now) ⇒ Object

Raises:

  • (EntryNotPaused)


37
38
39
40
# File 'lib/timelog/entry.rb', line 37

def resume(at = Time.now)
  raise EntryNotPaused unless paused?
  @pauses[@pauses.keys.last] = at
end

#started?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/timelog/entry.rb', line 24

def started?
  @started
end

#statusObject



81
82
83
84
85
# File 'lib/timelog/entry.rb', line 81

def status
  return "archived" if archived?
  return "paused" if paused?
  return "active" if active?
end

#stopped?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/timelog/entry.rb', line 28

def stopped?
  @stopped
end

#total_secondsObject



60
61
62
63
# File 'lib/timelog/entry.rb', line 60

def total_seconds
  return 0 unless started?
  ((stopped || Time.now) - started).round
end

#total_timeObject



73
74
75
# File 'lib/timelog/entry.rb', line 73

def total_time
  parse_seconds(total_seconds)
end