class Ampere::Collection

Collections are search results from queries. They can be used like arrays, but you cannot add anything to them.

Attributes

model[R]
raw_array[R]

Public Class Methods

new(model_class, array = []) click to toggle source

Instance methods ########################################################

# File lib/ampere/collection.rb, line 14
def initialize(model_class, array = [])
  @raw_array = array
  @model = model_class
end

Public Instance Methods

==(other) click to toggle source
# File lib/ampere/collection.rb, line 50
def ==(other)
  if other.is_a?(Array) then
    to_a == other
  end
end
[](idx) click to toggle source

Index into the search results. Lazily loads models when they’re accessed.

# File lib/ampere/collection.rb, line 31
def [](idx)
  if @raw_array[idx].is_a?(Ampere::Model) then
    @raw_array[idx]
  else
    # This is still an ID. Find it.
    @raw_array[idx] = @model.find(@raw_array[idx])
  end
end
each() { |x| ... } click to toggle source
# File lib/ampere/collection.rb, line 19
def each
  @raw_array.each_with_index do |x, i|
    if x.is_a?(Ampere::Model) then
      yield(x)
    else
      raw_array[i] = @model.find(x)
      yield(raw_array[i])
    end
  end
end
empty?() click to toggle source

Delegates to internal array.

# File lib/ampere/collection.rb, line 41
def empty?
  @raw_array.empty?
end
last() click to toggle source

Returns the last item.

# File lib/ampere/collection.rb, line 46
def last
  self[-1]
end