Sha256: 2f55f22205109c73db19013401ea5824b4a6eb78ccf7780c5f68e1dfd6bbcc09

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

class RemoteTable
  module RooSpreadsheet
    def each_row(&block)
      oo = roo_klass.new(path, nil, :ignore)
      oo.default_sheet = sheet.is_a?(Numeric) ? oo.sheets[sheet] : sheet
      column_references = Hash.new
      if headers == false
        # zero-based numeric keys
        for col in (1..oo.last_column)
          column_references[col] = col - 1
        end
      elsif headers.is_a? Array
        # names
        for col in (1..oo.last_column)
          column_references[col] = headers[col - 1]
        end
      else
        # read headers from the file itself
        for col in (1..oo.last_column)
          column_references[col] = oo.cell(header_row, col)
          column_references[col] = oo.cell(header_row - 1, col) if column_references[col].blank? # look up
        end
      end
      first_data_row.upto(oo.last_row) do |raw_row|
        ordered_hash = ActiveSupport::OrderedHash.new
        for col in (1..oo.last_column)
          next if column_references[col].blank?
          ordered_hash[column_references[col]] = oo.cell(raw_row, col).to_s.gsub(/<[^>]+>/, '').strip
        end
        yield ordered_hash if keep_blank_rows or ordered_hash.any? { |k, v| v.present? }
      end
    end

    private

    def header_row
      1 + skip.to_i
    end

    def first_data_row
      1 + header_row
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
remote_table-0.2.32 lib/remote_table/file/roo_spreadsheet.rb
remote_table-0.2.31 lib/remote_table/file/roo_spreadsheet.rb
remote_table-ruby19-0.2.30 lib/remote_table/file/roo_spreadsheet.rb
remote_table-0.2.30 lib/remote_table/file/roo_spreadsheet.rb