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 << :required << :key

Instance Attribute Summary

Attributes inherited from Relationship

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

Instance Method Summary collapse

Methods inherited from Relationship

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

Methods included from Subject

#default?

Methods included from DataMapper::Assertions

#assert_kind_of

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 source model

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dm-core/associations/many_to_one.rb', line 43

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

#default_for(source) ⇒ Object



152
153
154
# File 'lib/dm-core/associations/many_to_one.rb', line 152

def default_for(source)
  typecast(super)
end

#get(source, query = nil) ⇒ Object

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

Parameters:



119
120
121
122
123
124
125
126
127
128
# File 'lib/dm-core/associations/many_to_one.rb', line 119

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) ⇒ Object



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

def get_collection(source)
  target = get!(source)
  target.collection_for_self if target
end

#key?Boolean

Returns:

  • (Boolean)


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

def key?
  @key
end

#lazy_load(source) ⇒ undefined

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.

Loads association target and sets resulting value on given source resource

Parameters:

  • source (Resource)

    the source resource for the association

Returns:

  • (undefined)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dm-core/associations/many_to_one.rb', line 165

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?Boolean

Deprecated.

Returns:

  • (Boolean)


35
36
37
# File 'lib/dm-core/associations/many_to_one.rb', line 35

def nullable?
  raise "#{self.class}#nullable? is deprecated, use #{self.class}#required? instead (#{caller.first})"
end

#required?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dm-core/associations/many_to_one.rb', line 25

def required?
  @required
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 Resource 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



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

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) ⇒ Object

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

Parameters:



145
146
147
148
149
# File 'lib/dm-core/associations/many_to_one.rb', line 145

def set(source, target)
  target = typecast(target)
  source_key.set(source, target_key.get(target))
  set!(source, target)
end

#source_scope(source) ⇒ Hash

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 hash of conditions that scopes query that fetches target object

Returns:

  • (Hash)

    Hash of conditions that scopes query



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

def source_scope(source)
  if source.kind_of?(Resource)
    Query.target_conditions(source, source_key, target_key)
  else
    super
  end
end