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

#initialize(path) ⇒ void

Initializes configuration from a flat file.

Parameters:

  • path (String)

    The filepath to the config YAML



36
37
38
39
40
41
42
43
44
45
# File 'lib/cliutils/configurator.rb', line 36

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

  if File.file?(_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

#method_missing(name, *args) { ... } ⇒ Hash

Hook that fires when a non-existent method is called. Allows this module to return data from the config Hash when given a method name that matches a key.

Parameters:

  • name (<String, Symbol>)

    The name of the method

  • args (Array)

    The arguments

Yields:

  • if a block is passed

Returns:

  • (Hash)

    The hash with the method’s name as key



117
118
119
120
121
122
123
# File 'lib/cliutils/configurator.rb', line 117

def method_missing(name, *args, &block)
  if name[-1,1] == '='
    @data[name[0..-2].to_sym] = args[0]
  else
    @data[name.to_sym] ||= {}
  end
end

Instance Attribute Details

#config_pathString (readonly)

Stores the path to the configuration file.

Returns:



22
23
24
# File 'lib/cliutils/configurator.rb', line 22

def config_path
  @config_path
end

#current_versionSymbol

Stores the Configurator key that refers to the current configuration version.

Returns:

  • (Symbol)


12
13
14
# File 'lib/cliutils/configurator.rb', line 12

def current_version
  @current_version
end

#dataHash (readonly)

Stores the configuration data itself.

Returns:



26
27
28
# File 'lib/cliutils/configurator.rb', line 26

def data
  @data
end

#last_versionSymbol

Stores the Configurator key that refers to the value at which the app last changed config versions.

Returns:

  • (Symbol)


18
19
20
# File 'lib/cliutils/configurator.rb', line 18

def last_version
  @last_version
end

#version_sectionSymbol

Stores the section that contains the version keys.

Returns:

  • (Symbol)


31
32
33
# File 'lib/cliutils/configurator.rb', line 31

def version_section
  @version_section
end

Instance Method Details

#add_section(section_name) ⇒ void

This method returns an undefined value.

Adds a new section to the config file (if it doesn’t already exist).

Parameters:

  • section_name (String)

    The section to add



51
52
53
54
55
56
57
# File 'lib/cliutils/configurator.rb', line 51

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

#backupString

Convenience method to backup the configuration file in the same directory that the original inhabits.

Returns:

  • (String)

    The backed-up filepath



62
63
64
65
66
67
68
69
# File 'lib/cliutils/configurator.rb', line 62

def backup
  backup_path = ''
  unless @config_path.nil? || @config_path.empty?
    backup_path = "#{ @config_path }-#{ Time.now.to_i }"
    FileUtils.cp(@config_path, backup_path)
  end
  backup_path
end

#compare_versionvoid

This method returns an undefined value.

Compares the current version (if it exists) to the last version that needed a configuration change (if it exists). Assuming they exist and that the current version is behind the “last-config” version, execute a passed block.



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

def compare_version
  c_version = Gem::Version.new(@current_version)
  l_version = Gem::Version.new(@last_version)

  if @current_version.nil? || c_version < l_version
    yield @current_version, @last_version
  end
end

#delete_section(section_name) ⇒ void

This method returns an undefined value.

Removes a section to the config file (if it exists).

Parameters:

  • section_name (String)

    The section to remove



89
90
91
92
93
94
95
# File 'lib/cliutils/configurator.rb', line 89

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

#ingest_prefs(prefs) ⇒ void

This method returns an undefined value.

Ingests a Prefs class and adds its answers to the configuration data.

Parameters:

  • prefs (Prefs)

    The Prefs class to examine



101
102
103
104
105
106
107
108
# File 'lib/cliutils/configurator.rb', line 101

def ingest_prefs(prefs)
  fail 'Invaid Prefs class' unless prefs.kind_of?(Prefs)
  prefs.prompts.each do |p|
    section_sym = p.config_section.to_sym
    add_section(section_sym) unless @data.key?(section_sym)
    @data[section_sym].merge!(p.config_key.to_sym => p.answer)
  end
end

#resetvoid

This method returns an undefined value.

Clears the configuration data.



127
128
129
# File 'lib/cliutils/configurator.rb', line 127

def reset
  @data = {}
end

#savevoid

This method returns an undefined value.

Saves the configuration data to the previously stored flat file.



134
135
136
137
138
# File 'lib/cliutils/configurator.rb', line 134

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