Sha256: 89002bdd1e15ca543d1b93caf032f538834dfea1281dd5daab5a4980f4178e73

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module GoogleCells
  class CellSelector

    class RowSelector < CellSelector

      DEFAULT_BATCH_SIZE = 10

      def find_each(opts={}, &block)
        size = (opts[:batch_size] || DEFAULT_BATCH_SIZE).to_i
        rnum = @min_row
        loop do
          last = [rnum + size, @max_row].min
          break if rnum > last
          get_cells(rnum, last).each do |cells|
            yield Row.new(cells:cells, number:rnum, worksheet:worksheet)
            rnum += 1
          end
        end
      end

      def each
        all.each{|c| yield c}
      end

      def all
        @rows = []
        self.find_each(batch_size:@max_row - @min_row){|r| @rows << r}
        @rows
      end

      def first
        all.first
      end

      def from(num)
        @min_row = num.to_i
        self
      end

      def to(num)
        @max_row = num.to_i
        self
      end

      private

      def get_cells(start, last)
        cells = []
        each_entry(worksheet.cells_uri, 'return-empty' => 'true', 
          'min-row' => start.to_s, 'max-row' => last.to_s) do |entry|
          gscell = entry.css("gs|cell")[0]
          cell = Cell.new(
            id: entry.css("id").text,
            title: entry.css("title").text,
            value: gscell.inner_text,
            row: gscell["row"].to_i,
            col: gscell["col"].to_i,
            edit_url: entry.css("link[rel='edit']")[0]["href"],
            input_value: gscell["inputValue"],
            numeric_value: gscell["numericValue"],
            worksheet: self.worksheet
          )
          cells[cell.row - start] ||= []
          cells[cell.row - start][cell.col - 1] = cell
        end
        cells
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google-cells-0.3.0 lib/google_cells/cell_selector/row_selector.rb