Object
Represents the abstract collection of Chunks.
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
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
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
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
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
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
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
Generated with the Darkfish Rdoc Generator 2.