Class: UserChoices::YamlConfigFileSource

Inherits:
FileSource show all
Defined in:
lib/user-choices/sources.rb

Overview

Use an YAML file as a source of choices. Note: because the YAML parser can produce something out of many typo-filled YAML files, it’s a good idea to check that your file looks like you’d expect before trusting in it. Do that with:

irb> require 'yaml'
irb> YAML.load_file('config.yaml')

Instance Attribute Summary

Attributes inherited from AbstractSource

#external_names

Instance Method Summary collapse

Methods inherited from FileSource

#fill, #from_complete_path, #from_file, #read_into_hash, #source

Methods inherited from AbstractSource

#adjust, #apply, #each_conversion, #fill, #initialize, #source

Constructor Details

This class inherits a constructor from UserChoices::AbstractSource

Instance Method Details

#ensure_array_values_are_strings(a) ⇒ Object



259
260
261
# File 'lib/user-choices/sources.rb', line 259

def ensure_array_values_are_strings(a)
  a.each_with_index { |elt, index| ensure_element_is_string(a, index) }
end

#ensure_element_is_string(collection, key) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/user-choices/sources.rb', line 263

def ensure_element_is_string(collection, key)
  case collection[key]
    when Hash then ensure_hash_values_are_strings(collection[key])
    when Array then ensure_array_values_are_strings(collection[key])
    else collection[key] = collection[key].to_s
  end
end

#ensure_hash_values_are_strings(h) ⇒ Object



255
256
257
# File 'lib/user-choices/sources.rb', line 255

def ensure_hash_values_are_strings(h)
  h.each { |k, v| ensure_element_is_string(h, k) }
end

#format_specific_exception?(ex) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/user-choices/sources.rb', line 249

def format_specific_exception?(ex)
  ex.is_a?(ArgumentError)
end

#format_specific_readingObject

Treat filename as the configuration file. filename is expected to be in the home directory. The home directory is found in the same way Rubygems finds it.



243
244
245
246
247
# File 'lib/user-choices/sources.rb', line 243

def format_specific_reading
  result = YAML.load_file(@path)
  ensure_hash_values_are_strings(result)
  result
end