Sha256: 21fcce613883d735a998525ce3a820016761b3c6ee7f8ae8e263759c8ea0a127

Contents?: true

Size: 928 Bytes

Versions: 4

Compression:

Stored size: 928 Bytes

Contents

require_relative "./base_adapter"
require "workbook"
require "active_support/inflector"

module ActiveWorksheet
  module Adapters
    class FileAdapter < BaseAdapter
      ADAPTER_NAME = "File"

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

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

      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

      def find(index)
        all[index]
      end

      def first
        find(0)
      end

      def last
        find(-1)
      end

      def count
        all.count
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_worksheet-0.1.3 lib/active_worksheet/adapters/file_adapter.rb
active_worksheet-0.1.2 lib/active_worksheet/adapters/file_adapter.rb
active_worksheet-0.1.1 lib/active_worksheet/adapters/file_adapter.rb
active_worksheet-0.1.0 lib/active_worksheet/adapters/file_adapter.rb