Class: DotFiles::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = DotFiles.configuration_path) ⇒ Config

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
# File 'lib/dot_files/config.rb', line 6

def initialize(path = DotFiles.configuration_path)
  @path = path
  if File.exist?(path)
    file_data = File.read(path)
    @configuration = (file_data.nil? || file_data.length == 0 ? {} : YAML::load(file_data))
  else
    @configuration = Hash.new
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, value = nil) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/dot_files/config.rb', line 26

def method_missing(method_name, value = nil)
  method_name = method_name.to_s
  if method_name[-1,1] == '='
    set(method_name.gsub(/\=\z/, ''), value)
  else
    get(method_name)
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'lib/dot_files/config.rb', line 4

def configuration
  @configuration
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/dot_files/config.rb', line 4

def path
  @path
end

Instance Method Details

#get(key) ⇒ Object



16
17
18
# File 'lib/dot_files/config.rb', line 16

def get(key)
  configuration[key.to_sym]
end

#saveObject



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

def save
  File.open(path, 'w') { |f| f.write(YAML::dump(@configuration)) }
end

#set(key, value) ⇒ Object



20
21
22
23
24
# File 'lib/dot_files/config.rb', line 20

def set(key, value)
  configuration[key.to_sym] = value
  save
  value
end