Sha256: ca0eb37937c5371ec112a75ffb819b844549f43ea4831e18c4f52cefd759f2b4

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

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

      private

      def header_row
        1 + t.properties.skip
      end

      def first_data_row
        1 + header_row
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
remote_table-1.1.4 lib/remote_table/format/mixins/rooable.rb
remote_table-1.1.3 lib/remote_table/format/mixins/rooable.rb
remote_table-1.1.2 lib/remote_table/format/mixins/rooable.rb
remote_table-1.1.1 lib/remote_table/format/mixins/rooable.rb
remote_table-1.1.0 lib/remote_table/format/mixins/rooable.rb
remote_table-1.0.3 lib/remote_table/format/mixins/rooable.rb
remote_table-1.0.2 lib/remote_table/format/mixins/rooable.rb
remote_table-1.0.1 lib/remote_table/format/mixins/rooable.rb
remote_table-1.0.0 lib/remote_table/format/mixins/rooable.rb