Class: DataMapper::Associations::ManyToOne::Relationship

### Home Classes Methods

### In Files

- lib/dm-core/associations/many_to_one.rb

### Parent

DataMapper::Associations::Relationship

### Methods

- ::new
- #child_key
- #default_for
- #finalize
- #get
- #get_collection
- #key?
- #lazy_load
- #nullable?
- #required?
- #resource_for
- #set
- #source_key
- #source_scope
- #unique?

### 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::Associations::ManyToOne::Relationship

Relationship class with implementation specific to n side of 1 to n association

### Constants

OPTIONS

### Public Class Methods

new(name, source_model, target_model, options = {}) click to toggle source

Initializes the relationship, always using max cardinality of 1.

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 202
    def initialize(name, source_model, target_model, options = {})
      if options.key?(:nullable)
        raise ":nullable is deprecated, use :required instead (#{caller[2]})"
      end

      @required      = options.fetch(:required, true)
      @key           = options.fetch(:key,      false)
      @unique        = options.fetch(:unique,   false)
      target_model ||= DataMapper::Inflector.camelize(name)
      options        = { :min => @required ? 1 : 0, :max => 1 }.update(options)
      super
    end

### Public Instance Methods

child_key() click to toggle source

Returns a set of keys that identify source model

@return [DataMapper::PropertySet] a set of properties that identify source model @api private

    # File lib/dm-core/associations/many_to_one.rb, line 48
    def child_key
      return @child_key if defined?(@child_key)

      model           = source_model
      repository_name = source_repository_name || target_repository_name
      properties      = model.properties(repository_name)

      source_key = target_key.zip(@child_properties || []).map do |target_property, property_name|
        property_name ||= "#{name}_#{target_property.name}".to_sym

        properties[property_name] || begin
          # create the property within the correct repository
          DataMapper.repository(repository_name) do
            model.property(property_name, target_property.to_child_key, source_key_options(target_property))
          end
        end
      end

      @child_key = properties.class.new(source_key).freeze
    end

Also aliased as: source_key
default_for(source) click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 165
    def default_for(source)
      typecast(super)
    end

finalize() click to toggle source

Initialize the foreign key property this "many to one" relationship uses to persist itself

@api public

    # File lib/dm-core/associations/many_to_one.rb, line 76
    def finalize
      child_key
    end

get(source, query = nil) click to toggle source

Loads and returns association target (ex.: author) for given source resource (ex.: article)

@param source [DataMapper::Resource]

    source object (ex.: instance of article)

@param other_query [DataMapper::Query]

    Query options

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 132
    def get(source, query = nil)
      lazy_load(source)

      if query
        collection = get_collection(source)
        collection.first(query) if collection
      else
        get!(source)
      end
    end

get_collection(source) click to toggle source

    # File lib/dm-core/associations/many_to_one.rb, line 143
    def get_collection(source)
      target = get!(source)
      target.collection_for_self if target
    end

key?() click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 30
    def key?
      @key
    end

lazy_load(source) click to toggle source

Loads association target and sets resulting value on given source resource

@param [Resource] source

    the source resource for the association

@return [undefined]

@api private

    # File lib/dm-core/associations/many_to_one.rb, line 178
    def lazy_load(source)
      source_key_different = source_key_different?(source)

      if (loaded?(source) && !source_key_different) || !valid_source?(source)
        return
      end

      # SEL: load all related resources in the source collection
      if source.saved? && (collection = source.collection).size > 1
        eager_load(collection)
      end

      if !loaded?(source) || (source_key_dirty?(source) && source.saved?)
        set!(source, resource_for(source))
      elsif loaded?(source) && source_key_different
        source_key.set(source, target_key.get!(get!(source)))
      end
    end

nullable?() click to toggle source

@deprecated

    # File lib/dm-core/associations/many_to_one.rb, line 40
    def nullable?
      raise "#{self.class}#nullable? is deprecated, use #{self.class}#required? instead (#{caller.first})"
    end

required?() click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 25
    def required?
      @required
    end

resource_for(source, other_query = nil) click to toggle source

Returns a Resource for this relationship with a given source

@param [Resource] source

    A Resource to scope the collection with

@param [Query] other_query (optional)

    A Query to further scope the collection with

@return [Resource]

    The resource scoped to the relationship, source and query

@api private

    # File lib/dm-core/associations/many_to_one.rb, line 106
    def resource_for(source, other_query = nil)
      query = query_for(source, other_query)

      # If the target key is equal to the model key, we can use the
      # Model#get so the IdentityMap is used
      if target_key == target_model.key
        target = target_model.get(*source_key.get!(source))
        if query.conditions.matches?(target)
          target
        else
          nil
        end
      else
        target_model.first(query)
      end
    end

set(source, target) click to toggle source

Sets value of association target (ex.: author) for given source resource (ex.: article)

@param source [DataMapper::Resource]

    source object (ex.: instance of article)

@param target [DataMapper::Resource]

    target object (ex.: instance of author)

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 158
    def set(source, target)
      target = typecast(target)
      source_key.set(source, target_key.get(target))
      set!(source, target)
    end

source_key() click to toggle source

@api semipublic

Alias for: child_key
source_scope(source) click to toggle source

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

@return [Hash]

    Hash of conditions that scopes query

@api private

    # File lib/dm-core/associations/many_to_one.rb, line 87
    def source_scope(source)
      if source.kind_of?(Resource)
        Query.target_conditions(source, source_key, target_key)
      else
        super
      end
    end

unique?() click to toggle source

@api semipublic

    # File lib/dm-core/associations/many_to_one.rb, line 35
    def unique?
      !!@unique
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.