Class: RDFMapper::Scope::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/scope/collection.rb

Overview

This class contains collections of models. It is primarily used in search queries (find(:all) queries will yield instances of this class) and associations.

It implements most commonly used Array and Enumerable methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, options) ⇒ Collection

Returns a new instance of Collection.



14
15
16
17
18
# File 'lib/lib/scope/collection.rb', line 14

def initialize(cls, options)
  @loader = Loader.new(cls, options)
  @models = []
  @cls = cls
end

Instance Attribute Details

#loaderObject (readonly)

Temporary



12
13
14
# File 'lib/lib/scope/collection.rb', line 12

def loader
  @loader
end

Instance Method Details

#[](index) ⇒ Object Also known as: slice

Returns the object at ‘index`. Note that the object is not yet loaded at this point.

Parameters:

  • index (Integer)

Returns:

  • (Object)


64
65
66
# File 'lib/lib/scope/collection.rb', line 64

def [](index)
  at(index)
end

#at(index) ⇒ Object

Returns the object at ‘index`. Note that the object is not yet loaded at this point.

Parameters:

  • index (Integer)

Returns:

  • (Object)


75
76
77
# File 'lib/lib/scope/collection.rb', line 75

def at(index)
  @models[index] ||= @loader.get(index)
end

#each {|Object| ... } ⇒ self

Calls block once for each object in a collection, passing that element as a parameter.

Yields:

  • (Object)

Returns:

  • (self)


104
105
106
107
# File 'lib/lib/scope/collection.rb', line 104

def each(&block)
  items.each { |x| block.call(x) }
  self
end

#empty?Boolean

Returns true if collection has no objects.

Parameters:

  • (Boolean)

Returns:

  • (Boolean)


84
85
86
# File 'lib/lib/scope/collection.rb', line 84

def empty?
  length == 0
end

#exists?(object) ⇒ Boolean Also known as: include?

Returns true if collection contains specified object.

Parameters:

  • object (Object)

    instance of RDFMapper::Model

Returns:

  • (Boolean)


126
127
128
# File 'lib/lib/scope/collection.rb', line 126

def exists?(object)
  items.include?(object)
end

#firstObject

Returns first object of the collection. Note that the object is not yet loaded at this point.

Returns:

  • (Object)


43
44
45
# File 'lib/lib/scope/collection.rb', line 43

def first
  at(0)
end

#from(adapter, options = {}) ⇒ self

Set data adapter for the query and return self. This will override the default model adapter. It is intended to be used as a chain method:

Person.find(:all).from(:rest)          #=>  #<PersonCollection:217132856>
Person.find(:all).from(:rest).length   #=>  10

Parameters:

  • adapter (Symbol)

    (:rails, :sparql, :rest)

  • options (Hash) (defaults to: {})

    options to pass on to the adapter constructor

Returns:

  • (self)


32
33
34
35
# File 'lib/lib/scope/collection.rb', line 32

def from(adapter, options = {})
  @loader.from(adapter, options)
  self
end

#inspectString

Developer-friendly representation of the instance

Returns:

  • (String)


168
169
170
# File 'lib/lib/scope/collection.rb', line 168

def inspect #nodoc
  "#<%sCollection:%s>" % [@cls, object_id]
end

#kind_of?(cls) ⇒ Boolean

-

Returns:

  • (Boolean)


159
160
161
# File 'lib/lib/scope/collection.rb', line 159

def kind_of?(cls)
  cls == self.class || cls == Enumerable || cls == Array
end

#lastObject

Returns first object of the collection. Note that the object is not yet loaded at this point.

Returns:

  • (Object)


53
54
55
# File 'lib/lib/scope/collection.rb', line 53

def last
  at(-1)
end

#lengthInteger Also known as: size

Returns the number of objects in a collection.

Returns:

  • (Integer)


93
94
95
# File 'lib/lib/scope/collection.rb', line 93

def length
  @loader.length
end

#map {|Object| ... } ⇒ Array Also known as: collect

Invokes block once for each object in a collection. Creates a new array containing the values returned by the block

Yields:

  • (Object)

Returns:

  • (Array)


116
117
118
# File 'lib/lib/scope/collection.rb', line 116

def map(&block)
  items.map { |x| block.call(x) }
end

#select {|Object| ... } ⇒ Array

Invokes the block passing in successive elements from array, returning an array containing those elements for which the block returns a true value.

Yields:

  • (Object)

Returns:

  • (Array)


138
139
140
# File 'lib/lib/scope/collection.rb', line 138

def select(&block)
  items.select { |x| block.call(x) }
end

#to_aArray

Converts collection into Array.

Returns:

  • (Array)


147
148
149
# File 'lib/lib/scope/collection.rb', line 147

def to_a
  items
end