class ALD::API::Collection

Internal: Base class for collections of entries returned by a request to the ALD API.

Child classes inheriting from this class must support:

@data                  - Array of Hashes necessary to create a new
                         entry of this collection, initially nil
#entry_filter          - From the given argument array, make a filter
                         Hash that identifies the entry indicated by
                         the arguments.
#entry(hash, complete) - create a new entry from the given Hash
#request               - load the @data Array
#request_entry(filter) - From the given filter Hash, load all
                         information regarding the entry identified by
                         it.

Public Class Methods

new(api, conditions = {}, data = nil) click to toggle source

Internal: Create a new Collection

api - the ALD::API instance this collection belongs to conditions - a Hash of conditions entries in this collection must meet data - an Array of Hashes for @data. May be nil.

# File lib/ALD/collection.rb, line 28
def initialize(api, conditions = {}, data = nil)
  @api, @conditions, @data = api, conditions, data
end

Public Instance Methods

[](*args) click to toggle source

Internal: Access an entry in the collection.

Actual arguments and behaviour depends on child classes.

Returns an entry of the collection, or nil if none is found.

Raises ArgumentError if the given arguments are invalid.

# File lib/ALD/collection.rb, line 49
def [](*args)
  if args.length == 1 && args.first.is_a?(Integer)
    request unless initialized?
    entry(@data[args.first])
  else
    filter = entry_filter(args)

    if initialized?
      entry = @data.find { |hash| filter.keys.all? { |k| hash[k.to_s] == filter[k] } }
      full_entry = false
    else
      entry = request_entry(filter)
      full_entry = true
    end

    entry.nil? ? nil : entry(entry, full_entry)
  end
end
each() { |entry(hash)| ... } click to toggle source

Public: Iterate over the entries in this collection

Yields an entry, as returned by entry

# File lib/ALD/collection.rb, line 35
def each
  request unless initialized?
  @data.each do |hash|
    yield entry(hash)
  end
end
initialized?() click to toggle source

Public: Indicate if all data in this collection is present. If false, accessing an entry or iterating over entries in this collection may trigger a HTTP request.

Returns a Boolean; true if all data is present, false otherwise

# File lib/ALD/collection.rb, line 73
def initialized?
  !@data.nil?
end

Private Instance Methods

entry_filter(args) click to toggle source

Internal: Get filter conditions for an entry. Used by [] to get an entry based on the given arguments.

This method is a mere placeholder. Child classes must override it to implement their access semantics for entries.

args - an Array of arguments to convert into conditions

Returns the Hash of conditions, where each key represents a property of the entry to be found that must equal the corresponding value.

Raises ArgumentError if the arguments cannot be converted.

# File lib/ALD/collection.rb, line 91
def entry_filter(args)
  {}
end