Class: Tamarillo::Controller

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

Overview

Public: This is intended to provide an environment for tomatoes to work from. It integrates the storage, config and monitor into a single coherant object.

Instance Method Summary collapse

Constructor Details

#initialize(config, storage) ⇒ Controller

Initializes a new controller.



15
16
17
18
# File 'lib/tamarillo/controller.rb', line 15

def initialize(config, storage)
  @config = config
  @storage = storage
end

Instance Method Details

#configObject

Public: Returns the current config.



59
60
61
# File 'lib/tamarillo/controller.rb', line 59

def config
  @config
end

#interrupt_current_tomatoObject

Public: Interrupts the current tomato if one is running.



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

def interrupt_current_tomato
  stop_monitor

  tomato = @storage.latest
  return if tomato.nil? || tomato.interrupted?

  tomato.interrupt!
  @storage.write_tomato(tomato)

  tomato
end

#start_new_tomatoObject

Public: Starts a new tomato if one is not already running.



34
35
36
37
38
39
40
41
42
43
# File 'lib/tamarillo/controller.rb', line 34

def start_new_tomato
  tomato = @storage.latest
  return if tomato && tomato.active?

  tomato = Tomato.new(@config.duration_in_seconds, Clock.now)
  @storage.write_tomato(tomato)
  start_monitor(tomato)

  tomato
end

#status(format) ⇒ Object

Public: Formats and returns the status of the current tomato. Returns nil if no tomato is found.



22
23
24
25
26
27
28
29
30
31
# File 'lib/tamarillo/controller.rb', line 22

def status(format)
  tomato = @storage.latest
  return unless tomato && tomato.active?

  case format
  when Formats::HUMAN then format_approx_time(tomato.remaining)
  when Formats::PROMPT then format_prompt(tomato)
  else raise "Invalid format"
  end
end

#update_config(options = {}) ⇒ Object

Public: Updates the current config.



64
65
66
67
68
69
70
# File 'lib/tamarillo/controller.rb', line 64

def update_config(options = {})
  valid_config_options(options).each do |key,value|
    @config.send("#{key}=".to_sym, value)
  end

  @storage.write_config
end