Parent

Class Index [+]

Quicksearch

DataMapper::Associations::ManyToMany::Collection

Public Instance Methods

destroy() click to toggle source

Remove every Resource in the m: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

     # File lib/dm-core/associations/many_to_many.rb, line 328
328:         def destroy
329:           assert_source_saved 'The source must be saved before mass-deleting the collection'
330: 
331:           # make sure the records are loaded so they can be found when
332:           # the intermediaries are removed
333:           lazy_load
334: 
335:           unless intermediaries.all(via => self).destroy
336:             return false
337:           end
338: 
339:           super
340:         end
destroy!() click to toggle source

Remove every Resource in the m: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

     # File lib/dm-core/associations/many_to_many.rb, line 352
352:         def destroy!
353:           assert_source_saved 'The source must be saved before mass-deleting the collection'
354: 
355:           model      = self.model
356:           key        = model.key(repository_name)
357:           conditions = Query.target_conditions(self, key, key)
358: 
359:           unless intermediaries.all(via => self).destroy!
360:             return false
361:           end
362: 
363:           unless model.all(:repository => repository, :conditions => conditions).destroy!
364:             return false
365:           end
366: 
367:           each do |resource|
368:             resource.persistence_state = Resource::PersistenceState::Immutable.new(resource)
369:           end
370: 
371:           clear
372: 
373:           true
374:         end
intermediaries() click to toggle source

Return the intermediaries linking the source to the targets

@return [Collection]

  the intermediary collection

@api public

     # File lib/dm-core/associations/many_to_many.rb, line 382
382:         def intermediaries
383:           through = self.through
384:           source  = self.source
385: 
386:           @intermediaries ||= if through.loaded?(source)
387:             through.get_collection(source)
388:           else
389:             reset_intermediaries
390:           end
391:         end

Protected Instance Methods

intermediary_for() click to toggle source

Map the resources in the collection to the intermediaries

@return [Hash]

  the map of resources to their intermediaries

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 401
401:         def intermediary_for
402:           @intermediary_for ||= {}
403:         end
through() click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 406
406:         def through
407:           relationship.through
408:         end
via() click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 411
411:         def via
412:           relationship.via
413:         end

Private Instance Methods

_create(attributes, execute_hooks = true) click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 418
418:         def _create(attributes, execute_hooks = true)
419:           via = self.via
420:           if via.respond_to?(:resource_for)
421:             resource = super
422:             if create_intermediary(execute_hooks, resource)
423:               resource
424:             end
425:           else
426:             if intermediary = create_intermediary(execute_hooks)
427:               super(attributes.merge(via.inverse => intermediary), execute_hooks)
428:             end
429:           end
430:         end
_save(execute_hooks = true) click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 433
433:         def _save(execute_hooks = true)
434:           via = self.via
435: 
436:           if @removed.any?
437:             # delete only intermediaries linked to the removed targets
438:             return false unless intermediaries.all(via => @removed).send(execute_hooks ? :destroy : :destroy!)
439: 
440:             # reset the intermediaries so that it reflects the current state of the datastore
441:             reset_intermediaries
442:           end
443: 
444:           loaded_entries = self.loaded_entries
445: 
446:           if via.respond_to?(:resource_for)
447:             super
448:             loaded_entries.all? { |resource| create_intermediary(execute_hooks, resource) }
449:           else
450:             if loaded_entries.any? && (intermediary = create_intermediary(execute_hooks))
451:               inverse = via.inverse
452:               loaded_entries.each { |resource| inverse.set(resource, intermediary) }
453:             end
454: 
455:             super
456:           end
457:         end
create_intermediary(execute_hooks, resource = nil) click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 460
460:         def create_intermediary(execute_hooks, resource = nil)
461:           intermediary_for = self.intermediary_for
462: 
463:           intermediary_resource = intermediary_for[resource]
464:           return intermediary_resource if intermediary_resource
465: 
466:           intermediaries = self.intermediaries
467:           method         = execute_hooks ? :save : :save!
468: 
469:           return unless intermediaries.send(method)
470: 
471:           attributes = {}
472:           attributes[via] = resource if resource
473: 
474:           intermediary = intermediaries.first_or_new(attributes)
475:           return unless intermediary.__send__(method)
476: 
477:           # map the resource, even if it is nil, to the intermediary
478:           intermediary_for[resource] = intermediary
479:         end
inverse_set(*) click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 490
490:         def inverse_set(*)
491:           # do nothing
492:         end
reset_intermediaries() click to toggle source

@api private

     # File lib/dm-core/associations/many_to_many.rb, line 482
482:         def reset_intermediaries
483:           through = self.through
484:           source  = self.source
485: 
486:           through.set_collection(source, through.collection_for(source))
487:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.