0
@@ -4,20 +4,48 @@ module DataMapper
0
+ # @return [Repository] the repository the collection is
0
+ # loads the entries for the collection. Used by the
0
+ # adapters to load the instances of the declared
0
+ # model for this collection's query.
0
add(model.load(values, query))
0
+ # reloads the entries associated with this collection
0
+ # @param [DataMapper::Query] query (optional) additional query
0
+ # to scope by. Use this if you want to query a collections result
0
+ # @see DataMapper::Collection#all
0
@query = scoped_query(query)
0
@query.update(:fields => @query.fields | @key_properties)
0
replace(all(:reload => true))
0
+ # retrieves an entry out of the collection's entry by key
0
+ # @param [DataMapper::Types::*, ...] key keys which uniquely
0
+ # identify a resource in the collection
0
+ # @return [DataMapper::Resource, NilClass] the resource which
0
+ # has the supplied keys
0
# loop over the collection to find the matching resource
0
@@ -42,17 +70,51 @@ module DataMapper
0
+ # retrieves an entry out of the collection's entry by key,
0
+ # raising an exception if the object cannot be found
0
+ # @param [DataMapper::Types::*, ...] key keys which uniquely
0
+ # identify a resource in the collection
0
+ # @calls DataMapper::Collection#get
0
+ # @raise [ObjectNotFoundError] "Could not find #{model.name} with key #{key.inspect} in collection"
0
get(*key) || raise(ObjectNotFoundError, "Could not find #{model.name} with key #{key.inspect} in collection")
0
+ # Further refines a collection's conditions. #all provides an
0
+ # interface which simulates a database view.
0
+ # @param [Hash[Symbol, Object], DataMapper::Query] query parameters for
0
+ # an query within the results of the original query.
0
+ # @return [DataMapper::Collection] a collection whose query is the result
0
+ # TODO: this shouldn't be a kicker if scoped_query() is called
0
return self if query.kind_of?(Hash) ? query.empty? : query == self.query
0
query = scoped_query(query)
0
query.repository.read_many(query)
0
+ # Simulates Array#first by returning the first entry (when
0
+ # there are no arguments), or transforms the collection's query
0
+ # by applying :limit => n when you supply an Integer. If you
0
+ # provide a conditions hash, or a Query object, the internal
0
+ # query is scoped and a new collection is returned
0
+ # @param [Integer, Hash[Symbol, Object], Query] args
0
+ # @return [DataMapper::Resource, DataMapper::Collection] The
0
+ # first resource in the entries of this collection, or
0
+ # a new collection whose query has been merged
0
+ # TODO: this shouldn't be a kicker if scoped_query() is called
0
@@ -72,6 +134,16 @@ module DataMapper
0
+ # Simulates Array#last by returning the last entry (when
0
+ # there are no arguments), or transforming the collection's
0
+ # query by reversing the declared order, and applying
0
+ # :limit => n when you supply an Integer. If you
0
+ # supply a conditions hash, or a Query object, the
0
+ # internal query is scoped and a new collection is returned
0
+ # @calls Collection#first
0
return super if loaded? && args.empty?
0
@@ -84,11 +156,40 @@ module DataMapper
0
+ # Simulates Array#at and returns the entrie at that index.
0
+ # Also accepts negative indexes and appropriate reverses
0
+ # the order of the query
0
+ # @calls Collection#first
0
+ # @calls Collection#last
0
return super if loaded?
0
offset >= 0 ? first(:offset => offset) : last(:offset => offset.abs - 1)
0
+ # Simulates Array#slice and returns a new Collection
0
+ # whose query has a new offset or limit according to the
0
+ # If you provide a range, the min is used as the offset
0
+ # and the max minues the offset is used as the limit.
0
+ # @param [Integer, Array(Integer), Range] args the offset,
0
+ # offset and limit, or range indicating offsets and limits
0
+ # @return [DataMapper::Resource, DataMapper::Collection]
0
+ # The entry which resides at that offset and limit,
0
+ # or a new Collection object with the set limits and offset
0
+ # @raise [ArgumentError] "arguments may be 1 or 2 Integers,
0
+ # or 1 Range object, was: #{args.inspect}"
0
return at(args.first) if args.size == 1 && args.first.kind_of?(Integer)
0
@@ -108,22 +209,35 @@ module DataMapper
0
+ # @return [DataMapper::Collection] a new collection whose
0
+ # query is sorted in the reverse
0
+ # @see Array#reverse, DataMapper#all, DataMapper::Query#reverse
0
all(self.query.reverse)
0
relate_resource(resource)
0
resources.each { |resource| relate_resource(resource) }
0
def unshift(*resources)
0
resources.each { |resource| relate_resource(resource) }
0
@@ -163,6 +277,11 @@ module DataMapper
0
+ # creates a new array, saves it, and << it onto the collection
0
+ # @param Hash[Symbol => Object] attributes attributes which
0
+ # the new resource should have.
0
def create(attributes = {})
0
resource = model.create(default_attributes.merge(attributes))
0
@@ -171,8 +290,18 @@ module DataMapper
0
- # TODO: delegate to Model.update
0
+ # batch updates the entries belongs to this collection.
0
+ # @example Reached the Age of Alchohol Consumption
0
+ # Person.all(:age.gte => 21).update(:allow_beer => true)
0
+ # @return [TrueClass, FalseClass]
0
+ # TrueClass indicates that all entries were affected
0
+ # FalseClass indicates that some entries were affected
0
def update(attributes = {},preload=false)
0
+ # TODO: delegate to Model.update
0
return true if attributes.empty?
0
dirty_attributes, keys_to_reload = {}, {}
0
@@ -199,8 +328,18 @@ module DataMapper
0
return loaded? ? affected == size : affected > 0
0
- # TODO: delegate to Model.destroy
0
+ # batch destroy the entries belongs to this collection.
0
+ # @example The War On Terror (if only it were this easy)
0
+ # Person.all(:terrorist => true).destroy() #
0
+ # @return [TrueClass, FalseClass]
0
+ # TrueClass indicates that all entries were affected
0
+ # FalseClass indicates that some entries were affected