lib/cliutils/prefs.rb in cliutils-1.2.2 vs lib/cliutils/prefs.rb in cliutils-1.2.3
- old
+ new
@@ -14,39 +14,39 @@
# @return [Configurator]
attr_reader :configurator
# Stores answers to prompt questions.
# @return [Array]
- attr_reader :prefs
+ attr_reader :prompts
# Reads prompt data from and stores it.
# @param [<String, Hash, Array>] data Filepath to YAML, Hash, or Array
# @param [Configurator] configurator The configurator to take default values from
# @return [void]
def initialize(data, configurator = nil)
@answers = []
@configurator = configurator
- @prefs = []
+ @prompts = []
case data
when String
if File.exists?(data)
@config_path = data
data = YAML::load_file(data).deep_symbolize_keys
- @prefs = _generate_prefs(data)
+ @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!
- @prefs = _generate_prefs(data)
+ @prompts = _generate_prefs(data)
when Array
@config_path = nil
data = {:prompts => data}.deep_symbolize_keys
- @prefs = _generate_prefs(data)
+ @prompts = _generate_prefs(data)
else
fail 'Invalid configuration data'
end
end
@@ -54,15 +54,15 @@
# answers from the user. Note that all questions w/o
# prerequisites are examined first; once those are
# complete, questions w/ prerequisites are examined.
# @return [void]
def ask
- @prefs.reject { |p| p.prereqs }.each do |p|
+ @prompts.reject { |p| p.prereqs }.each do |p|
_deliver_prompt(p)
end
- @prefs.find_all { |p| p.prereqs }.each do |p|
+ @prompts.find_all { |p| p.prereqs }.each do |p|
_deliver_prompt(p) if _prereqs_fulfilled?(p)
end
end
private
@@ -71,13 +71,13 @@
# question (taking into account any options).
# @param [Hash] p The prompt
# @return [void]
def _deliver_prompt(p)
default = p.default
-
+
unless @configurator.nil?
- unless @configurator.data[p.section.to_sym].nil?
+ unless @configurator.data[p.config_section.to_sym].nil?
config_val = @configurator.data[p.config_section.to_sym][p.config_key.to_sym]
default = config_val unless config_val.nil?
end
end
@@ -107,10 +107,10 @@
# @param [Hash] p The prompt
# @return [void]
def _prereqs_fulfilled?(p)
ret = true
p.prereqs.each do |req|
- a = @prefs.detect do |answer|
+ a = @prompts.detect do |answer|
answer.config_key == req[:config_key] &&
answer.answer == req[:config_value]
end
ret = false if a.nil?
end