Module: DataReader

Defined in:
lib/data_reader.rb,
lib/data_reader/version.rb

Constant Summary collapse

VERSION =
"1.1.0".freeze

Instance Method Summary collapse

Instance Method Details

#data_pathObject



11
12
13
14
15
16
# File 'lib/data_reader.rb', line 11

def data_path
  return @data_path if @data_path
  return default_data_path if respond_to? :default_data_path

  nil
end

#data_path=(path) ⇒ Object



7
8
9
# File 'lib/data_reader.rb', line 7

def data_path=(path)
  @data_path = path
end

#data_sourceObject



18
19
20
21
22
# File 'lib/data_reader.rb', line 18

def data_source
  return @data_source if @data_source

  nil
end

#include_data(file) ⇒ Object



34
35
36
37
# File 'lib/data_reader.rb', line 34

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) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/data_reader.rb', line 24

def load(file_list)
  files = file_list.include?(',') ? file_list.split(',') : [file_list]
  files = files.collect(&:strip)

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