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

Inherits:
Relationship
  • Object
show all
Defined in:
lib/dm-core/associations/many_to_one.rb

Overview

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

Constant Summary collapse

OPTIONS =
superclass::OPTIONS.dup << :nullable

Instance Attribute Summary

Attributes inherited from Relationship

#child_repository_name, #instance_variable_name, #max, #min, #name, #options, #parent_repository_name, #query

Instance Method Summary collapse

Methods inherited from Relationship

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

Instance Method Details

#child_keyDataMapper::PropertySet Also known as: source_key

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a set of keys that identify child model

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dm-core/associations/many_to_one.rb', line 39

def child_key
  return @child_key if defined?(@child_key)

  repository_name = child_repository_name || parent_repository_name
  properties      = child_model.properties(repository_name)

  child_key = parent_key.zip(@child_properties || []).map do |parent_property, property_name|
    property_name ||= "#{name}_#{parent_property.name}".to_sym

    properties[property_name] || begin
      # create the property within the correct repository
      DataMapper.repository(repository_name) do
        type = parent_property.send(parent_property.type == DataMapper::Types::Boolean ? :type : :primitive)
        child_model.property(property_name, type, child_key_options(parent_property))
      end
    end
  end

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

#get(source, other_query = nil) ⇒ Object

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

Parameters:



93
94
95
96
97
98
99
100
101
102
# File 'lib/dm-core/associations/many_to_one.rb', line 93

def get(source, other_query = nil)
  assert_kind_of 'source', source, source_model

  lazy_load(source) unless loaded?(source)

  resource = get!(source)
  if other_query.nil? || query_for(source, other_query).conditions.matches?(resource)
    resource
  end
end

#inherited_by(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



128
129
130
131
# File 'lib/dm-core/associations/many_to_one.rb', line 128

def inherited_by(model)
  model.relationships(source_repository_name)[name] ||
    self.class.new(name, model, parent_model_name, options_with_inverse)
end

#nullable?Boolean

TODO: document

Returns:

  • (Boolean)


31
32
33
# File 'lib/dm-core/associations/many_to_one.rb', line 31

def nullable?
  @nullable
end

#resource_for(source, other_query = nil) ⇒ Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a Resoruce for this relationship with a given source

Parameters:

  • source (Resource)

    A Resource to scope the collection with

  • other_query (Query) (defaults to: nil)

    (optional) A Query to further scope the collection with

Returns:

  • (Resource)

    The resource scoped to the relationship, source and query



75
76
77
78
79
80
81
82
# File 'lib/dm-core/associations/many_to_one.rb', line 75

def resource_for(source, other_query = nil)
  query = query_for(source, other_query)

  # TODO: lookup the resource in the Identity Map, and make sure
  # it matches the query criteria, otherwise perform the query

  target_model.first(query)
end

#set(source, target) ⇒ Object

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

Parameters:



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dm-core/associations/many_to_one.rb', line 114

def set(source, target)
  assert_kind_of 'source', source, source_model
  assert_kind_of 'target', target, target_model, Hash, NilClass

  if target.kind_of?(Hash)
    target = target_model.new(target)
  end

  source_key.set(source, target.nil? ? [] : target_key.get(target))
  set!(source, target)
end