Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Defined in:
- lib/day/configuration.rb
Overview
Config class handles access to our config file, which mostly just provides a data-access layer. Schema:
configuration = {
context = :task_key,
entry_time = :task_start_time
tasks = {
:key => {
:description => (string),
:active_days => [(keys of active days)],
:estimate => (integer in minutes),
:fulfillment => (integer in minutes)
}
}
}
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#entry_time ⇒ Object
readonly
Returns the value of attribute entry_time.
Instance Method Summary collapse
-
#clear_context ⇒ Object
Exit context without switching to a new one.
-
#clear_fulfillment(task) ⇒ Object
Clear fulfillment for one or all tasks.
-
#delete(task_key) ⇒ Object
Remove a task from config object data.
-
#initialize(db) ⇒ Configuration
constructor
Load DB data and bootstrap it if empty.
-
#lookup_task(task) ⇒ Object
Used to verify that a task actually exists and to cross-reference indices to names.
-
#new_task(opts) ⇒ Object
Interface to save_task which decomposes opts hash.
-
#reload ⇒ Object
Reload class objects from config data.
-
#save ⇒ Object
To be called at the very end of the script to write data back into YAML::DBM.
-
#switch_to(next_key) ⇒ Object
Change context to a different task.
Constructor Details
#initialize(db) ⇒ Configuration
Load DB data and bootstrap it if empty.
30 31 32 33 34 |
# File 'lib/day/configuration.rb', line 30 def initialize(db) @db = db @data = @db.to_hash bootstrap_db if @data.empty? end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
25 26 27 |
# File 'lib/day/configuration.rb', line 25 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
25 26 27 |
# File 'lib/day/configuration.rb', line 25 def data @data end |
#entry_time ⇒ Object (readonly)
Returns the value of attribute entry_time.
25 26 27 |
# File 'lib/day/configuration.rb', line 25 def entry_time @entry_time end |
Instance Method Details
#clear_context ⇒ Object
Exit context without switching to a new one.
54 55 56 57 |
# File 'lib/day/configuration.rb', line 54 def clear_context() cap_current_fulfillment @data['context'], @data['entry_time'] = nil, nil end |
#clear_fulfillment(task) ⇒ Object
Clear fulfillment for one or all tasks.
62 63 64 65 66 67 68 69 70 |
# File 'lib/day/configuration.rb', line 62 def clear_fulfillment(task) if task clear_fulfillment_for task else @data['tasks'].each do |task, task_data| clear_fulfillment_for task end end end |
#delete(task_key) ⇒ Object
Remove a task from config object data. (Note that this doesn’t persist to the DBM files by itself… And again, the responsibility for calling save() and closing DB lies with the consumer of this class.)
78 79 80 |
# File 'lib/day/configuration.rb', line 78 def delete(task_key) @data['tasks'].delete task_key end |
#lookup_task(task) ⇒ Object
Used to verify that a task actually exists and to cross-reference indices to names
98 99 100 101 102 103 104 |
# File 'lib/day/configuration.rb', line 98 def lookup_task(task) if task.number? @data['tasks'].keys[task.to_i] else task if @data['tasks'].has_key? task end end |
#new_task(opts) ⇒ Object
Interface to save_task which decomposes opts hash.
39 40 41 |
# File 'lib/day/configuration.rb', line 39 def new_task(opts) save_task(opts[:task], opts[:description], opts[:days], opts[:estimate]) end |
#reload ⇒ Object
Reload class objects from config data. (Used during testing.)
84 85 86 87 88 |
# File 'lib/day/configuration.rb', line 84 def reload() @context = @data['context'] @entry_time = @data['entry_time'] end |
#save ⇒ Object
To be called at the very end of the script to write data back into YAML::DBM
91 92 93 |
# File 'lib/day/configuration.rb', line 91 def save() @db.replace(@data) end |
#switch_to(next_key) ⇒ Object
Change context to a different task. (Saves fulfillment for previous task.)
47 48 49 50 51 |
# File 'lib/day/configuration.rb', line 47 def switch_to(next_key) cap_current_fulfillment if @context @data['context'] = next_key if @data['tasks'].has_key?(next_key) @data['entry_time'] = Time.now.getutc end |