Class: Configuration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(data_file) ⇒ Configuration

Load config data from file – bootstrap it if empty.

Parameters:

  • data_file

    Location of task data file on local drive.



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

#contextObject (readonly)

Returns the value of attribute context.



25
26
27
# File 'lib/day/configuration.rb', line 25

def context
  @context
end

#entry_timeObject (readonly)

Returns the value of attribute entry_time.



25
26
27
# File 'lib/day/configuration.rb', line 25

def entry_time
  @entry_time
end

#tasksObject (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_contextObject

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.

Parameters:

  • task (String)

    valid task name to specify action for (defaults to all otherwise)



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.)

Parameters:

  • task_key (String)

    Valid task name to delete.



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

Parameters:

  • task (String)

    name or index reference to task



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.

Parameters:

  • opts (Hash)

    options hash containing input data



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

#reloadObject

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.)

Parameters:

  • next_key (String)

    the name of the task to switch to.



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