Module: DataMapper::Model::Relationship

Extended by:
Chainable
Includes:
Extlib::Assertions
Defined in:
lib/dm-core/model/relationship.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chainable

chainable, extendable

Class Method Details

.extended(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.

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.



20
21
22
# File 'lib/dm-core/model/relationship.rb', line 20

def self.extended(model)
  model.instance_variable_set(:@relationships, {})
end

Instance Method Details

#belongs_to(name, *args) ⇒ Association::Relationship

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

Parameters:

  • name (Symbol)

    the name that the association will be referenced by

  • model (Model, #to_str)

    the target model of the relationship

  • opts (Hash)

    an options hash

  • :model[Model, (Hash)

    a customizable set of options

  • :repository[Symbol] (Hash)

    a customizable set of options

Returns:

  • (Association::Relationship)

    The association created should not be accessed directly



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dm-core/model/relationship.rb', line 164

def belongs_to(name, *args)
  assert_kind_of 'name', name, Symbol

  model   = extract_model(args)
  options = extract_options(args)

  if options.key?(:through)
    warn "#{self.name}#belongs_to with :through is deprecated, use 'has 1, :#{name}, #{options.inspect}' in #{self.name} instead (#{caller[0]})"
    return has(1, name, model, options)
  end

  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 source_repository_name and target_respository_name
  options[:child_repository_name]  = repository_name
  options[:parent_repository_name] = options.delete(:repository)

  relationship = relationships(repository.name)[name] = Associations::ManyToOne::Relationship.new(name, self, model, options)

  descendants.each do |descendant|
    descendant.relationships(repository.name)[name] ||= relationship.inherited_by(descendant)
  end

  relationship
end

#has(cardinality, name, *args) ⇒ Association::Relationship

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

Parameters:

  • cardinality (Integer, Range, Infinity)

    cardinality that defines the association type and constraints

  • name (Symbol)

    the name that the association will be referenced by

  • model (Model, #to_str)

    the target model of the relationship

  • opts (Hash)

    an options hash

  • :through[Symbol] (Hash)

    a customizable set of options

  • :model[Model, (Hash)

    a customizable set of options

  • :repository[Symbol] (Hash)

    a customizable set of options

Returns:

  • (Association::Relationship)

    the relationship that was created to reflect either a one-to-one, one-to-many or many-to-many relationship

Raises:

  • (ArgumentError)

    if the cardinality was not understood. Should be a Integer, Range or Infinity(n)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dm-core/model/relationship.rb', line 105

def has(cardinality, name, *args)
  assert_kind_of 'cardinality', cardinality, Integer, Range, n.class
  assert_kind_of 'name',        name,        Symbol

  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)

  # 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 options[:max] > 1
    options.key?(:through) ? Associations::ManyToMany::Relationship : Associations::OneToMany::Relationship
  else
    Associations::OneToOne::Relationship
  end

  relationship = relationships(repository.name)[name] = klass.new(name, model, self, options)

  descendants.each do |descendant|
    descendant.relationships(repository.name)[name] ||= relationship.inherited_by(descendant)
  end

  relationship
end

#nObject

Used to express unlimited cardinality of association, see has



68
69
70
# File 'lib/dm-core/model/relationship.rb', line 68

def n
  1.0/0
end

#relationships(repository_name = default_repository_name) ⇒ Mash

Returns copy of relationships set in given repository.

Parameters:

  • repository_name (Symbol) (defaults to: default_repository_name)

    Name of the repository for which relationships set is returned

Returns:

  • (Mash)

    relationships set for given repository



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dm-core/model/relationship.rb', line 52

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

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