lib/cliutils/prefs.rb in cliutils-1.2.3 vs lib/cliutils/prefs.rb in cliutils-1.2.4

- old
+ new

@@ -1,7 +1,7 @@ require 'cliutils/prefs/pref' -require 'cliutils/pretty-io' +require 'cliutils/pretty_io' module CLIUtils # Engine to derive preferences from a YAML file, deliver # those to a user via a prompt, and collect the results. class Prefs @@ -11,41 +11,41 @@ attr_reader :config_path # Stores a Configurator instance. # @return [Configurator] attr_reader :configurator - + # Stores answers to prompt questions. # @return [Array] 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 + # @param [Configurator] configurator Source of defailt values # @return [void] def initialize(data, configurator = nil) @answers = [] @configurator = configurator @prompts = [] case data when String - if File.exists?(data) + if File.exist?(data) @config_path = data - data = YAML::load_file(data).deep_symbolize_keys + data = YAML.load_file(data).deep_symbolize_keys @prompts = _generate_prefs(data) else - fail "Invalid configuration file: #{ data }" + fail "Invalid configuration file: #{ data }" end when Hash @config_path = nil - data = {:prompts => data} unless data.keys[0] == :prompts + 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 + data = { prompts: data }.deep_symbolize_keys @prompts = _generate_prefs(data) else fail 'Invalid configuration data' end end @@ -57,12 +57,12 @@ # @return [void] def ask @prompts.reject { |p| p.prereqs }.each do |p| _deliver_prompt(p) end - - @prompts.find_all { |p| p.prereqs }.each do |p| + + @prompts.select { |p| p.prereqs }.each do |p| _deliver_prompt(p) if _prereqs_fulfilled?(p) end end private @@ -73,20 +73,20 @@ # @return [void] def _deliver_prompt(p) default = p.default unless @configurator.nil? - unless @configurator.data[p.config_section.to_sym].nil? - config_val = @configurator.data[p.config_section.to_sym][p.config_key.to_sym] + section_sym = p.config_section.to_sym + unless @configurator.data[section_sym].nil? + config_val = @configurator.data[section_sym][p.config_key.to_sym] default = config_val unless config_val.nil? end end valid_option_chosen = false until valid_option_chosen response = prompt(p.prompt, default) - if p.validate(response) valid_option_chosen = true p.answer = p.evaluate_behaviors(response) else messenger.error(p.last_error_message) @@ -107,10 +107,10 @@ # @param [Hash] p The prompt # @return [void] def _prereqs_fulfilled?(p) ret = true p.prereqs.each do |req| - a = @prompts.detect do |answer| + a = @prompts.find do |answer| answer.config_key == req[:config_key] && answer.answer == req[:config_value] end ret = false if a.nil? end