Class: Mongoid::Relations::Metadata

Inherits:
Hash show all
Defined in:
lib/mongoid/relations/metadata.rb

Overview

The “Grand Poobah” of information about any relation is this class. It contains everything you could ever possible want to know.

Instance Method Summary collapse

Methods included from Extensions::Hash::Scoping

#scoped

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria

Constructor Details

#initialize(properties = {}) ⇒ Metadata

Instantiate new metadata for a relation.

Examples:

Create the new metadata.

Metadata.new(:name => :addresses)

Parameters:

  • properties (Hash) (defaults to: {})

    The relation options.

Since:

  • 2.0.0.rc.1



151
152
153
# File 'lib/mongoid/relations/metadata.rb', line 151

def initialize(properties = {})
  merge!(properties)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Handles two different cases - the first is a convenience for JSON like access to the hash instead of having to call []. The second is a delegation of the “*?” methods to has_key? as a convenience to check for existence of a value.

Examples:

Extras provided by this method.

.name
.name?

Parameters:

  • name (Symbol)

    The name of the method.

  • args (Array)

    The arguments passed to the method.

Returns:

  • (Object)

    Either the value or a boolen.

Since:

  • 2.0.0.rc.1



533
534
535
536
# File 'lib/mongoid/relations/metadata.rb', line 533

def method_missing(name, *args)
  method = name.to_s
  method =~ /\?/ ? has_key?(method.sub('?', '').to_sym) : self[name]
end

Instance Method Details

#builder(object) ⇒ Builder

Gets a relation builder associated with the relation this metadata is for.

Examples:

Get the builder.

.builder(document)

Parameters:

  • object (Object)

    A document or attributes to give the builder.

Returns:

  • (Builder)

    The builder for the relation.

Since:

  • 2.0.0.rc.1



22
23
24
# File 'lib/mongoid/relations/metadata.rb', line 22

def builder(object)
  relation.builder(self, object)
end

#cascade_strategyObject

Returns the name of the strategy used for handling dependent relations.

Examples:

Get the strategy.

.cascade_strategy

Returns:

  • (Object)

    The cascading strategy to use.

Since:

  • 2.0.0.rc.1



34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/relations/metadata.rb', line 34

def cascade_strategy
  if dependent?
    strategy =
      %{Mongoid::Relations::Cascading::#{dependent.to_s.classify}}
    strategy.constantize
  else
    return nil
  end
end

#class_nameString

Returns the name of the class that this relation contains. If the class_name was provided as an option this will return that, otherwise it will determine the name from the name property.

Examples:

Get the class name.

.class_name

Returns:

  • (String)

    The name of the relation’s proxied class.

Since:

  • 2.0.0.rc.1



54
55
56
# File 'lib/mongoid/relations/metadata.rb', line 54

def class_name
  @class_name ||= (self[:class_name] || classify)
end

#constraintObject



58
59
60
# File 'lib/mongoid/relations/metadata.rb', line 58

def constraint
  @constraint ||= Constraint.new(self)
end

#embedded?true, false

Will determine if the relation is an embedded one or not. Currently only checks against embeds one and many.

Examples:

Is the document embedded.

.embedded?

Returns:

  • (true, false)

    True if embedded, false if not.

Since:

  • 2.0.0.rc.1



71
72
73
# File 'lib/mongoid/relations/metadata.rb', line 71

def embedded?
  @embedded ||= (macro == :embeds_one || macro == :embeds_many)
end

#extensionProc

Returns the extension of the relation. This can be a proc or module.

Examples:

Get the relation extension.

.extension

Returns:

  • (Proc)

    The extension or nil.

Since:

  • 2.0.0.rc.1



83
84
85
# File 'lib/mongoid/relations/metadata.rb', line 83

def extension
  self[:extend]
end

#extension?true, false

Tells whether an extension definition exist for this relation.

Examples:

Is an extension defined?

.extension?

Returns:

  • (true, false)

    True if an extension exists, false if not.

Since:

  • 2.0.0.rc.1



95
96
97
# File 'lib/mongoid/relations/metadata.rb', line 95

def extension?
  !!extension
end

#foreign_keyString

Handles all the logic for figuring out what the foreign_key is for each relations query. The logic is as follows:

  1. If the developer defined a custom key, use that.

  2. If the relation stores a foreign key, use the class_name_id strategy.

  3. If the relation does not store the key, use the inverse_class_name_id strategy.

Examples:

Get the foreign key.

.foreign_key

Returns:

  • (String)

    The foreign key for the relation.

Since:

  • 2.0.0.rc.1



114
115
116
# File 'lib/mongoid/relations/metadata.rb', line 114

def foreign_key
  @foreign_key ||= determine_foreign_key
end

#foreign_key_setterString

Returns the name of the method used to set the foreign key on a document.

Examples:

Get the setter for the foreign key.

.foreign_key_setter

Returns:

  • (String)

    The foreign_key plus =.

Since:

  • 2.0.0.rc.1



127
128
129
# File 'lib/mongoid/relations/metadata.rb', line 127

def foreign_key_setter
  @foreign_key_setter ||= "#{foreign_key}="
end

#indexed?true, false

Tells whether a foreign key index exists on the relation.

Examples:

Is the key indexed?

.indexed?

Returns:

  • (true, false)

    True if an index exists, false if not.

Since:

  • 2.0.0.rc.1



139
140
141
# File 'lib/mongoid/relations/metadata.rb', line 139

def indexed?
  !!self[:index]
end

#inspectString

Since a lot of the information from the metadata is inferred and not explicitly stored in the hash, the inspection needs to be much more detailed.

Examples:

Inspect the metadata.

.inspect

Returns:

  • (String)

    Oodles of information in a nice format.

Since:

  • 2.0.0.rc.1



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/mongoid/relations/metadata.rb', line 165

def inspect
  "#<Mongoid::Relations::Metadata\n" <<
  "  class_name:           #{class_name},\n" <<
  "  cyclic:               #{cyclic || "No"},\n" <<
  "  dependent:            #{dependent || "None"},\n" <<
  "  inverse_of:           #{inverse_of || "N/A"},\n" <<
  "  inverse_setter:       #{inverse_setter},\n" <<
  "  inverse_type:         #{inverse_type || "N/A"},\n" <<
  "  inverse_type_setter:  #{inverse_type_setter || "N/A"},\n" <<
  "  key:                  #{key},\n" <<
  "  macro:                #{macro},\n" <<
  "  name:                 #{name},\n" <<
  "  polymorphic:          #{polymorphic? ? "Yes" : "No"},\n" <<
  "  relation:             #{relation},\n" <<
  "  setter:               #{setter}>\n"
end

#inverse(other = nil) ⇒ Symbol

Get the name of the inverse relation if it exists. If this is a polymorphic relation then just return the :as option that was defined.

Examples:

Get the name of the inverse.

.inverse

Parameters:

  • other (Document) (defaults to: nil)

    The document to aid in the discovery.

Returns:

  • (Symbol)

    The inverse name.

Since:

  • 2.0.0.rc.1



193
194
195
196
197
# File 'lib/mongoid/relations/metadata.rb', line 193

def inverse(other = nil)
  return self[:inverse_of] if inverse_of?
  return self[:as] || lookup_inverse(other) if polymorphic?
  @inverse ||= (cyclic? ? cyclic_inverse : inverse_relation)
end

#inverse_foreign_keyString

Used for relational many to many only. This determines the name of the foreign key field on the inverse side of the relation, since in this case there are keys on both sides.

Examples:

Find the inverse foreign key

.inverse_foreign_key

Returns:

  • (String)

    The foreign key on the inverse.

Since:

  • 2.0.0.rc.1



209
210
211
212
213
# File 'lib/mongoid/relations/metadata.rb', line 209

def inverse_foreign_key
  @inverse_foreign_key ||=
    ( inverse_of ? inverse_of.to_s.singularize : inverse_class_name.underscore ) <<
    relation.foreign_key_suffix
end

#inverse_klassClass

Returns the inverse class of the proxied relation.

Examples:

Get the inverse class.

.inverse_klass

Returns:

  • (Class)

    The class of the inverse of the relation.

Since:

  • 2.0.0.rc.1



223
224
225
# File 'lib/mongoid/relations/metadata.rb', line 223

def inverse_klass
  @inverse_klass ||= inverse_class_name.constantize
end

#inverse_setter(other = nil) ⇒ String

Returns the setter for the inverse side of the relation.

Examples:

Get the inverse setter.

.inverse_setter

Parameters:

  • other (Document) (defaults to: nil)

    A document to aid in the discovery.

Returns:

  • (String)

    The inverse setter name.

Since:

  • 2.0.0.rc.1



237
238
239
# File 'lib/mongoid/relations/metadata.rb', line 237

def inverse_setter(other = nil)
  inverse(other).to_s << "="
end

#inverse_typeString

Returns the name of the field in which to store the name of the class for the polymorphic relation.

Examples:

Get the name of the field.

.inverse_type

Returns:

  • (String)

    The name of the field for storing the type.

Since:

  • 2.0.0.rc.1



250
251
252
253
254
255
256
# File 'lib/mongoid/relations/metadata.rb', line 250

def inverse_type
  if relation.stores_foreign_key? && polymorphic?
    (polymorphic? ? name.to_s : class_name.underscore) << "_type"
  else
    return nil
  end
end

#inverse_type_setterString

Gets the setter for the field that sets the type of document on a polymorphic relation.

Examples:

Get the inverse type setter.

.inverse_type_setter

Returns:

  • (String)

    The name of the setter.

Since:

  • 2.0.0.rc.1



267
268
269
# File 'lib/mongoid/relations/metadata.rb', line 267

def inverse_type_setter
  inverse_type ? inverse_type << "=" : nil
end

#keyString

This returns the key that is to be used to grab the attributes for the relation or the foreign key or id that a referenced relation will use to query for the object.

Examples:

Get the lookup key.

.key

Returns:

  • (String)

    The association name, foreign key name, or _id.

Since:

  • 2.0.0.rc.1



281
282
283
# File 'lib/mongoid/relations/metadata.rb', line 281

def key
  @key ||= determine_key
end

#klassClass

Returns the class of the proxied relation.

Examples:

Get the class.

.klass

Returns:

  • (Class)

    The class of the relation.

Since:

  • 2.0.0.rc.1



293
294
295
# File 'lib/mongoid/relations/metadata.rb', line 293

def klass
  @klass ||= class_name.constantize
end

#macroSymbol

Returns the macro for the relation of this metadata.

Examples:

Get the macro.

.macro

Returns:

Since:

  • 2.0.0.rc.1



305
306
307
# File 'lib/mongoid/relations/metadata.rb', line 305

def macro
  relation.macro
end

#nested_builder(attributes, options) ⇒ NestedBuilder

Gets a relation nested builder associated with the relation this metadata is for. Nested builders are used in conjunction with nested attributes.

Examples:

Get the nested builder.

.nested_builder(attributes, options)

Parameters:

  • attributes (Hash)

    The attributes to build the relation with.

  • options (Hash)

    Options for the nested builder.

Returns:

Since:

  • 2.0.0.rc.1



321
322
323
# File 'lib/mongoid/relations/metadata.rb', line 321

def nested_builder(attributes, options)
  relation.nested_builder(self, attributes, options)
end

#polymorphic?true, false

Returns true if the relation is polymorphic.

Examples:

Is the relation polymorphic?

.polymorphic?

Returns:

  • (true, false)

    True if the relation is polymorphic, false if not.

Since:

  • 2.0.0.rc.1



333
334
335
# File 'lib/mongoid/relations/metadata.rb', line 333

def polymorphic?
  @polymorphic ||= (!!self[:as] || !!self[:polymorphic])
end

#setterString

Gets the method name used to set this relation.

Examples:

Get the setter.

 = Metadata.new(:name => :person)
.setter # => "person="

Returns:

  • (String)

    The name plus “=”.

Since:

  • 2.0.0.rc.1



346
347
348
# File 'lib/mongoid/relations/metadata.rb', line 346

def setter
  @setter ||= "#{name.to_s}="
end

#validate?true, false

Are we validating this relation automatically?

Examples:

Is automatic validation on?

.validate?

Returns:

  • (true, false)

    True unless explictly set to false.

Since:

  • 2.0.0.rc.1



358
359
360
# File 'lib/mongoid/relations/metadata.rb', line 358

def validate?
  self[:validate] != false
end