Class: CLIUtils::Prefs

Inherits:
Object
  • Object
show all
Includes:
Messaging
Defined in:
lib/cliutils/prefs.rb

Overview

Engine to derive preferences from a YAML file, deliver those to a user via a prompt, and collect the results.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Messaging

#messenger

Constructor Details

#initialize(data, configurator = nil) ⇒ void

Reads prompt data from and stores it.

Parameters:

  • data (<String, Hash, Array>)

    Filepath to YAML, Hash, or Array

  • configurator (Configurator) (defaults to: nil)

    Source of defailt values



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cliutils/prefs.rb', line 47

def initialize(data, configurator = nil)
  @prompts = []
  @configurator = configurator
  case data
  when String
    if File.file?(data)
      @config_path = data
      data = YAML.load_file(data).deep_symbolize_keys
      @prompts = _generate_prefs(data)
    else
        fail "Invalid configuration file: #{ data }"
    end
  when Hash
    @config_path = nil
    data = { prompts: data } unless data.keys[0] == :prompts
    data.deep_symbolize_keys!
    @prompts = _generate_prefs(data)
  when Array
    @config_path = nil
    data = { prompts: data }.deep_symbolize_keys
    @prompts = _generate_prefs(data)
  else
    fail 'Invalid configuration data'
  end
end

Class Attribute Details

.registered_actionsHash

Stores any actions registered for all instances of Prefs.

Returns:



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

def registered_actions
  @registered_actions
end

.registered_behaviorsHash

Stores any behaviors registered for all instances of Prefs.

Returns:



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

def registered_behaviors
  @registered_behaviors
end

.registered_validatorsHash

Stores any validators registered for all instances of Prefs.

Returns:



36
37
38
# File 'lib/cliutils/prefs.rb', line 36

def registered_validators
  @registered_validators
end

Instance Attribute Details

#config_pathString (readonly)

Stores the filepath (if it exists) to the prefs file.

Returns:



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

def config_path
  @config_path
end

#configuratorConfigurator (readonly)

Stores a Configurator instance.

Returns:



16
17
18
# File 'lib/cliutils/prefs.rb', line 16

def configurator
  @configurator
end

#promptsArray (readonly)

Stores answers to prompt questions.

Returns:

  • (Array)


20
21
22
# File 'lib/cliutils/prefs.rb', line 20

def prompts
  @prompts
end

Class Method Details

._deregister_asset(symbol, type) ⇒ void

This method returns an undefined value.

Utility function to deregister an asset.

Parameters:

  • symbol (Symbol)

    The asset to remove

  • type (Fixnum)

    A Pref asset type



187
188
189
190
191
192
193
194
195
196
# File 'lib/cliutils/prefs.rb', line 187

def self._deregister_asset(symbol, type)
  case type
  when Pref::ASSET_TYPE_ACTION
    CLIUtils::Prefs.registered_actions.delete(symbol)
  when Pref::ASSET_TYPE_BEHAVIOR
    CLIUtils::Prefs.registered_behaviors.delete(symbol)
  when Pref::ASSET_TYPE_VALIDATOR
    CLIUtils::Prefs.registered_validators.delete(symbol)
  end
end

._register_asset(path, type) ⇒ void

This method returns an undefined value.

Utility function to register an asset and associate it with the Prefs eigenclass.

Parameters:

  • path (String)

    The filepath to the asset

  • type (Fixnum)

    A Pref asset type

Raises:

  • (StandardError)

    if the asset is unknown



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cliutils/prefs.rb', line 204

def self._register_asset(path, type)
  if File.file?(path)
    class_name = File.basename(path, '.*').camelize
    case type
    when Pref::ASSET_TYPE_ACTION
      k = class_name.sub('Action', '').to_sym
      unless CLIUtils::Prefs.registered_actions.key?(k)
        h = { k => { class: class_name, path: path } }
        CLIUtils::Prefs.registered_actions.merge!(h)
      end
    when Pref::ASSET_TYPE_BEHAVIOR
      k = class_name.sub('Behavior', '').to_sym
      unless CLIUtils::Prefs.registered_behaviors.key?(k)
        h = { k => { class: class_name, path: path } }
        CLIUtils::Prefs.registered_behaviors.merge!(h)
      end
    when Pref::ASSET_TYPE_VALIDATOR
      k = class_name.sub('Validator', '').to_sym
      unless CLIUtils::Prefs.registered_validators.key?(k)
        h = { k => { class: class_name, path: path } }
        CLIUtils::Prefs.registered_validators.merge!(h)
      end
    end
  else
    fail "Registration failed because of unknown filepath: #{ path }"
  end
end

.deregister_action(symbol) ⇒ void

This method returns an undefined value.

Deregister an action based on the symbol it was stored under.

Parameters:

  • symbol (Symbol)

    The symbol to remove



96
97
98
# File 'lib/cliutils/prefs.rb', line 96

def self.deregister_action(symbol)
  _deregister_asset(symbol, Pref::ASSET_TYPE_ACTION)
end

.deregister_behavior(symbol) ⇒ void

This method returns an undefined value.

Deregister a behavior based on the symbol it was stored under.

Parameters:

  • symbol (Symbol)

    The symbol to remove



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

def self.deregister_behavior(symbol)
  _deregister_asset(symbol, Pref::ASSET_TYPE_BEHAVIOR)
end

.deregister_validator(symbol) ⇒ void

This method returns an undefined value.

Deregister a validator based on the symbol it was stored under.

Parameters:

  • symbol (Symbol)

    The symbol to remove



112
113
114
# File 'lib/cliutils/prefs.rb', line 112

def self.deregister_validator(symbol)
  _deregister_asset(symbol, Pref::ASSET_TYPE_VALIDATOR)
end

.register_action(path) ⇒ void

This method returns an undefined value.

Register an action.

Parameters:

  • path (String)

    The filepath of the action



119
120
121
# File 'lib/cliutils/prefs.rb', line 119

def self.register_action(path)
  self._register_asset(path, Pref::ASSET_TYPE_ACTION)
end

.register_behavior(path) ⇒ void

This method returns an undefined value.

Register a behavior.

Parameters:

  • path (String)

    The filepath of the behavior



126
127
128
# File 'lib/cliutils/prefs.rb', line 126

def self.register_behavior(path)
  _register_asset(path, Pref::ASSET_TYPE_BEHAVIOR)
end

.register_validator(path) ⇒ void

This method returns an undefined value.

Register a validator.

Parameters:

  • path (String)

    The filepath of the validator



133
134
135
# File 'lib/cliutils/prefs.rb', line 133

def self.register_validator(path)
  _register_asset(path, Pref::ASSET_TYPE_VALIDATOR)
end

Instance Method Details

#askvoid

This method returns an undefined value.

Runs through all of the prompt questions and collects answers from the user. Note that all questions w/o prerequisites are examined first; once those are complete, questions w/ prerequisites are examined.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cliutils/prefs.rb', line 78

def ask
  # First, deliver all the prompts that don't have
  # any prerequisites.
  @prompts.reject { |p| p.prereqs }.each do |p|
    _deliver_prompt(p)
  end

  # After the "non-prerequisite" prompts are delivered,
  # deliver any that require prerequisites.
  @prompts.select { |p| p.prereqs }.each do |p|
    _deliver_prompt(p) if _prereqs_fulfilled?(p)
  end
end