Class: DataMapper::ChunkedQuery::Chunks
- Inherits:
-
Object
- Object
- DataMapper::ChunkedQuery::Chunks
- Includes:
- Enumerable
- Defined in:
- lib/dm-chunked_query/chunks.rb
Overview
Represents the abstract collection of Chunks.
Instance Attribute Summary collapse
-
#per_chunk ⇒ Object
readonly
The number of resources per chunk.
Instance Method Summary collapse
-
#[](key) ⇒ DataMapper::Collection
Provides random access to chunks.
-
#at(index) ⇒ DataMapper::Collection
Accesses a chunk at a specific index.
-
#chunk_at(index, span = 1) ⇒ DataMapper::Collection
protected
Creates a chunk of resources.
-
#count ⇒ Integer
Counts how many underlying resources are available.
-
#each {|chunk| ... } ⇒ Enumerator
Enumerates over each chunk in the collection of Chunks.
-
#first(n = 1) ⇒ DataMapper::Collection
Returns the first chunk(s).
-
#initialize(query, per_chunk) ⇒ Chunks
constructor
Creates a new collection of Chunks.
-
#length ⇒ Integer
(also: #size)
Calculate the number of Chunks.
Constructor Details
#initialize(query, per_chunk) ⇒ Chunks
Creates a new collection of Chunks.
22 23 24 25 |
# File 'lib/dm-chunked_query/chunks.rb', line 22 def initialize(query,per_chunk) @query = query @per_chunk = per_chunk end |
Instance Attribute Details
#per_chunk ⇒ Object (readonly)
The number of resources per chunk
11 12 13 |
# File 'lib/dm-chunked_query/chunks.rb', line 11 def per_chunk @per_chunk end |
Instance Method Details
#[](key) ⇒ DataMapper::Collection
Provides random access to chunks.
36 37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ DataMapper::Collection
Accesses a chunk at a specific index.
57 58 59 |
# File 'lib/dm-chunked_query/chunks.rb', line 57 def at(index) chunk_at(index.to_i) end |
#chunk_at(index, span = 1) ⇒ DataMapper::Collection (protected)
Creates a chunk of resources.
141 142 143 |
# File 'lib/dm-chunked_query/chunks.rb', line 141 def chunk_at(index,span=1) @query[(index * @per_chunk), (span * @per_chunk)] end |
#count ⇒ Integer
Counts how many underlying resources are available.
111 112 113 |
# File 'lib/dm-chunked_query/chunks.rb', line 111 def count @count ||= @query.count end |
#each {|chunk| ... } ⇒ Enumerator
Enumerates over each chunk in the collection of Chunks.
95 96 97 98 99 100 101 102 103 |
# 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) ⇒ DataMapper::Collection
Returns the first chunk(s).
75 76 77 78 79 80 81 |
# 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 ⇒ Integer Also known as: size
Calculate the number of Chunks.
121 122 123 |
# File 'lib/dm-chunked_query/chunks.rb', line 121 def length @length ||= (count.to_f / @per_chunk).ceil end |