Parent

Class/Module Index [+]

Quicksearch

Mongoid::Relations::Metadata

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

Public Instance Methods

as() click to toggle source

Returns the as option of the relation.

@example Get the as option.

metadata.as

@return [ true, false ] The as option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 18
def as
  self[:as]
end
as?() click to toggle source

Tells whether an as option exists.

@example Is the as option set?

metadata.as?

@return [ true, false ] True if an as exists, false if not.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 30
def as?
  !!as
end
autosave() click to toggle source

Returns the autosave option of the relation.

@example Get the autosave option.

metadata.autosave

@return [ true, false ] The autosave option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 42
def autosave
  self[:autosave]
end
autosave?() click to toggle source

Does the metadata have a autosave option?

@example Is the relation autosaving?

metadata.autosave?

@return [ true, false ] If the relation autosaves.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 54
def autosave?
  !!autosave
end
builder(base, object) click to toggle source

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

@example Get the builder.

metadata.builder(document)

@param [ Document ] base The base document. @param [ Object ] object A document or attributes to give the builder.

@return [ Builder ] The builder for the relation.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 70
def builder(base, object)
  relation.builder(base, self, object)
end
cascade_strategy() click to toggle source

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

@example Get the strategy.

metadata.cascade_strategy

@return [ Object ] The cascading strategy to use.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 82
def cascade_strategy
  if dependent?
    strategy =
      %{Mongoid::Relations::Cascading::#{dependent.to_s.classify}}
    strategy.constantize
  else
    return nil
  end
end
cascading_callbacks?() click to toggle source

Is this an embedded relations that allows callbacks to cascade down to it?

@example Does the relation have cascading callbacks?

metadata.cascading_callbacks?

@return [ true, false ] If the relation cascades callbacks.

@since 2.3.0

# File lib/mongoid/relations/metadata.rb, line 101
def cascading_callbacks?
  !!self[:cascade_callbacks]
end
class_name() click to toggle source

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.

@example Get the class name.

metadata.class_name

@return [ String ] The name of the relation’s proxied class.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 115
def class_name
  @class_name ||= (self[:class_name] || classify).sub(/^::/,"")
end
constraint() click to toggle source

Get the foreign key contraint for the metadata.

@example Get the constaint.

metadata.constraint

@return [ Constraint ] The constraint.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 127
def constraint
  @constraint ||= Constraint.new(self)
end
criteria(object, type = nil) click to toggle source

Get the criteria that is used to query for this metadata’s relation.

@example Get the criteria.

metadata.criteria([ id_one, id_two ])

@param [ Object ] object The foreign key used for the query.

@return [ Criteria ] The criteria.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 141
def criteria(object, type = nil)
  query = relation.criteria(self, object, type)
  order ? query.order_by(order) : query
end
cyclic() click to toggle source

Returns the cyclic option of the relation.

@example Get the cyclic option.

metadata.cyclic

@return [ true, false ] The cyclic option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 154
def cyclic
  self[:cyclic]
end
cyclic?() click to toggle source

Does the metadata have a cyclic option?

@example Is the metadata cyclic?

metadata.cyclic?

@return [ true, false ] If the metadata is cyclic.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 166
def cyclic?
  !!cyclic
end
dependent() click to toggle source

Returns the dependent option of the relation.

@example Get the dependent option.

metadata.dependent

@return [ Symbol ] The dependent option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 178
def dependent
  self[:dependent]
end
dependent?() click to toggle source

Does the metadata have a dependent option?

@example Is the metadata performing cascades?

metadata.dependent?

@return [ true, false ] If the metadata cascades.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 190
def dependent?
  !!dependent
end
destructive?() click to toggle source

Does the relation have a destructive dependent option specified. This is true for :dependent => :delete and :dependent => :destroy.

@example Is the relation destructive?

metadata.destructive?

@return [ true, false ] If the relation is destructive.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 594
def destructive?
  @destructive ||= (dependent == :delete || dependent == :destroy)
end
eager_load(criteria) click to toggle source

Get the criteria needed to eager load this relation.

@example Get the eager loading criteria.

metadata.eager_load(criteria)

@param [ Criteria ] criteria The criteria to load from.

@return [ Criteria ] The eager loading criteria.

@since 2.2.0

# File lib/mongoid/relations/metadata.rb, line 204
def eager_load(criteria)
  relation.eager_load(self, criteria.clone)
end
embedded?() click to toggle source

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

@example Is the document embedded.

metadata.embedded?

@return [ true, false ] True if embedded, false if not.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 217
def embedded?
  @embedded ||= (macro == :embeds_one || macro == :embeds_many)
end
extension() click to toggle source

Returns the extension of the relation.

@example Get the relation extension.

metadata.extension

@return [ Module ] The extension or nil.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 229
def extension
  self[:extend]
end
extension?() click to toggle source

Tells whether an extension definition exist for this relation.

@example Is an extension defined?

metadata.extension?

@return [ true, false ] True if an extension exists, false if not.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 241
def extension?
  !!extension
end
forced_nil_inverse?() click to toggle source

Does this metadata have a forced nil inverse_of defined. (Used in many to manies)

@example Is this a forced nil inverse?

metadata.forced_nil_inverse?

@return [ true, false ] If inverse_of has been explicitly set to nil.

@since 2.3.3

# File lib/mongoid/relations/metadata.rb, line 254
def forced_nil_inverse?
  has_key?(:inverse_of) && inverse_of.nil?
end
foreign_key() click to toggle source

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.

@example Get the foreign key.

metadata.foreign_key

@return [ String ] The foreign key for the relation.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 273
def foreign_key
  @foreign_key ||= determine_foreign_key
end
foreign_key_check() click to toggle source

Get the name of the method to check if the foreign key has changed.

@example Get the foreign key check method.

metadata.foreign_key_check

@return [ String ] The foreign key check.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 285
def foreign_key_check
  @foreign_key_check ||= "#{foreign_key}_changed?"
end
foreign_key_setter() click to toggle source

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

@example Get the setter for the foreign key.

metadata.foreign_key_setter

@return [ String ] The foreign_key plus =.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 298
def foreign_key_setter
  @foreign_key_setter ||= "#{foreign_key}="
end
index() click to toggle source

Returns the index option of the relation.

@example Get the index option.

metadata.index

@return [ true, false ] The index option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 310
def index
  self[:index]
end
indexed?() click to toggle source

Tells whether a foreign key index exists on the relation.

@example Is the key indexed?

metadata.indexed?

@return [ true, false ] True if an index exists, false if not.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 322
def indexed?
  !!index
end
inspect() click to toggle source

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.

@example Inspect the metadata.

metadata.inspect

@return [ String ] Oodles of information in a nice format.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 349
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" <<
  "  key:                  #{key},\n" <<
  "  macro:                #{macro},\n" <<
  "  name:                 #{name},\n" <<
  "  order:                #{order.inspect || "No"},\n" <<
  "  polymorphic:          #{polymorphic? || "No"},\n" <<
  "  relation:             #{relation},\n" <<
  "  setter:               #{setter},\n" <<
  "  versioned:            #{versioned? || "No"}>\n"
end
inverse(other = nil) click to toggle source

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.

@example Get the name of the inverse.

metadata.inverse

@param [ Document ] other The document to aid in the discovery.

@return [ Symbol ] The inverse name.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 376
def inverse(other = nil)
  return self[:inverse_of] if has_key?(:inverse_of)
  return self[:as] || lookup_inverse(other) if polymorphic?
  @inverse ||= (cyclic? ? cyclic_inverse : inverse_relation)
end
inverse_class_name() click to toggle source

Returns the inverse_class_name option of the relation.

@example Get the inverse_class_name option.

metadata.inverse_class_name

@return [ true, false ] The inverse_class_name option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 390
def inverse_class_name
  self[:inverse_class_name]
end
inverse_class_name?() click to toggle source

Returns the if the inverse class name option exists.

@example Is an inverse class name defined?

metadata.inverse_class_name?

@return [ true, false ] If the inverse if defined.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 402
def inverse_class_name?
  !!inverse_class_name
end
inverse_foreign_key() click to toggle source

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.

@example Find the inverse foreign key

metadata.inverse_foreign_key

@return [ String ] The foreign key on the inverse.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 416
def inverse_foreign_key
  @inverse_foreign_key ||= determine_inverse_foreign_key
end
inverse_klass() click to toggle source

Returns the inverse class of the proxied relation.

@example Get the inverse class.

metadata.inverse_klass

@return [ Class ] The class of the inverse of the relation.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 428
def inverse_klass
  @inverse_klass ||= inverse_class_name.constantize
end
inverse_metadata(document) click to toggle source

Get the metadata for the inverse relation.

@example Get the inverse metadata.

metadata.inverse_metadata(doc)

@param [ Document ] document The document to check.

@return [ Metadata ] The inverse metadata.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 442
def inverse_metadata(document)
  document.reflect_on_association(inverse(document))
end
inverse_of() click to toggle source

Returns the inverse_of option of the relation.

@example Get the inverse_of option.

metadata.inverse_of

@return [ true, false ] The inverse_of option.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 454
def inverse_of
  self[:inverse_of]
end
inverse_of?() click to toggle source

Does the metadata have a inverse_of option?

@example Is an inverse_of defined?

metadata.inverse_of?

@return [ true, false ] If the relation has an inverse_of defined.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 466
def inverse_of?
  !!inverse_of
end
inverse_setter(other = nil) click to toggle source

Returns the setter for the inverse side of the relation.

@example Get the inverse setter.

metadata.inverse_setter

@param [ Document ] other A document to aid in the discovery.

@return [ String ] The inverse setter name.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 480
def inverse_setter(other = nil)
  "#{inverse(other)}="
end
inverse_type() click to toggle source

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

@example Get the name of the field.

metadata.inverse_type

@return [ String ] The name of the field for storing the type.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 493
def inverse_type
  @inverse_type ||=
    relation.stores_foreign_key? && polymorphic? ? "#{name}_type" : nil
end
inverse_type_setter() click to toggle source

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

@example Get the inverse type setter.

metadata.inverse_type_setter

@return [ String ] The name of the setter.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 507
def inverse_type_setter
  @inverse_type_setter ||= inverse_type ? "#{inverse_type}=" : nil
end
key() click to toggle source

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.

@example Get the lookup key.

metadata.key

@return [ String ] The association name, foreign key name, or _id.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 521
def key
  @key ||= determine_key
end
klass() click to toggle source

Returns the class of the proxied relation.

@example Get the class.

metadata.klass

@return [ Class ] The class of the relation.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 533
def klass
  @klass ||= class_name.constantize
end
macro() click to toggle source

Returns the macro for the relation of this metadata.

@example Get the macro.

metadata.macro

@return [ Symbol ] The macro.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 557
def macro
  relation.macro
end
many?() click to toggle source

Is this metadata representing a one to many or many to many relation?

@example Is the relation a many?

metadata.many?

@return [ true, false ] If the relation is a many.

@since 2.1.6

# File lib/mongoid/relations/metadata.rb, line 545
def many?
  @many ||= (relation.macro.to_s =~ /many/)
end
name() click to toggle source

Get the name associated with this metadata.

@example Get the name.

metadata.name

@return [ Symbol ] The name.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 569
def name
  self[:name]
end
name?() click to toggle source

Is the name defined?

@example Is the name defined?

metadata.name?

@return [ true, false ] If the name is defined.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 581
def name?
  !!name
end
nested_builder(attributes, options) click to toggle source

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

@example Get the nested builder.

metadata.nested_builder(attributes, options)

@param [ Hash ] attributes The attributes to build the relation with. @param [ Hash ] options Options for the nested builder.

@return [ NestedBuilder ] The nested builder for the relation.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 610
def nested_builder(attributes, options)
  relation.nested_builder(self, attributes, options)
end
order() click to toggle source

Returns default order for this association.

@example Get default order

metadata.order

@return [ Criterion::Complex, nil] nil if doesn’t set

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 727
def order
  self[:order]
end
order?() click to toggle source

Is a default order set?

@example Is the order set?

metadata.order?

@return [ true, false ] If the order is set.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 739
def order?
  !!order
end
path(document) click to toggle source

Get the path calculator for the supplied document.

@example Get the path calculator.

metadata.path(document)

@param [ Document ] document The document to calculate on.

@return [ Object ] The atomic path calculator.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 624
def path(document)
  relation.path(document)
end
polymorphic?() click to toggle source

Returns true if the relation is polymorphic.

@example Is the relation polymorphic?

metadata.polymorphic?

@return [ true, false ] True if the relation is polymorphic, false if not.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 636
def polymorphic?
  @polymorphic ||= (!!self[:as] || !!self[:polymorphic])
end
relation() click to toggle source

Get the relation associated with this metadata.

@example Get the relation.

metadata.relation

@return [ Proxy ] The relation proxy class.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 648
def relation
  self[:relation]
end
setter() click to toggle source

Gets the method name used to set this relation.

@example Get the setter.

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

@return [ String ] The name plus “=”.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 661
def setter
  @setter ||= "#{name.to_s}="
end
type() click to toggle source

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

@example Get the name of the field.

metadata.inverse_type

@return [ String ] The name of the field for storing the type.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 674
def type
  @type ||= polymorphic? ? "#{as.to_s}_type" : nil
end
type_setter() click to toggle source

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

@example Get the inverse type setter.

metadata.inverse_type_setter

@return [ String ] The name of the setter.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 687
def type_setter
  @type_setter ||= type ? "#{type}=" : nil
end
validate?() click to toggle source

Are we validating this relation automatically?

@example Is automatic validation on?

metadata.validate?

@return [ true, false ] True unless explictly set to false.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 699
def validate?
  unless self[:validate].nil?
    self[:validate]
  else
    self[:validate] = relation.validation_default
  end
end
versioned?() click to toggle source

Is this relation using Mongoid’s internal versioning system?

@example Is this relation versioned?

metadata.versioned?

@return [ true, false ] If the relation uses Mongoid versioning.

@since 2.1.0

# File lib/mongoid/relations/metadata.rb, line 715
def versioned?
  !!self[:versioned]
end

Public Class Methods

new(properties = {}) click to toggle source

Instantiate new metadata for a relation.

@example Create the new metadata.

Metadata.new(:name => :addresses)

@param [ Hash ] properties The relation options.

@since 2.0.0.rc.1

# File lib/mongoid/relations/metadata.rb, line 334
def initialize(properties = {})
  Options.validate!(properties)
  merge!(properties)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.