Class: CLIUtils::Prefs

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

Overview

PrefManager Class

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from PrettyIO

#color_chart, #debug, #error, included, #info, #info_block, #log, #prompt, #section, #section_block, #success, #warn, wrap, wrap_at, wrap_limit

Constructor Details

- (Prefs) initialize(data)

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

Reads prompt data from YAML file.
@return Void
----------------------------------------------------


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cliutils/prefs.rb', line 24

def initialize(data)
  @answers = []
  @prompts = {}

  case data
  when String
    if File.exists?(data)
      @config_path = data

      prompts = YAML::load_file(data)
      @prompts.deep_merge!(prompts).deep_symbolize_keys!
    else
      fail "Invalid configuration file: #{ yaml_path }"
    end
  when Array
    @config_path = nil

    prompts = {:prompts => data}
    @prompts.deep_merge!(prompts).deep_symbolize_keys!
  else
    fail 'Invalid configuration data'
  end
end

Instance Attribute Details

- (Object) answers (readonly)

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


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

def answers
  @answers
end

- (Object) config_path (readonly)

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


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

def config_path
  @config_path
end

- (Object) prompts (readonly)

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


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

def prompts
  @prompts
end

Instance Method Details

- (Object) ask


ask method

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


57
58
59
60
61
62
63
64
65
# File 'lib/cliutils/prefs.rb', line 57

def ask
  @prompts[:prompts].reject { |p| p[:requirements] }.each do |p|
    _deliver_prompt(p)
  end

  @prompts[:prompts].find_all { |p| p[:requirements] }.each do |p|
    _deliver_prompt(p) if _requirements_fulfilled?(p)
  end
end