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(db) ⇒ Configuration

Load DB data and bootstrap it if empty.

Parameters:

  • db (PSYCH::DBM)

    database (hash-like) initialized and closed by caller.



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

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
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

Instance Method Details

#clear_contextObject

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.

Parameters:

  • task (String)

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



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

Parameters:

  • task_key (String)

    Valid task name to delete.



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

Parameters:

  • task (String)

    name or index reference to task



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.

Parameters:

  • opts (Hash)

    options hash containing input data



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

#reloadObject

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

#saveObject

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

Parameters:

  • next_key (String)

    the name of the task to switch to.



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