Class: Todone::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/todone/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



34
35
36
37
# File 'lib/todone/config.rb', line 34

def initialize(data={})
	@data = {}
	update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/todone/config.rb', line 65

def method_missing(sym, *args)
	if sym.to_s =~ /(.+)=$/
		self[$1] = args.first
	else
		self[sym]
	end
end

Class Method Details

.load_config(dir) ⇒ Todone::Config

Returns The populated Todone::Config instance.

Parameters:

Returns:

See Also:

  • Todone::Consts:CONFIG_FILE


21
22
23
24
25
# File 'lib/todone/config.rb', line 21

def load_config dir
  require 'yaml'
  data = YAML.load(File.open(File.join(dir, Todone::Consts::CONFIG_FILE)).read)
  Todone::Config.new data
end

Instance Method Details

#[](key) ⇒ Object



45
46
47
# File 'lib/todone/config.rb', line 45

def [](key)
	@data[key.to_sym]
end

#[]=(key, value) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/todone/config.rb', line 49

def []=(key, value)
	if value.class == Hash
		@data[key.to_sym] = Config.new(value)
	else
		@data[key.to_sym] = value
	end
end

#save(dir) ⇒ Object



28
29
30
31
32
# File 'lib/todone/config.rb', line 28

def save dir
	File.open(File.join(dir, Todone::Consts::CONFIG_FILE), 'w') do |f| 
		f.write( @data.to_hash.to_yaml )
	end
end

#to_hashObject



57
58
59
60
61
62
63
# File 'lib/todone/config.rb', line 57

def to_hash
	hash = {}
	@data.each do |key,value|
		hash[key] = value.class == Config ? value.to_hash : value
	end
	hash
end

#update!(data) ⇒ Object



39
40
41
42
43
# File 'lib/todone/config.rb', line 39

def update!(data)
	data.each do |key, value|
		self[key] = value
	end
end