Class: CLIUtils::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/cliutils/configurator.rb

Overview

Configuration Class

Manages any configuration values and the flat YAML file
into which they get stored.
======================================================

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Configurator) initialize(path)

Methods
====================================================
----------------------------------------------------
initialize method

Initializes configuration from a flat file.
@param path The filepath to the config YAML
@return void
----------------------------------------------------


26
27
28
29
30
31
32
33
34
35
# File 'lib/cliutils/configurator.rb', line 26

def initialize(path)
  _path = File.expand_path(path)
  @config_path = _path
  @data = {}

  if File.exists?(_path)
    data = YAML::load_file(_path)
    @data.deep_merge!(data).deep_symbolize_keys!
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args, &block)


method_missing method

Allows this module to return data from the config
Hash when given a method name that matches a key.
@param name
@param *args
@param &block
@return Hash
----------------------------------------------------


94
95
96
# File 'lib/cliutils/configurator.rb', line 94

def method_missing(name, *args, &block)
  @data[name.to_sym] || @data.merge!(name.to_sym => {})
end

Instance Attribute Details

- (Object) config_path (readonly)

Attributes
====================================================


14
15
16
# File 'lib/cliutils/configurator.rb', line 14

def config_path
  @config_path
end

- (Object) data (readonly)

Attributes
====================================================


14
15
16
# File 'lib/cliutils/configurator.rb', line 14

def data
  @data
end

Instance Method Details

- (Object) add_section(section_name)


add_section method

Adds a new section to the config file (if it doesn't
already exist).
@param section_name The section to add
@return Void
----------------------------------------------------


45
46
47
48
49
50
51
# File 'lib/cliutils/configurator.rb', line 45

def add_section(section_name)
  if !@data.key?(section_name)
    @data[section_name] = {}
  else
    fail "Section already exists: #{ section_name }"
  end
end

- (Object) delete_section(section_name)


delete_section method

Removes a section to the config file (if it exists).
@param section_name The section to remove
@return Void
----------------------------------------------------


60
61
62
63
64
65
66
# File 'lib/cliutils/configurator.rb', line 60

def delete_section(section_name)
  if @data.key?(section_name)
    @data.delete(section_name)
  else
    fail "Cannot delete nonexistent section: #{ section_name }"
  end
end

- (Object) ingest_prefs(prefs)


ingest_prefs method

Ingests a Prefs class and adds its answers to the
configuration data.
@param prefs The Prefs class to examine
@return Void
----------------------------------------------------


76
77
78
79
80
81
82
# File 'lib/cliutils/configurator.rb', line 76

def ingest_prefs(prefs)
  fail 'Invaid Prefs class' if !prefs.kind_of?(Prefs) || prefs.answers.nil?
  prefs.answers.each do |p|
    add_section(p[:section]) unless @data.key?(p[:section])
    @data[p[:section]].merge!(p[:key] => p[:answer])
  end
end

- (Object) reset


reset method

Clears the configuration data.
@return Void
----------------------------------------------------


104
105
106
# File 'lib/cliutils/configurator.rb', line 104

def reset
  @data = {}
end

- (Object) save


save method

Saves the configuration data to the previously
stored flat file.
@return Void
----------------------------------------------------


115
116
117
# File 'lib/cliutils/configurator.rb', line 115

def save
  File.open(@config_path, 'w') { |f| f.write(@data.deep_stringify_keys.to_yaml) }
end