Module: DataMapper::Associations

Public Visibility

Public Instance Method Summary

#belongs_to(name, options={})

A shorthand, clear syntax for defining many-to-one resource relationships.

Returns:

#has(cardinality, name, options = {})

A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.

Returns:

#n
#relationships(repository_name = default_repository_name)

Public Instance Method Details

belongs_to

public belongs_to(name, options={})

A shorthand, clear syntax for defining many-to-one resource relationships.

Meta Tags

Parameters:

name<Symbol>

The name that the association will be referenced by

opts<Hash>

An options hash (see below)

Returns:

<DataMapper::Association::ManyToOne> The association created should not be accessed directly

See Also:

#has
[View source]


102
103
104
105
106
107
108
109
110
111
# File 'dm-core/lib/dm-core/associations.rb', line 102

def belongs_to(name, options={})
  relationship = ManyToOne.setup(name, self, options)
  # Please leave this in - I will release contextual serialization soon
  # which requires this -- guyvdb
  # TODO convert this to a hook in the plugin once hooks work on class
  # methods
  self.init_belongs_relationship_for_serialization(relationship) if self.respond_to?(:init_belongs_relationship_for_serialization)

  relationship
end

has

public has(cardinality, name, options = {})

A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.

Meta Tags

Parameters:

cardinality

<Integer, Range, Infinity> cardinality that defines the association type and constraints

name

<Symbol> the name that the association will be referenced by

opts

<Hash> an options hash

Returns:

<DataMapper::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)

[View source]


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'dm-core/lib/dm-core/associations.rb', line 63

def has(cardinality, name, options = {})
  options = options.merge(extract_min_max(cardinality))
  options = options.merge(extract_throughness(name))

  # do not remove this. There is alot of confusion on people's
  # part about what the first argument to has() is.  For the record it
  # is the cardinality, or rather the min and max number of results
  # the association will return.  It is not, as has been assumed,
  # the number of results on the left and right hand side of the
  # reltionship.
  raise ArgumentError, 'Cardinality may not be n..n.  The cardinality specifies the min/max number of results from the association' if options[:min] == n && options[:max] == n

  klass = options[:max] == 1 ? OneToOne : OneToMany
  relationship = klass.setup(options.delete(:name), self, options)

  # Please leave this in - I will release contextual serialization soon
  # which requires this -- guyvdb
  # TODO convert this to a hook in the plugin once hooks work on class
  # methods
  self.init_has_relationship_for_serialization(relationship) if self.respond_to?(:init_has_relationship_for_serialization)

  relationship
end

n

public n
[View source]


21
22
23
24
25
# File 'dm-core/lib/dm-core/associations.rb', line 21

def n


  1.0/0
end

relationships

public relationships(repository_name = default_repository_name)
[View source]


16
17
18
19
# File 'dm-core/lib/dm-core/associations.rb', line 16

def relationships(repository_name = default_repository_name)
  @relationships ||= Hash.new { |h,k| h[k] = k == Repository.default_name ? {} : h[Repository.default_name].dup }
  @relationships[repository_name]
end