class DataMapper::ChunkedQuery::Chunks

Represents the abstract collection of Chunks.

Attributes

per_chunk[R]

The number of resources per chunk

Public Class Methods

new(query,per_chunk) click to toggle source

Creates a new collection of Chunks.

@param [DataMapper::Model, DataMapper::Collection] query

The model or collection to access via chunks.

@param [Integer] #per_chunk

The number of records per-chunk.
# File lib/dm-chunked_query/chunks.rb, line 22
def initialize(query,per_chunk)
  @query = query
  @per_chunk = per_chunk
end

Public Instance Methods

[](key) click to toggle source

Provides random access to chunks.

@param [Range<Integer>, Integer] key

The index or range of indices to access.

@return [DataMapper::Collection]

A collection of resources at the given index or indices.
# File lib/dm-chunked_query/chunks.rb, line 36
def [](key)
  case key
  when Range
    index = key.first
    span = key.to_a.size

    chunk_at(index,span)
  when Integer
    chunk_at(key)
  end
end
at(index) click to toggle source

Accesses a chunk at a specific index.

@param [#to_i] index

The index to access.

@return [DataMapper::Collection]

The chunk of resources at the given index.
# File lib/dm-chunked_query/chunks.rb, line 57
def at(index)
  chunk_at(index.to_i)
end
count() click to toggle source

Counts how many underlying resources are available.

@return [Integer]

The total number of resources.
# File lib/dm-chunked_query/chunks.rb, line 111
def count
  @count ||= @query.count
end
each() { |chunk_at(index)| ... } click to toggle source

Enumerates over each chunk in the collection of Chunks.

@yield [chunk]

The given block will be passed each chunk.

@yieldparam [DataMapper::Collection] chunk

The collection of resources that makes up a chunk.

@return [Enumerator]

If no block is given, an Enumerator object will be returned.
# File lib/dm-chunked_query/chunks.rb, line 95
def each
  return enum_for(:each) unless block_given?

  length.times do |index|
    yield chunk_at(index)
  end

  return self
end
first(n=1) click to toggle source

Returns the first chunk(s).

@param [Integer] n

The number of sub-chunks to include.

@return [DataMapper::Collection]

The first chunk of resources.

@raise [ArgumentError]

The number of sub-chunks was negative.

@since 0.2.0

# File lib/dm-chunked_query/chunks.rb, line 75
def first(n=1)
  if n >= 0
    chunk_at(0,n)
  else
    raise(ArgumentError,"negative array size")
  end
end
length() click to toggle source

Calculate the number of Chunks.

@return [Integer]

The number of available Chunks.
# File lib/dm-chunked_query/chunks.rb, line 121
def length
  @length ||= (count.to_f / @per_chunk).ceil
end
Also aliased as: size
size()
Alias for: length

Protected Instance Methods

chunk_at(index,span=1) click to toggle source

Creates a chunk of resources.

@param [Integer] index

The index of the chunk.

@param [Integer] span

The number of chunks the chunk should span.

@return [DataMapper::Collection]

The collection of resources that makes up the chunk.
# File lib/dm-chunked_query/chunks.rb, line 141
def chunk_at(index,span=1)
  @query[(index * @per_chunk), (span * @per_chunk)]
end