Class: Tamarillo::Tomato

Inherits:
Object
  • Object
show all
Defined in:
lib/tamarillo/tomato.rb

Overview

Public: A unit of work.

A Tomato is a ‘pomodoro’, it keeps track of the amount of time you have focused on a single task. It can be interrupted or completed.

Defined Under Namespace

Modules: States

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration, clock) ⇒ Tomato

Public: Initializes a new Tomato.

duration - The length of the Tomato in seconds. clock - A Clock instance to keep track of elapsed time.



23
24
25
26
# File 'lib/tamarillo/tomato.rb', line 23

def initialize(duration, clock)
  @duration = duration
  @clock = clock
end

Instance Attribute Details

#durationObject

Public: Gets/Sets the length of the tomato in seconds.



17
18
19
# File 'lib/tamarillo/tomato.rb', line 17

def duration
  @duration
end

Instance Method Details

#active?Boolean

Public: Returns true if the Tomato has not been completed or interrupted.

Returns:

  • (Boolean)


71
72
73
# File 'lib/tamarillo/tomato.rb', line 71

def active?
  States::ACTIVE == state
end

#approx_minutes_remainingObject

Public: Returns the approximate number of minutes until completetion.



52
53
54
# File 'lib/tamarillo/tomato.rb', line 52

def approx_minutes_remaining
  (remaining / 60.0).round.to_i
end

#completed?Boolean

Public: Returns true if the elapsed Time matches the duration.

Returns:

  • (Boolean)


76
77
78
# File 'lib/tamarillo/tomato.rb', line 76

def completed?
  States::COMPLETED == state
end

#dateObject

Public: Returns the Date the Tomato was started on.



34
35
36
# File 'lib/tamarillo/tomato.rb', line 34

def date
  @clock.start_date
end

#elapsedObject

Public: Returns the number of seconds elapsed since start.



57
58
59
# File 'lib/tamarillo/tomato.rb', line 57

def elapsed
  @clock.elapsed
end

#eql?(other) ⇒ Boolean

Public: Returns true if two Tomatoes share a start Time.

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/tamarillo/tomato.rb', line 39

def eql?(other)
  other.started_at == started_at ||
  super(other)
end

#interrupt!Object

Public: Marks the tomato as interrupted.

Returns the Tomato.



64
65
66
67
# File 'lib/tamarillo/tomato.rb', line 64

def interrupt!
  @interrupted = true
  self
end

#interrupted?Boolean

Public: Returns true if the Tomato has been interrupted.

Returns:

  • (Boolean)


81
82
83
# File 'lib/tamarillo/tomato.rb', line 81

def interrupted?
  States::INTERRUPTED == state
end

#remainingObject

Public: Returns the number of seconds until completion.



45
46
47
48
# File 'lib/tamarillo/tomato.rb', line 45

def remaining
  d = @duration - @clock.elapsed
  d > 0 ? d : 0
end

#started_atObject

Public: Returns the starting Time of the Tomato.



29
30
31
# File 'lib/tamarillo/tomato.rb', line 29

def started_at
  @clock.start_time
end

#stateObject

Public: Returns which state the Tomato is in.

I’d rather keep this internal, but the storage system needs to know what state the tamato was in when it was written.



89
90
91
92
93
94
95
96
97
# File 'lib/tamarillo/tomato.rb', line 89

def state
  if @interrupted
    States::INTERRUPTED
  elsif remaining == 0
    States::COMPLETED
  else
    States::ACTIVE
  end
end