Class: ActiveWorksheet::Adapters::FileAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/active_worksheet/adapters/file_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
"File"

Instance Attribute Summary

Attributes inherited from BaseAdapter

#authorization, #source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#new

Constructor Details

#initialize(source: nil) ⇒ FileAdapter

Returns a new instance of FileAdapter.



10
11
12
13
14
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 10

def initialize(source: nil)
  super(source: source)

  @workbook = Workbook::Book.open(source)
end

Instance Method Details

#allObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 16

def all
  table = @workbook.sheet.table

  headers = table.shift.map do |header|
    ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.parameterize(header.value))
  end

  table.map do |row|
    row.each_with_index.reduce({}) do |result, (cell, index)|
      result[headers[index]] = cell.value
      result
    end
  end
end

#countObject



43
44
45
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 43

def count
  all.count
end

#find(index) ⇒ Object



31
32
33
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 31

def find(index)
  all[index]
end

#firstObject



35
36
37
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 35

def first
  find(0)
end

#lastObject



39
40
41
# File 'lib/active_worksheet/adapters/file_adapter.rb', line 39

def last
  find(-1)
end