Module: Tracking::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/tracking/config.rb

Overview

Contains methods to interface with tracking’s config file.

similar to Sam Goldstein’s config.rb for timetrap

Constant Summary collapse

PATH =

The path to the config file

File.join(ENV['HOME'], '.tracking', 'config.yml')
DIR =

The path to the config file’s parent directory

File.join(ENV['HOME'], '.tracking')

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Overloading [] operator

Accessor for values in the config

Parameters:

  • key (Symbol)

    the key in the config hash

Returns:

  • (Object)

    the value associated with that key



46
47
48
49
50
# File 'lib/tracking/config.rb', line 46

def [] key
	write unless File.exist? PATH
	data = YAML.load_file PATH
	defaults.merge(data)[key]
end

#[]=(key, value) ⇒ Object

Overloading []= operator

Setter for values in the config

Parameters:

  • key (Symbol)

    the key you are setting a value for

  • value (Object)

    the value you associated with the key



58
59
60
61
62
63
64
65
66
# File 'lib/tracking/config.rb', line 58

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

#defaultsHash<Symbol,Object>

Default config hash

Returns:

  • (Hash<Symbol,Object>)

    the default config in a hash



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tracking/config.rb', line 21

def defaults
	{
		# path to the data file (string, ~ can be used)
		:data_file => '~/.tracking/data.csv',
		# number of lines to be displayed at once by default (integer)
		:lines => 10,
		# width of the task name column, in characters (integer)
		:task_width => 40,
		# format to use for elapsed time display (:colons or :letters)
		:elapsed_format => :colons,
		# toggle colored display of the current (last) task
		:color_current_task => true,
		# toggle header describing tracking's display columns (true or false)
		:show_header => true,
		# toggle display of seconds in elapsed time (true of false)
		:show_elapsed_seconds => false
	}
end

#writeObject

Writes the configs to the file config.yml



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tracking/config.rb', line 69

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