module DataReader

Constants

VERSION

Public Instance Methods

data_contents() click to toggle source

Returns the contents that have been read in from a loaded file data file.

# File lib/data_reader.rb, line 22
def data_contents
  return @data_contents if @data_contents

  nil
end
data_path() click to toggle source

Returns the path that will be used to read data files.

# File lib/data_reader.rb, line 14
def data_path
  return @data_path if @data_path
  return default_data_path if respond_to? :default_data_path

  nil
end
data_path=(path) click to toggle source

Sets the path to use when reading data files.

# File lib/data_reader.rb, line 9
def data_path=(path)
  @data_path = path
end
include_data(file) click to toggle source
# File lib/data_reader.rb, line 38
def include_data(file)
  filename = Pathname.new(file).absolute? ? file : "#{data_path}/#{file}"
  ERB.new(IO.read(filename)).result(binding) if File.exist?(filename)
end
load(file_list) click to toggle source
# File lib/data_reader.rb, line 28
def load(file_list)
  files = file_list.include?(',') ? file_list.split(',') : [file_list]
  files = files.collect(&:strip)

  @data_contents = files.inject({}) do |all_data, file|
    data = include_key(::YAML.safe_load(include_data(file)))
    all_data.merge!(data) if data
  end
end

Private Instance Methods

include_key(data) click to toggle source
# File lib/data_reader.rb, line 45
def include_key(data)
  include_data = {}

  if data.key?('_include_')
    [data['_include_']].flatten.each do |file_path|
      include_data.merge!(load(file_path))
    end
  end

  data.delete('_include_')
  data.merge!(include_data)

  data.each do |key, value|
    data[key] = include_key(value) if value.is_a?(Hash)
  end

  data
end