Class: Forge::Config

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

Overview

Reads/Writes a configuration file in the user’s home directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
# File 'lib/forge/config.rb', line 10

def initialize()
  @config = {
    :theme => {
      :author     => nil,
      :author_url => nil,
    },
    :links => []
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/forge/config.rb', line 8

def config
  @config
end

Instance Method Details

#[](var) ⇒ Object

Provides access to the config using the Hash square brackets



21
22
23
# File 'lib/forge/config.rb', line 21

def [](var)
  @config[var]
end

#[]=(var, value) ⇒ Object

Allows modifying variables through hash square brackets



26
27
28
# File 'lib/forge/config.rb', line 26

def []=(var, value)
  @config[var] = value
end

#config_fileObject

Returns the path to the user’s configuration file



31
32
33
# File 'lib/forge/config.rb', line 31

def config_file
  @config_file ||= File.expand_path(File.join('~', '.forge', 'config.yml'))
end

#readObject

Loads config declarations in user’s home dir

If file does not exist then it will be created



51
52
53
54
55
56
57
58
59
# File 'lib/forge/config.rb', line 51

def read
  return write unless File.exists?(self.config_file)

  data = File.open(self.config_file).read

  @config = data.empty? ? {} : JSON.parse(data)

  self
end

#write(options = {}) ⇒ Object

Writes the configuration file



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/forge/config.rb', line 36

def write(options={})
  # If we're unit testing then it helps to use a
  # StringIO object instead of a file buffer
  io = options[:io] || File.open(self.config_file, 'w')

  io.write JSON.generate(@config)

  io.close

  self
end