Class: FReCon::ConfigurationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/frecon/configuration_file.rb

Overview

Public: A class to handle configuration files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ ConfigurationFile

Public: Initialize a ConfigurationFile.

filename - The name of the file.



22
23
24
# File 'lib/frecon/configuration_file.rb', line 22

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject

Public: The filename for the file.



17
18
19
# File 'lib/frecon/configuration_file.rb', line 17

def filename
  @filename
end

Class Method Details

.config_directoryObject

Public: Returns the User’s config home directory.



64
65
66
# File 'lib/frecon/configuration_file.rb', line 64

def self.config_directory
  ENV['XDG_CONFIG_HOME'] || File.join(Dir.home, '.config')
end

.defaultObject

Public: Create a new ConfigurationFile corresponding to the default defaults configuration location.



45
46
47
# File 'lib/frecon/configuration_file.rb', line 45

def self.default
  self.new(File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "default.yml")))
end

.systemObject

Public: Create a new ConfigurationFile corresponding to the default system configuration location.



51
52
53
# File 'lib/frecon/configuration_file.rb', line 51

def self.system
  self.new(File.join("", "etc", "frecon", "config.yml"))
end

.userObject

Public: Create a new ConfigurationFile corresponding to the default user configuration location.



57
58
59
# File 'lib/frecon/configuration_file.rb', line 57

def self.user
  self.new(File.join(config_directory, "frecon.yml"))
end

Instance Method Details

#readObject

Public: Read from the file and generate a Configuration from the YAML data therein.

Returns a Configuration representing the file’s data or nil if it didn’t exist.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/frecon/configuration_file.rb', line 31

def read
  begin
    data = open(@filename, "rb") do |io|
      io.read
    end

    Configuration.new(YAML.load(data))
  rescue Errno::ENOENT
    nil
  end
end