Module: Todo::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/to-do/config.rb

Overview

The module that contains the methods relevant to configurations and custom configuration in config.yml

similar to Sam Goldstein's config.rb for timetrap

Constant Summary collapse

PATH =

The path to the the config file

File.join(ENV['HOME'], '.to-do', 'config.yml')

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Overloading [] operator

Accessor for values in the config



38
39
40
41
# File 'lib/to-do/config.rb', line 38

def [] key
  fileval = YAML.load_file PATH
  defaults.merge(fileval)[key]
end

#[]=(key, value) ⇒ Object

Overloading []= operator

Setter for values in the Config hash



49
50
51
52
53
54
55
56
# File 'lib/to-do/config.rb', line 49

def []= key, value
  fileval = YAML.load_file PATH
  configs = defaults.merge(fileval)
  configs[key] = value
  File.open(PATH, 'w') do |fh|
    fh.puts(configs.to_yaml)
  end
end

#defaultsHash<Symbol,Object>

Default config key value pairs



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/to-do/config.rb', line 17

def defaults
  {
    # the location of all all your list yaml files
    :lists_directory => File.join(ENV["HOME"],".to-do","lists"),
    # a sqlite3 databse that contains all of the tasks 
    :task_database => File.join(ENV["HOME"], ".to-do", "to-do.sqlite"), 
    # the current working list
    :working_list_name => "My New List", 
    # default width for formatting
    :width => 50,
    # default color scheme options
    :color => "light"
  }
end

#writeObject

Writes the configs to the file config.yml



59
60
61
62
63
64
65
66
67
68
# File 'lib/to-do/config.rb', line 59

def write
  configs = if File.exist? PATH
    defaults.merge(YAML.load_file PATH)
  else
    defaults
  end
  File.open(PATH, 'w') do |fh|
    fh.puts(configs.to_yaml)
  end
end