Class/Module Index [+]

Quicksearch

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.