Module: DataMapper::Model::Relationship

### Home Classes Methods

### In Files

- lib/dm-core/model/relationship.rb

### Methods

- ::extended
- #belongs_to
- #has
- #inherited
- #n
- #relationships

### Included Modules

- DataMapper::Assertions

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Adapters::AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- DataMapper::Assertions
- DataMapper::Associations
- DataMapper::Associations::ManyToMany
- DataMapper::Associations::ManyToMany::Collection
- DataMapper::Associations::ManyToMany::Relationship
- DataMapper::Associations::ManyToOne
- DataMapper::Associations::ManyToOne::Relationship
- DataMapper::Associations::OneToMany
- DataMapper::Associations::OneToMany::Collection
- DataMapper::Associations::OneToMany::Relationship
- DataMapper::Associations::OneToOne
- DataMapper::Associations::OneToOne::Relationship
- DataMapper::Associations::Relationship
- DataMapper::Chainable
- DataMapper::Collection
- DataMapper::Deprecate
- DataMapper::DescendantSet
- DataMapper::Equalizer
- DataMapper::Ext
- DataMapper::Ext::Array
- DataMapper::Ext::Hash
- DataMapper::Ext::Module
- DataMapper::Ext::Object
- DataMapper::Ext::String
- DataMapper::Hook
- DataMapper::Hook::ClassMethods
- DataMapper::IdentityMap
- DataMapper::ImmutableDeletedError
- DataMapper::ImmutableError
- DataMapper::IncompleteModelError
- DataMapper::Inflector
- DataMapper::Inflector::Inflections
- DataMapper::LocalObjectSpace
- DataMapper::Logger
- DataMapper::Mash
- DataMapper::Model
- DataMapper::Model::Hook
- DataMapper::Model::Hook::MethodCommand
- DataMapper::Model::Hook::Methods
- DataMapper::Model::Hook::ProcCommand
- DataMapper::Model::Is
- DataMapper::Model::Property
- DataMapper::Model::Relationship
- DataMapper::Model::Scope
- DataMapper::NamingConventions
- DataMapper::NamingConventions::Field
- DataMapper::NamingConventions::Field::Underscored
- DataMapper::NamingConventions::Field::UnderscoredAndPluralized
- DataMapper::NamingConventions::Field::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Field::Yaml
- DataMapper::NamingConventions::Resource
- DataMapper::NamingConventions::Resource::Underscored
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralized
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutLeadingModule
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Resource::Yaml
- DataMapper::ObjectNotFoundError
- DataMapper::OrderedSet
- DataMapper::OrderedSet::Cache
- DataMapper::OrderedSet::Cache::API
- DataMapper::PersistenceError
- DataMapper::PluginNotFoundError
- DataMapper::Property
- DataMapper::Property::Binary
- DataMapper::Property::Boolean
- DataMapper::Property::Class
- DataMapper::Property::Date
- DataMapper::Property::DateTime
- DataMapper::Property::Decimal
- DataMapper::Property::Discriminator
- DataMapper::Property::Discriminator::Model
- DataMapper::Property::Float
- DataMapper::Property::Integer
- DataMapper::Property::Lookup
- DataMapper::Property::Numeric
- DataMapper::Property::Object
- DataMapper::Property::PassThroughLoadDump
- DataMapper::Property::Serial
- DataMapper::Property::String
- DataMapper::Property::Text
- DataMapper::Property::Time
- DataMapper::Property::Typecast
- DataMapper::Property::Typecast::Numeric
- DataMapper::Property::Typecast::Time
- DataMapper::PropertySet
- DataMapper::Query
- DataMapper::Query::Conditions
- DataMapper::Query::Conditions::AbstractComparison
- DataMapper::Query::Conditions::AbstractOperation
- DataMapper::Query::Conditions::AndOperation
- DataMapper::Query::Conditions::Comparison
- DataMapper::Query::Conditions::EqualToComparison
- DataMapper::Query::Conditions::FlattenOperation
- DataMapper::Query::Conditions::GreaterThanComparison
- DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
- DataMapper::Query::Conditions::InclusionComparison
- DataMapper::Query::Conditions::LessThanComparison
- DataMapper::Query::Conditions::LessThanOrEqualToComparison
- DataMapper::Query::Conditions::LikeComparison
- DataMapper::Query::Conditions::NotOperation
- DataMapper::Query::Conditions::NullOperation
- DataMapper::Query::Conditions::Operation
- DataMapper::Query::Conditions::OrOperation
- DataMapper::Query::Conditions::RegexpComparison
- DataMapper::Query::Conditions::RelationshipHandler
- DataMapper::Query::Direction
- DataMapper::Query::Operator
- DataMapper::Query::Path
- DataMapper::Query::Sort
- DataMapper::RelationshipSet
- DataMapper::Repository
- DataMapper::RepositoryNotSetupError
- DataMapper::Resource
- DataMapper::Resource::PersistenceState
- DataMapper::Resource::PersistenceState::Clean
- DataMapper::Resource::PersistenceState::Deleted
- DataMapper::Resource::PersistenceState::Dirty
- DataMapper::Resource::PersistenceState::Immutable
- DataMapper::Resource::PersistenceState::Persisted
- DataMapper::Resource::PersistenceState::Transient
- DataMapper::SaveFailureError
- DataMapper::Spec
- DataMapper::Spec::Adapters
- DataMapper::Spec::Adapters::Adapter
- DataMapper::Spec::Adapters::Helpers
- DataMapper::Spec::Adapters::InMemoryAdapter
- DataMapper::Spec::CollectionHelpers
- DataMapper::Spec::CollectionHelpers::GroupMethods
- DataMapper::Spec::Helpers
- DataMapper::Spec::PendingHelpers
- DataMapper::Subject
- DataMapper::SubjectSet
- DataMapper::SubjectSet::NameCache
- DataMapper::Undefined
- DataMapper::UnknownRelationshipError
- DataMapper::UnsavedParentError
- DataMapper::UpdateConflictError
- Blog
- Blog::Article
- ChildSubType
- CounterAdapter
- Heffalump
- Kernel
- LazyArray
- Object
- OtherComparison
- Pathname
- SubType
- Symbol

No matching classes.

# DataMapper::Model::Relationship

### Public Class Methods

extended(model) click to toggle source

Initializes relationships hash for extended model class.

When model calls has n, has 1 or belongs_to, relationships are stored in that hash: keys are repository names and values are relationship sets.

@api private

    # File lib/dm-core/model/relationship.rb, line 19
    def self.extended(model)
      model.instance_variable_set(:@relationships, {})
    end

### Public Instance Methods

belongs_to(name, *args) click to toggle source

A shorthand, clear syntax for defining many-to-one resource relationships.

    * belongs_to :user                              # many to one user
    * belongs_to :friend, :model => 'User'          # many to one friend
    * belongs_to :reference, :repository => :pubmed # association for repository other than default

@param name [Symbol]

    the name that the association will be referenced by

@param *args [Model, Hash] model and/or options hash

@option *args :model[Model, String] The name of the class to associate with, if omitted

    then the association name is assumed to match the class name

@option *args :repository name of child model repository

@return [Association::Relationship] The association created

    should not be accessed directly

@api public

    # File lib/dm-core/model/relationship.rb, line 156
    def belongs_to(name, *args)
      name       = name.to_sym
      model_name = self.name
      model      = extract_model(args)
      options    = extract_options(args)

      if options.key?(:through)
        raise "#{model_name}#belongs_to with :through is deprecated, use 'has 1, :#{name}, #{options.inspect}' in #{model_name} instead (#{caller.first})"
      elsif options.key?(:model) && model
        raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument'
      end

      assert_valid_options(options)

      model ||= options.delete(:model)

      repository_name = repository.name

      # TODO: change to source_repository_name and target_respository_name
      options[:child_repository_name]  = repository_name
      options[:parent_repository_name] = options.delete(:repository)

      relationship = Associations::ManyToOne::Relationship.new(name, self, model, options)

      relationships(repository_name) << relationship

      descendants.each do |descendant|
        descendant.relationships(repository_name) << relationship
      end

      create_relationship_reader(relationship)
      create_relationship_writer(relationship)

      relationship
    end

has(cardinality, name, *args) click to toggle source

A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.

    * has 1,    :friend                             # one friend
    * has n,    :friends                            # many friends
    * has 1..3, :friends                            # many friends (at least 1, at most 3)
    * has 3,    :friends                            # many friends (exactly 3)
    * has 1,    :friend,  'User'                    # one friend with the class User
    * has 3,    :friends, :through => :friendships  # many friends through the friendships relationship

@param cardinality [Integer, Range, Infinity]

    cardinality that defines the association type and constraints

@param name [Symbol]

    the name that the association will be referenced by

@param *args [Model, Hash] model and/or options hash

@option *args :through A association that this join should go through to form

    a many-to-many association

@option *args :model[Model, String] The name of the class to associate with, if omitted

    then the association name is assumed to match the class name

@option *args :repository name of child model repository

@return [Association::Relationship] the relationship that was

    created to reflect either a one-to-one, one-to-many or many-to-many
    relationship

@raise [ArgumentError] if the cardinality was not understood. Should be a

    Integer, Range or Infinity(n)

@api public

    # File lib/dm-core/model/relationship.rb, line 96
    def has(cardinality, name, *args)
      name    = name.to_sym
      model   = extract_model(args)
      options = extract_options(args)

      min, max = extract_min_max(cardinality)
      options.update(:min => min, :max => max)

      assert_valid_options(options)

      if options.key?(:model) && model
        raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument'
      end

      model ||= options.delete(:model)

      repository_name = repository.name

      # TODO: change to :target_respository_name and :source_repository_name
      options[:child_repository_name]  = options.delete(:repository)
      options[:parent_repository_name] = repository_name

      klass = if max > 1
        options.key?(:through) ? Associations::ManyToMany::Relationship : Associations::OneToMany::Relationship
      else
        Associations::OneToOne::Relationship
      end

      relationship = klass.new(name, model, self, options)

      relationships(repository_name) << relationship

      descendants.each do |descendant|
        descendant.relationships(repository_name) << relationship
      end

      create_relationship_reader(relationship)
      create_relationship_writer(relationship)

      relationship
    end

inherited(model) click to toggle source

When DataMapper model is inherited, relationships of parent are duplicated and copied to subclass model

@api private

    # File lib/dm-core/model/relationship.rb, line 27
    def inherited(model)
      model.instance_variable_set(:@relationships, {})

      @relationships.each do |repository_name, relationships|
        model_relationships = model.relationships(repository_name)
        relationships.each { |relationship| model_relationships << relationship }
      end

      super
    end

n() click to toggle source

Used to express unlimited cardinality of association, see `has`

@api public

    # File lib/dm-core/model/relationship.rb, line 63
    def n
      Infinity
    end

relationships(repository_name = default_repository_name) click to toggle source

Returns copy of relationships set in given repository.

@param [Symbol] repository_name

    Name of the repository for which relationships set is returned

@return [RelationshipSet] relationships set for given repository

@api semipublic

    # File lib/dm-core/model/relationship.rb, line 45
    def relationships(repository_name = default_repository_name)
      # TODO: create RelationshipSet#copy that will copy the relationships, but assign the
      # new Relationship objects to a supplied repository and model.  dup does not really
      # do what is needed

      default_repository_name = self.default_repository_name

      @relationships[repository_name] ||= if repository_name == default_repository_name
        RelationshipSet.new
      else
        relationships(default_repository_name).dup
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.