Class: Squib::Import::YamlImporter

Inherits:
Object
  • Object
show all
Includes:
QuantityExploder
Defined in:
lib/squib/import/yaml_importer.rb

Instance Method Summary collapse

Methods included from QuantityExploder

#explode_quantities

Instance Method Details

#import_to_dataframe(import, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/squib/import/yaml_importer.rb', line 8

def import_to_dataframe(import, &block)
  data = import.data.nil? ? File.read(import.file) : import.data
  yml = YAML.load(data)
  data = Squib::DataFrame.new
  # Get a universal list of keys to ensure everything is covered.
  keys = yml.map { |c| c.keys}.flatten.uniq
  keys.each { |k| data[k] = [] } #init arrays
  yml.each do |card|
  # nil value if key isn't set.
  keys.each { |k| data[k] << card[k] }
  end
  unless block.nil?
    data.each do |header, col|
      col.map! do |val|
      block.yield(header, val)
      end
    end
  end
  explode_quantities(data, import.explode)
end