class Confinicky::ConfigurationFile

A singleton model representing the configuration YAML utilized by Confinicky.

Constants

PATH
SUPPORTED_KEYS

Public Class Methods

force_config!(configuration) click to toggle source

Forces a configuration, primarily for testing purposes.

# File lib/confinicky/configuration_file.rb, line 52
def self.force_config!(configuration)
  @@configuration = configuration
end
path_for_key(key: nil) click to toggle source

Retrieves the value of the environment variables file path.

# File lib/confinicky/configuration_file.rb, line 15
def self.path_for_key(key: nil)
  self.raise_if_key_not_supported(key)
  self.get_configuration[:files][key]
end
set_path_for_key(path: "", key: nil) click to toggle source

Set’s the aliases file path.

# File lib/confinicky/configuration_file.rb, line 22
def self.set_path_for_key(path: "", key: nil)
  self.raise_if_key_not_supported(key)
  self.get_configuration[:files][key] = path if self.valid_path?(path)
end
setup?() click to toggle source

Returns true if the configuration file exists along with all specified paths.

# File lib/confinicky/configuration_file.rb, line 44
def self.setup?
  File.exists?(PATH) &&
  File.exists?(self.get_configuration[:files][:env]) &&
  File.exists?(self.get_configuration[:files][:aliases])
end
table() click to toggle source

Returns a terminal table displaying the current configuration.

# File lib/confinicky/configuration_file.rb, line 35
def self.table
  Terminal::Table.new(title: "Configuration", headings: ['type', 'path']) do |t|
    self.get_configuration[:files].each {|k,v| t.add_row [k.to_s,v]}
  end
end
write!() click to toggle source

Writes the output to the appropriate YAML file.

# File lib/confinicky/configuration_file.rb, line 29
def self.write!
  File.open(PATH, 'w') {|f| f.write self.get_configuration.to_yaml }
end

Private Class Methods

get_configuration() click to toggle source

Lazy loading to retrieve the contents of the configuration file.

# File lib/confinicky/configuration_file.rb, line 77
def self.get_configuration
  @@configuration || self.load_configuration
end
load_configuration() click to toggle source

Loads the configuration file or generates a default hash.

# File lib/confinicky/configuration_file.rb, line 83
def self.load_configuration
  return @@configuration = YAML::load_file(PATH) if File.exists?(PATH)
  @@configuration = {files: {aliases: "#{ENV['HOME']}/aliases", env: "#{ENV['HOME']}/env"}}
end
raise_if_key_not_supported(key) click to toggle source

Raises an error if a specified key is not supported.

# File lib/confinicky/configuration_file.rb, line 60
def self.raise_if_key_not_supported(key)
  raise "Unsupported key supplied to configuration: "+key.to_s unless SUPPORTED_KEYS.include?(key)
end
valid_path?(path) click to toggle source

Returns false if the path is blank and raises an error if the supplied path does not point to an existing file.

# File lib/confinicky/configuration_file.rb, line 67
def self.valid_path?(path)
  if (!path.nil? && path.length > 0)
    raise "File does not exist at: #{path}" unless File.exists?(path)
    return true
  end
  false
end