class DataMapper::Associations::OneToMany::Collection

Attributes

relationship[RW]

@api private

source[RW]

@api private

Public Instance Methods

clear() click to toggle source

Removes all Resources from the 1:m Collection

This should remove and orphan each Resource from the 1:m Collection.

@return [Collection]

self

@api public

Calls superclass method DataMapper::Collection#clear
# File lib/dm-core/associations/one_to_many.rb, line 214
def clear
  lazy_load  # lazy load so that targets are always orphaned
  super
end
destroy() click to toggle source

Remove every Resource in the 1:m Collection from the repository

This performs a deletion of each Resource in the Collection from the repository and clears the Collection.

@return [Boolean]

true if the resources were successfully destroyed

@api public

Calls superclass method DataMapper::Collection#destroy
# File lib/dm-core/associations/one_to_many.rb, line 256
def destroy
  assert_source_saved 'The source must be saved before mass-deleting the collection'
  super
end
destroy!() click to toggle source

Remove every Resource in the 1:m Collection from the repository, bypassing validation

This performs a deletion of each Resource in the Collection from the repository and clears the Collection while skipping validation.

@return [Boolean]

true if the resources were successfully destroyed

@api public

Calls superclass method DataMapper::Collection#destroy!
# File lib/dm-core/associations/one_to_many.rb, line 271
def destroy!
  assert_source_saved 'The source must be saved before mass-deleting the collection'
  super
end
reload(*) click to toggle source

@api public

Calls superclass method DataMapper::Collection#reload
# File lib/dm-core/associations/one_to_many.rb, line 187
def reload(*)
  assert_source_saved 'The source must be saved before reloading the collection'
  super
end
replace(*) click to toggle source

Replace the Resources within the 1:m Collection

@param [Enumerable] other

List of other Resources to replace with

@return [Collection]

self

@api public

Calls superclass method DataMapper::Collection#replace
# File lib/dm-core/associations/one_to_many.rb, line 201
def replace(*)
  lazy_load  # lazy load so that targets are always orphaned
  super
end
update(*) click to toggle source

Update every Resource in the 1:m Collection

@param [Hash] attributes

attributes to update with

@return [Boolean]

true if the resources were successfully updated

@api public

Calls superclass method DataMapper::Collection#update
# File lib/dm-core/associations/one_to_many.rb, line 228
def update(*)
  assert_source_saved 'The source must be saved before mass-updating the collection'
  super
end
update!(*) click to toggle source

Update every Resource in the 1:m Collection, bypassing validation

@param [Hash] attributes

attributes to update

@return [Boolean]

true if the resources were successfully updated

@api public

Calls superclass method DataMapper::Collection#update!
# File lib/dm-core/associations/one_to_many.rb, line 242
def update!(*)
  assert_source_saved 'The source must be saved before mass-updating the collection'
  super
end

Private Instance Methods

_create(*) click to toggle source

@api private

Calls superclass method DataMapper::Collection#_create
# File lib/dm-core/associations/one_to_many.rb, line 279
def _create(*)
  assert_source_saved 'The source must be saved before creating a resource'
  super
end
_save(execute_hooks = true) click to toggle source

@api private

Calls superclass method DataMapper::Collection#_save
# File lib/dm-core/associations/one_to_many.rb, line 285
def _save(execute_hooks = true)
  assert_source_saved 'The source must be saved before saving the collection'

  # update removed resources to not reference the source
  @removed.all? { |resource| resource.destroyed? || resource.__send__(execute_hooks ? :save : :save!) } && super
end
assert_source_saved(message) click to toggle source

@api private

# File lib/dm-core/associations/one_to_many.rb, line 337
def assert_source_saved(message)
  unless source.saved?
    raise UnsavedParentError, message
  end
end
inverse_set(source, target) click to toggle source

@api private

# File lib/dm-core/associations/one_to_many.rb, line 330
def inverse_set(source, target)
  unless source.readonly?
    relationship.inverse.set(source, target)
  end
end
lazy_load() click to toggle source

@api private

Calls superclass method DataMapper::Collection#lazy_load
# File lib/dm-core/associations/one_to_many.rb, line 293
def lazy_load
  if source.saved?
    super
  end
end
new_collection(query, resources = nil, &block) click to toggle source

@api private

# File lib/dm-core/associations/one_to_many.rb, line 300
def new_collection(query, resources = nil, &block)
  collection = self.class.new(query, &block)

  collection.relationship = relationship
  collection.source       = source

  resources ||= filter(query) if loaded?

  # set the resources after the relationship and source are set
  if resources
    collection.set(resources)
  end

  collection
end
resource_added(resource) click to toggle source

@api private

Calls superclass method DataMapper::Collection#resource_added
# File lib/dm-core/associations/one_to_many.rb, line 317
def resource_added(resource)
  resource = initialize_resource(resource)
  inverse_set(resource, source)
  super
end
resource_removed(resource) click to toggle source

@api private

Calls superclass method DataMapper::Collection#resource_removed
# File lib/dm-core/associations/one_to_many.rb, line 324
def resource_removed(resource)
  inverse_set(resource, nil)
  super
end