Class: DataMapper::ChunkedQuery::Chunks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dm-chunked_query/chunks.rb

Overview

Represents the abstract collection of Chunks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, per_chunk) ⇒ Chunks

Creates a new collection of Chunks.

Parameters:

  • query (DataMapper::Model, DataMapper::Collection)

    The model or collection to access via chunks.

  • per_chunk (Integer)

    The number of records per-chunk.



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_chunkObject (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.

Parameters:

  • key (Range<Integer>, Integer)

    The index or range of indices to access.

Returns:

  • (DataMapper::Collection)

    A collection of resources at the given index or indices.



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.

Parameters:

  • index (#to_i)

    The index to access.

Returns:

  • (DataMapper::Collection)

    The chunk of resources at the given 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.

Parameters:

  • index (Integer)

    The index of the chunk.

  • span (Integer) (defaults to: 1)

    The number of chunks the chunk should span.

Returns:

  • (DataMapper::Collection)

    The collection of resources that makes up the chunk.



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

#countInteger

Counts how many underlying resources are available.

Returns:

  • (Integer)

    The total number of resources.



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.

Yields:

  • (chunk)

    The given block will be passed each chunk.

Yield Parameters:

  • chunk (DataMapper::Collection)

    The collection of resources that makes up a chunk.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



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).

Parameters:

  • n (Integer) (defaults to: 1)

    The number of sub-chunks to include.

Returns:

  • (DataMapper::Collection)

    The first chunk of resources.

Raises:

  • (ArgumentError)

    The number of sub-chunks was negative.

Since:

  • 0.2.0



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

#lengthInteger Also known as: size

Calculate the number of Chunks.

Returns:

  • (Integer)

    The number of available Chunks.



121
122
123
# File 'lib/dm-chunked_query/chunks.rb', line 121

def length
  @length ||= (count.to_f / @per_chunk).ceil
end