Class: Bmg::Reader::Excel

Inherits:
Object
  • Object
show all
Includes:
Bmg::Reader
Defined in:
lib/bmg/reader/excel.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  skip: 0
}

Instance Attribute Summary

Attributes included from Bmg::Reader

#type

Instance Method Summary collapse

Methods included from Bmg::Relation

#bind, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_csv, #to_json, #update, #visit, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #union, #unspied

Methods included from Algebra::Shortcuts

#image, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix

Constructor Details

#initialize(type, path, options = {}) ⇒ Excel

Returns a new instance of Excel.



10
11
12
13
14
# File 'lib/bmg/reader/excel.rb', line 10

def initialize(type, path, options = {})
  @type = type
  @path = path
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#eachObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bmg/reader/excel.rb', line 16

def each
  require 'roo'
  xlsx = Roo::Spreadsheet.open(@path, @options)
  headers = nil
  xlsx.sheet(0)
    .each
    .drop(@options[:skip])
    .each_with_index
    .each do |row, i|
      if i==0
        headers = row.map(&:to_sym)
      else
        tuple = (0...headers.size).each_with_object({}){|i,t| t[headers[i]] = row[i] }
        yield(tuple)
      end
    end
end

#to_astObject



34
35
36
# File 'lib/bmg/reader/excel.rb', line 34

def to_ast
  [ :excel, @path, @options ]
end

#to_sObject Also known as: inspect



38
39
40
# File 'lib/bmg/reader/excel.rb', line 38

def to_s
  "(excel #{path})"
end