Class: DataMapper::Associations::Relationship

Class | DataMapper::Associations::Relationship
In: | lib/dm-core/associations/relationship.rb
Parent: | Object

Base class for relationships. Each type of relationship (1 to 1, 1 to n, n to m) implements a subclass of this class with methods like get and set overridden.

### Methods

== child_key child_model child_model? child_model_name eager_load eql? get get! inverse loaded? new parent_key parent_model parent_model? parent_model_name query_for relative_target_repository_name relative_target_repository_name_for set set! source_scope valid?

### Included Modules

Extlib::Assertions

### Constants

OPTIONS | = | [ :child_repository_name, :parent_repository_name, :child_key, :parent_key, :min, :max, :inverse, :reader_visibility, :writer_visibility ].to_set

### Attributes

child_properties | [R] | TODO: document @api private
child_repository_name | [R] | Repository from where child objects are loaded @api semipublic
instance_variable_name | [R] | ivar used to store collection of child options in source @example for :commits association in
      class VersionControl::Branch
        # ...

        has n, :commits
      end
instance variable name for source will be @commits @api semipublic
max | [R] | Maximum number of child objects for relationship @example for :fouls association in
      class Basketball::Player
        # ...

        has 0..5, :fouls
      end
maximum is 5 @api semipublic
min | [R] | Minimum number of child objects for relationship @example for :cores association in
      class CPU::Multicore
        # ...

        has 2..n, :cores
      end
minimum is 2 @api semipublic
name | [R] | Relationship name @example for :parent association in
      class VersionControl::Commit
        # ...

        belongs_to :parent
      end
name is :parent @api semipublic
options | [R] | Options used to set up association of this relationship @example for :author association in
      class VersionControl::Commit
        # ...

        belongs_to :author, :model => 'Person'
      end
options is a hash with a single key, :model @api semipublic
parent_properties | [R] | TODO: document @api private
parent_repository_name | [R] | Repository from where parent objects are loaded @api semipublic
query | [R] | Returns query options for relationship. For this base class, always returns query options has been initialized with. Overriden in subclasses. @api private
reader_visibility | [R] | Returns the visibility for the source accessor @return [Symbol]
      the visibility for the accessor added to the source
@api semipublic
writer_visibility | [R] | Returns the visibility for the source mutator @return [Symbol]
      the visibility for the mutator added to the source
@api semipublic

### Public Class methods

new(name, child_model, parent_model, options = {})

Initializes new Relationship: sets attributes of relationship from options as well as conventions: for instance, @ivar name for association is constructed by prefixing @ to association name.

Once attributes are set, reader and writer are created for the resource association belongs to

@api semipublic

### Public Instance methods

==(other)

Compares another Relationship for equivalency

@param [Relationship] other

      the other Relationship to compare with

@return [Boolean]

      true if they are equal, false if not

@api public

child_key()

Returns a set of keys that identify the target model

@return [PropertySet]

      a set of properties that identify the target model

@api semipublic

child_model()

Returns model class used by child side of the relationship

@return [Resource]

      Model for association child

@api private

child_model?()

TODO: document @api private

child_model_name()

TODO: document @api private

eager_load(source, query = nil)

Eager load the collection using the source as a base

@param [Resource, Collection] source

      the source to query with

@param [Query, Hash] query

      optional query to restrict the collection

@return [Collection]

      the loaded collection for the source

@api private

eql?(other)

Compares another Relationship for equality

@param [Relationship] other

      the other Relationship to compare with

@return [Boolean]

      true if they are equal, false if not

@api public

get(resource, other_query = nil)

Loads and returns "other end" of the association. Must be implemented in subclasses.

@api semipublic

get!(resource)

Gets "other end" of the association directly as @ivar on given resource. Subclasses usually use implementation of this class.

@api semipublic

inverse()

Get the inverse relationship from the target model

@api semipublic

loaded?(resource)

Checks if "other end" of association is loaded on given resource.

@api semipublic

parent_key()

Returns a set of keys that identify parent model

@return [PropertySet]

      a set of properties that identify parent model

@api private

parent_model()

Returns model class used by parent side of the relationship

@return [Resource]

      Class of association parent

@api private

parent_model?()

TODO: document @api private

parent_model_name()

TODO: document @api private

query_for(source, other_query = nil)

Creates and returns Query instance that fetches target resource(s) (ex.: articles) for given target resource (ex.: author)

@api semipublic

relative_target_repository_name()

TODO: document @api private

relative_target_repository_name_for(source)

TODO: document @api private

set(resource, association)

Sets value of the "other end" of association on given resource. Must be implemented in subclasses.

@api semipublic

set!(resource, association)

Sets "other end" of the association directly as @ivar on given resource. Subclasses usually use implementation of this class.

@api semipublic

source_scope(source)

Returns a hash of conditions that scopes query that fetches target object

@return [Hash]

      Hash of conditions that scopes query

@api private

valid?(source)

Test the source to see if it is a valid target

@param [Object] source

      the resource or collection to be tested

@return [Boolean]

      true if the resource is valid

@api semipulic

[Validate]