class Fones::Config

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

Attributes

config[RW]

Public Class Methods

new() click to toggle source
# File lib/fones/config.rb, line 12
def initialize()
  @config = {
    :theme => {
      :author     => nil,
      :author_url => nil,
    },
    :links => []
  }
end

Public Instance Methods

[](var) click to toggle source

Provides access to the config using the Hash square brackets

# File lib/fones/config.rb, line 23
def [](var)
  @config[var]
end
[]=(var, value) click to toggle source

Allows modifying variables through hash square brackets

# File lib/fones/config.rb, line 28
def []=(var, value)
  @config[var] = value
end
config_file() click to toggle source

Returns the path to the user’s configuration file

# File lib/fones/config.rb, line 33
def config_file
  @config_file ||= File.expand_path(File.join('~', '.fones', 'config.yml'))
end
read() click to toggle source

Loads config declarations in user’s home dir

If file does not exist then it will be created

# File lib/fones/config.rb, line 55
def read
  return write unless File.exists?(self.config_file) 

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

  @config = YAML::load_file(self.config_file) unless self.config_file.empty?

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

  self
end
write(options={}) click to toggle source

Writes the configuration file

# File lib/fones/config.rb, line 38
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.write @config.to_yaml

  io.close

  self
end