Class: RDFMapper::Scope::Collection
- Inherits:
-
Object
- Object
- RDFMapper::Scope::Collection
- 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
-
#loader ⇒ Object
readonly
Temporary.
Instance Method Summary collapse
-
#[](index) ⇒ Object
(also: #slice)
Returns the object at ‘index`.
-
#at(index) ⇒ Object
Returns the object at ‘index`.
-
#each {|Object| ... } ⇒ self
Calls block once for each object in a collection, passing that element as a parameter.
-
#empty? ⇒ Boolean
Returns true if collection has no objects.
-
#exists?(object) ⇒ Boolean
(also: #include?)
Returns true if collection contains specified object.
-
#first ⇒ Object
Returns first object of the collection.
-
#from(adapter, options = {}) ⇒ self
Set data adapter for the query and return self.
-
#initialize(cls, options) ⇒ Collection
constructor
A new instance of Collection.
-
#inspect ⇒ String
Developer-friendly representation of the instance.
-
#kind_of?(cls) ⇒ Boolean
[-].
-
#last ⇒ Object
Returns first object of the collection.
-
#length ⇒ Integer
(also: #size)
Returns the number of objects in a collection.
-
#map {|Object| ... } ⇒ Array
(also: #collect)
Invokes block once for each object in a collection.
-
#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.
-
#to_a ⇒ Array
Converts collection into Array.
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, ) @loader = Loader.new(cls, ) @models = [] @cls = cls end |
Instance Attribute Details
#loader ⇒ Object (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.
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.
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.
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.
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.
126 127 128 |
# File 'lib/lib/scope/collection.rb', line 126 def exists?(object) items.include?(object) end |
#first ⇒ Object
Returns first object of the collection. Note that the object is not yet loaded at this point.
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
32 33 34 35 |
# File 'lib/lib/scope/collection.rb', line 32 def from(adapter, = {}) @loader.from(adapter, ) self end |
#inspect ⇒ String
Developer-friendly representation of the instance
168 169 170 |
# File 'lib/lib/scope/collection.rb', line 168 def inspect #nodoc "#<%sCollection:%s>" % [@cls, object_id] end |
#kind_of?(cls) ⇒ Boolean
- -
159 160 161 |
# File 'lib/lib/scope/collection.rb', line 159 def kind_of?(cls) cls == self.class || cls == Enumerable || cls == Array end |
#last ⇒ Object
Returns first object of the collection. Note that the object is not yet loaded at this point.
53 54 55 |
# File 'lib/lib/scope/collection.rb', line 53 def last at(-1) end |
#length ⇒ Integer Also known as: size
Returns the number of objects in a collection.
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
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.
138 139 140 |
# File 'lib/lib/scope/collection.rb', line 138 def select(&block) items.select { |x| block.call(x) } end |
#to_a ⇒ Array
Converts collection into Array.
147 148 149 |
# File 'lib/lib/scope/collection.rb', line 147 def to_a items end |