Class: Tamarillo::Config
- Inherits:
-
Object
- Object
- Tamarillo::Config
- 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
-
#duration_in_minutes ⇒ Object
(also: #duration)
Public: Returns the duration of each tomato in minutes.
Class Method Summary collapse
-
.load(path) ⇒ Object
Public: Initializes a config from a YAML file.
Instance Method Summary collapse
-
#duration_in_seconds ⇒ Object
Public: Returns the duration of each tomato in seconds.
-
#initialize(options = {}) ⇒ Config
constructor
Public: Initializes a new config.
-
#notifier ⇒ Object
Public: Returns the notifier type.
-
#notifier=(value) ⇒ Object
Public: Sets the notifier type.
-
#to_yaml ⇒ Object
Public: Returns config in YAML format.
-
#write(path) ⇒ Object
Public: Write a config out to a file.
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( = {}) self.duration_in_minutes = [:duration_in_minutes] self.notifier = [:notifier] end |
Instance Attribute Details
#duration_in_minutes ⇒ Object 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) = {} if File.exist?(path) yaml = YAML.load( File.read(path.to_s) ) || {} [:duration_in_minutes] = yaml['duration'] [:notifier] = yaml['notifier'] end new() end |
Instance Method Details
#duration_in_seconds ⇒ Object
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 |
#notifier ⇒ Object
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_yaml ⇒ Object
Public: Returns config in YAML format.
87 88 89 90 91 92 |
# File 'lib/tamarillo/config.rb', line 87 def to_yaml = { 'duration' => duration_in_minutes, 'notifier' => notifier.to_s } YAML.dump() 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 |