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.
-
#entry_time ⇒ Object
readonly
Returns the value of attribute entry_time.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
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(data_file) ⇒ Configuration
constructor
Load config data from file – 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(data_file) ⇒ Object
To be called at the very end of the script to write data back into YAML.
-
#switch_to(next_key) ⇒ Object
Change context to a different task.
Constructor Details
#initialize(data_file) ⇒ Configuration
Load config data from file – bootstrap it if empty.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/day/configuration.rb', line 30 def initialize(data_file) unless File.exist? data_file bootstrap_db(data_file) puts "day.rb created new config file " + CONFIG_FILE end if File.exist? data_file loaded_data = Psych.safe_load_file(data_file, permitted_classes: [Time, Symbol]) @context = loaded_data[:context] @entry_time = loaded_data[:entry_time] @tasks = loaded_data[:tasks] else puts "Error creating / loading config file" end 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 |
#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 |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
25 26 27 |
# File 'lib/day/configuration.rb', line 25 def tasks @tasks end |
Instance Method Details
#clear_context ⇒ Object
Exit context without switching to a new one.
65 66 67 68 |
# File 'lib/day/configuration.rb', line 65 def clear_context() cap_current_fulfillment @context, @entry_time = nil, nil end |
#clear_fulfillment(task) ⇒ Object
Clear fulfillment for one or all tasks.
73 74 75 76 77 78 79 80 81 |
# File 'lib/day/configuration.rb', line 73 def clear_fulfillment(task) if task clear_fulfillment_for task else @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.)
89 90 91 |
# File 'lib/day/configuration.rb', line 89 def delete(task_key) @tasks.delete task_key end |
#lookup_task(task) ⇒ Object
Used to verify that a task actually exists and to cross-reference indices to names
115 116 117 118 119 120 121 |
# File 'lib/day/configuration.rb', line 115 def lookup_task(task) if task.number? @tasks.keys[task.to_i] else task if @tasks.has_key? task end end |
#new_task(opts) ⇒ Object
Interface to save_task which decomposes opts hash.
50 51 52 |
# File 'lib/day/configuration.rb', line 50 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.)
95 96 97 98 99 |
# File 'lib/day/configuration.rb', line 95 def reload() @context = @context @entry_time = @entry_time end |
#save(data_file) ⇒ Object
To be called at the very end of the script to write data back into YAML
102 103 104 105 106 107 108 109 110 |
# File 'lib/day/configuration.rb', line 102 def save(data_file) data = { context: @context, entry_time: @entry_time, tasks: @tasks } File.open(data_file, 'w') do |file| file.write(Psych.dump(data)) end end |
#switch_to(next_key) ⇒ Object
Change context to a different task. (Saves fulfillment for previous task.)
58 59 60 61 62 |
# File 'lib/day/configuration.rb', line 58 def switch_to(next_key) cap_current_fulfillment if @context @context = next_key if @tasks.has_key?(next_key) @entry_time = Time.now.getutc end |