Class: Minder::PomodoroRunner

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/minder/pomodoro/pomodoro_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ PomodoroRunner

Returns a new instance of PomodoroRunner.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 19

def initialize(**options)
  self.work_duration = options.fetch(:work_duration)
  self.short_break_duration = options.fetch(:short_break_duration)
  self.long_break_duration = options.fetch(:long_break_duration)
  self.database = options.fetch(:database)
  @period_count = 0
  current_period = IdlePeriod.new
  current_period.start!
  database.add_period(current_period)
  @current_period = database.last_period
end

Instance Attribute Details

#current_periodObject (readonly)

Returns the value of attribute current_period.



16
17
18
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 16

def current_period
  @current_period
end

#databaseObject

Returns the value of attribute database.



11
12
13
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 11

def database
  @database
end

#long_break_durationObject

Returns the value of attribute long_break_duration.



11
12
13
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 11

def long_break_duration
  @long_break_duration
end

#period_countObject (readonly)

Returns the value of attribute period_count.



16
17
18
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 16

def period_count
  @period_count
end

#short_break_durationObject

Returns the value of attribute short_break_duration.



11
12
13
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 11

def short_break_duration
  @short_break_duration
end

#work_durationObject

Returns the value of attribute work_duration.



11
12
13
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 11

def work_duration
  @work_duration
end

Instance Method Details

#advance_periodObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 63

def advance_period
  @period_count += 1
  changed

  if period_count.odd?
    notify_observers(:started_work)
    @current_period = WorkPeriod.new(duration_in_minutes: work_duration)
  else
    notify_observers(:started_break)
    @current_period = BreakPeriod.new(duration_in_minutes: break_duration)
  end
end

#break_durationObject



76
77
78
79
80
81
82
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 76

def break_duration
  if period_count % 8 == 0
    long_break_duration
  else
    short_break_duration
  end
end

#complete_period(period) ⇒ Object



58
59
60
61
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 58

def complete_period(period)
  database.complete_period(period)
  @pomodoros_today = nil
end

#continueObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 46

def continue
  return unless current_period.elapsed?

  current_period.complete!
  complete_period(current_period)

  advance_period
  current_period.start!
  database.add_period(current_period)
  @current_period = database.last_period
end

#periodsObject



88
89
90
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 88

def periods
  database.periods
end

#pomodoros_todayObject



84
85
86
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 84

def pomodoros_today
  @pomodoros_today ||= database.pomodoros_today
end

#tickObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minder/pomodoro/pomodoro_runner.rb', line 31

def tick
  return if !current_period.elapsed? || current_period.completed?

  old_period = current_period
  complete_period(old_period)
  @current_period = IdlePeriod.new

  changed
  if old_period.is_a?(WorkPeriod)
    notify_observers(:completed_work)
  elsif old_period.is_a?(BreakPeriod)
    notify_observers(:completed_break)
  end
end