Class: Tamarillo::Config

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

Overview

Internal: This configures the Tamarillo CLI, which uses values from this config when generating and interacting with Tomatoes. Currently it just holds the duration of each tomato, but it will later configure things like daemon process location and storage interface.

Constant Summary collapse

DEFAULT_DURATION_IN_MINUTES =
25

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Public: Initializes a new config.

options - The hash used to configure Tamarillo.

:duration_in_minutes - The duration in minutes.


19
20
21
22
# File 'lib/tamarillo/config.rb', line 19

def initialize(options = {})
  self.duration_in_minutes = options[:duration_in_minutes]
  self.notifier = options[:notifier]
end

Instance Attribute Details

#duration_in_minutesObject Also known as: duration

Public: Returns the duration of each tomato in minutes.



13
14
15
# File 'lib/tamarillo/config.rb', line 13

def duration_in_minutes
  @duration_in_minutes
end

Class Method Details

.load(path) ⇒ Object

Public: Initializes a config from a YAML file.

This tries to read config data from YAML. If the YAML is missing values or is invalid, defaults are used. If the file doesn’t exist, then a default configuration is created.

path - String or Pathname to the YAML file.

Returns: A config instance.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tamarillo/config.rb', line 67

def self.load(path)
  options = {}

  if File.exist?(path)
    yaml = YAML.load( File.read(path.to_s) ) || {}
    options[:duration_in_minutes] = yaml['duration']
    options[:notifier] = yaml['notifier']
  end

  new(options)
end

Instance Method Details

#duration_in_secondsObject

Public: Returns the duration of each tomato in seconds.



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

def duration_in_seconds
  @duration_in_minutes.to_i * 60
end

#notifierObject

Public: Returns the notifier type.



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

def notifier
  @notifier
end

#notifier=(value) ⇒ Object

Public: Sets the notifier type.



50
51
52
53
54
55
56
# File 'lib/tamarillo/config.rb', line 50

def notifier=(value)
  new_value = Notification.valid!(value)
  new_value ||= @notifier
  new_value ||= Notification.default

  @notifier = new_value
end

#to_yamlObject

Public: Returns config in YAML format.



87
88
89
90
91
92
# File 'lib/tamarillo/config.rb', line 87

def to_yaml
  options = {
    'duration' => duration_in_minutes,
    'notifier' => notifier.to_s }
  YAML.dump(options)
end

#write(path) ⇒ Object

Public: Write a config out to a file.

path - a String or Pathname to the destination file.



82
83
84
# File 'lib/tamarillo/config.rb', line 82

def write(path)
  File.open(path, 'w') { |f| f << to_yaml }
end