Class: Mongoid::Relations::Metadata

Inherits:
Hash
  • Object
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)

Constructor Details

- (Metadata) initialize(properties = {})

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



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

def initialize(properties = {})
  Options.validate!(properties)
  merge!(properties)
end

Instance Method Details

- (true, false) as

Returns the as option of the relation.

Examples:

Get the as option.

.as

Returns:

  • (true, false)

    The as option.

Since:

  • 2.1.0



19
20
21
# File 'lib/mongoid/relations/metadata.rb', line 19

def as
  self[:as]
end

- (true, false) as?

Tells whether an as option exists.

Examples:

Is the as option set?

.as?

Returns:

  • (true, false)

    True if an as exists, false if not.

Since:

  • 2.0.0.rc.1



31
32
33
# File 'lib/mongoid/relations/metadata.rb', line 31

def as?
  !!as
end

- (true, false) autobuilding?

Is the relation autobuilding if accessed via the getter and the document is new.

Examples:

Is the relation autobuilding?

.autobuilding?

Returns:

  • (true, false)

    If the relation autobuilds.

Since:

  • 3.0.0



44
45
46
# File 'lib/mongoid/relations/metadata.rb', line 44

def autobuilding?
  !!self[:autobuild]
end

- (true, false) autosave

Returns the autosave option of the relation.

Examples:

Get the autosave option.

.autosave

Returns:

  • (true, false)

    The autosave option.

Since:

  • 2.1.0



56
57
58
# File 'lib/mongoid/relations/metadata.rb', line 56

def autosave
  self[:autosave]
end

- (true, false) autosave?

Does the metadata have a autosave option?

Examples:

Is the relation autosaving?

.autosave?

Returns:

  • (true, false)

    If the relation autosaves.

Since:

  • 2.1.0



68
69
70
# File 'lib/mongoid/relations/metadata.rb', line 68

def autosave?
  !!autosave
end

- (Builder) builder(base, object)

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

Examples:

Get the builder.

.builder(document)

Parameters:

  • base (Document)

    The base document.

  • object (Object)

    A document or attributes to give the builder.

Returns:

  • (Builder)

    The builder for the relation.

Since:

  • 2.0.0.rc.1



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

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

- (Object) cascade_strategy

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



96
97
98
99
100
# File 'lib/mongoid/relations/metadata.rb', line 96

def cascade_strategy
  if dependent?
    "Mongoid::Relations::Cascading::#{dependent.to_s.classify}".constantize
  end
end

- (true, false) cascading_callbacks?

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

Examples:

Does the relation have cascading callbacks?

.cascading_callbacks?

Returns:

  • (true, false)

    If the relation cascades callbacks.

Since:

  • 2.3.0



111
112
113
# File 'lib/mongoid/relations/metadata.rb', line 111

def cascading_callbacks?
  !!self[:cascade_callbacks]
end

- (String) class_name

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



125
126
127
# File 'lib/mongoid/relations/metadata.rb', line 125

def class_name
  @class_name ||= (self[:class_name] || classify).sub(/\A::/,"")
end

- (Constraint) constraint

Get the foreign key contraint for the metadata.

Examples:

Get the constaint.

.constraint

Returns:

Since:

  • 2.0.0.rc.1



137
138
139
# File 'lib/mongoid/relations/metadata.rb', line 137

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

- (String) counter_cache_column_name

Returns the counter cache column name

Examples:

Get the counter cache column.

.counter_cache_column_name

Returns:

  • (String)

    The counter cache column

Since:

  • 3.1.0



161
162
163
164
165
166
167
# File 'lib/mongoid/relations/metadata.rb', line 161

def counter_cache_column_name
  if self[:counter_cache] == true
    "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count"
  else
    self[:counter_cache].to_s
  end
end

- (true, false) counter_cached?

Does the metadata have a counter cache?

Examples:

Is the metadata counter_cached?

.counter_cached?

Returns:

  • (true, false)

    If the metadata has counter_cache

Since:

  • 3.1.0



149
150
151
# File 'lib/mongoid/relations/metadata.rb', line 149

def counter_cached?
  !!self[:counter_cache]
end

- (Criteria) criteria(object, type)

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

Examples:

Get the criteria.

.criteria([ id_one, id_two ], Person)

Parameters:

  • object (Object)

    The foreign key used for the query.

  • type (Class)

    The base class.

Returns:

Since:

  • 2.1.0



180
181
182
# File 'lib/mongoid/relations/metadata.rb', line 180

def criteria(object, type)
  relation.criteria(self, object, type)
end

- (true, false) cyclic

Returns the cyclic option of the relation.

Examples:

Get the cyclic option.

.cyclic

Returns:

  • (true, false)

    The cyclic option.

Since:

  • 2.1.0



192
193
194
# File 'lib/mongoid/relations/metadata.rb', line 192

def cyclic
  self[:cyclic]
end

- (true, false) cyclic?

Does the metadata have a cyclic option?

Examples:

Is the metadata cyclic?

.cyclic?

Returns:

  • (true, false)

    If the metadata is cyclic.

Since:

  • 2.1.0



204
205
206
# File 'lib/mongoid/relations/metadata.rb', line 204

def cyclic?
  !!cyclic
end

- (Symbol) dependent

Returns the dependent option of the relation.

Examples:

Get the dependent option.

.dependent

Returns:

  • (Symbol)

    The dependent option.

Since:

  • 2.1.0



216
217
218
# File 'lib/mongoid/relations/metadata.rb', line 216

def dependent
  self[:dependent]
end

- (true, false) dependent?

Does the metadata have a dependent option?

Examples:

Is the metadata performing cascades?

.dependent?

Returns:

  • (true, false)

    If the metadata cascades.

Since:

  • 2.1.0



228
229
230
# File 'lib/mongoid/relations/metadata.rb', line 228

def dependent?
  !!dependent
end

- (true, false) destructive?

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

Examples:

Is the relation destructive?

.destructive?

Returns:

  • (true, false)

    If the relation is destructive.

Since:

  • 2.1.0



635
636
637
# File 'lib/mongoid/relations/metadata.rb', line 635

def destructive?
  @destructive ||= (dependent == :delete || dependent == :destroy)
end

- (true, false) embedded?

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



241
242
243
# File 'lib/mongoid/relations/metadata.rb', line 241

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

- (Module) extension

Returns the extension of the relation.

Examples:

Get the relation extension.

.extension

Returns:

  • (Module)

    The extension or nil.

Since:

  • 2.0.0.rc.1



253
254
255
# File 'lib/mongoid/relations/metadata.rb', line 253

def extension
  self[:extend]
end

- (true, false) extension?

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



265
266
267
# File 'lib/mongoid/relations/metadata.rb', line 265

def extension?
  !!extension
end

- (true, false) forced_nil_inverse?

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

Examples:

Is this a forced nil inverse?

.forced_nil_inverse?

Returns:

  • (true, false)

    If inverse_of has been explicitly set to nil.

Since:

  • 2.3.3



278
279
280
# File 'lib/mongoid/relations/metadata.rb', line 278

def forced_nil_inverse?
  @forced_nil_inverse ||= has_key?(:inverse_of) && inverse_of.nil?
end

- (String) foreign_key

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



297
298
299
# File 'lib/mongoid/relations/metadata.rb', line 297

def foreign_key
  @foreign_key ||= determine_foreign_key
end

- (String) foreign_key_check

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

Examples:

Get the foreign key check method.

.foreign_key_check

Returns:

  • (String)

    The foreign key check.

Since:

  • 2.1.0



309
310
311
# File 'lib/mongoid/relations/metadata.rb', line 309

def foreign_key_check
  @foreign_key_check ||= "#{foreign_key}_changed?"
end

- (String) foreign_key_setter

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



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

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

- (true, false) index

Returns the index option of the relation.

Examples:

Get the index option.

.index

Returns:

  • (true, false)

    The index option.

Since:

  • 2.1.0



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

def index
  self[:index]
end

- (true, false) indexed?

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



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

def indexed?
  !!index
end

- (String) inspect

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



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/mongoid/relations/metadata.rb', line 373

def inspect
%Q{#<Mongoid::Relations::Metadata
  autobuild:    #{autobuilding?}
  class_name:   #{class_name}
  cyclic:       #{cyclic.inspect}
  counter_cache:#{counter_cached?}
  dependent:    #{dependent.inspect}
  inverse_of:   #{inverse_of.inspect}
  key:          #{key}
  macro:        #{macro}
  name:         #{name}
  order:        #{order.inspect}
  polymorphic:  #{polymorphic?}
  relation:     #{relation}
  setter:       #{setter}>
}
end

- (Symbol) inverse(other = nil)

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



419
420
421
422
# File 'lib/mongoid/relations/metadata.rb', line 419

def inverse(other = nil)
  invs = inverses(other)
  invs.first if invs.count == 1
end

- (true, false) inverse_class_name

Returns the inverse_class_name option of the relation.

Examples:

Get the inverse_class_name option.

.inverse_class_name

Returns:

  • (true, false)

    The inverse_class_name option.

Since:

  • 2.1.0



432
433
434
# File 'lib/mongoid/relations/metadata.rb', line 432

def inverse_class_name
  self[:inverse_class_name]
end

- (true, false) inverse_class_name?

Returns the if the inverse class name option exists.

Examples:

Is an inverse class name defined?

.inverse_class_name?

Returns:

  • (true, false)

    If the inverse if defined.

Since:

  • 2.1.0



444
445
446
# File 'lib/mongoid/relations/metadata.rb', line 444

def inverse_class_name?
  !!inverse_class_name
end

- (String) inverse_foreign_key

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



458
459
460
# File 'lib/mongoid/relations/metadata.rb', line 458

def inverse_foreign_key
  @inverse_foreign_key ||= determine_inverse_foreign_key
end

- (Class) inverse_klass

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



470
471
472
# File 'lib/mongoid/relations/metadata.rb', line 470

def inverse_klass
  @inverse_klass ||= inverse_class_name.constantize
end

- (Metadata) inverse_metadata(object)

Get the metadata for the inverse relation.

Examples:

Get the inverse metadata.

.(doc)

Parameters:

  • object (Document, Class)

    The document or class.

Returns:

Since:

  • 2.1.0



484
485
486
# File 'lib/mongoid/relations/metadata.rb', line 484

def (object)
  object.reflect_on_association(inverse(object))
end

- (true, false) inverse_of

Returns the inverse_of option of the relation.

Examples:

Get the inverse_of option.

.inverse_of

Returns:

  • (true, false)

    The inverse_of option.

Since:

  • 2.1.0



496
497
498
# File 'lib/mongoid/relations/metadata.rb', line 496

def inverse_of
  self[:inverse_of]
end

- (true, false) inverse_of?

Does the metadata have a inverse_of option?

Examples:

Is an inverse_of defined?

.inverse_of?

Returns:

  • (true, false)

    If the relation has an inverse_of defined.

Since:

  • 2.1.0



508
509
510
# File 'lib/mongoid/relations/metadata.rb', line 508

def inverse_of?
  !!inverse_of
end

- (String) inverse_setter(other = nil)

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



522
523
524
# File 'lib/mongoid/relations/metadata.rb', line 522

def inverse_setter(other = nil)
  inverse(other).__setter__
end

- (String) inverse_type

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



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

def inverse_type
  @inverse_type ||= determine_inverse_for(:type)
end

- (String) inverse_type_setter

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



548
549
550
# File 'lib/mongoid/relations/metadata.rb', line 548

def inverse_type_setter
  @inverse_type_setter ||= inverse_type.__setter__
end

- (Array<Symbol>) inverses(other = nil)

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

Examples:

Get the names of the inverses.

.inverses

Parameters:

  • other (Document) (defaults to: nil)

    The document to aid in the discovery.

Returns:

  • (Array<Symbol>)

    The inverse name.



400
401
402
403
404
405
406
# File 'lib/mongoid/relations/metadata.rb', line 400

def inverses(other = nil)
  if self[:polymorphic]
    lookup_inverses(other)
  else
    @inverses ||= determine_inverses
  end
end

- (String) key

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



562
563
564
# File 'lib/mongoid/relations/metadata.rb', line 562

def key
  @key ||= determine_key
end

- (Class) klass

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



574
575
576
# File 'lib/mongoid/relations/metadata.rb', line 574

def klass
  @klass ||= class_name.constantize
end

- (Symbol) macro

Returns the macro for the relation of this metadata.

Examples:

Get the macro.

.macro

Returns:

Since:

  • 2.0.0.rc.1



598
599
600
# File 'lib/mongoid/relations/metadata.rb', line 598

def macro
  relation.macro
end

- (true, false) many?

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

Examples:

Is the relation a many?

.many?

Returns:

  • (true, false)

    If the relation is a many.

Since:

  • 2.1.6



586
587
588
# File 'lib/mongoid/relations/metadata.rb', line 586

def many?
  @many ||= (relation.macro.to_s =~ /many/)
end

- (Symbol) name

Get the name associated with this metadata.

Examples:

Get the name.

.name

Returns:

Since:

  • 2.1.0



610
611
612
# File 'lib/mongoid/relations/metadata.rb', line 610

def name
  self[:name]
end

- (true, false) name?

Is the name defined?

Examples:

Is the name defined?

.name?

Returns:

  • (true, false)

    If the name is defined.

Since:

  • 2.1.0



622
623
624
# File 'lib/mongoid/relations/metadata.rb', line 622

def name?
  !!name
end

- (NestedBuilder) nested_builder(attributes, options)

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



651
652
653
# File 'lib/mongoid/relations/metadata.rb', line 651

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

- (Metadata) options

Returns the metadata itself. Here for compatibility with Rails association metadata.

Examples:

Get the options.

.options

Returns:

Since:

  • 2.4.6



780
781
782
# File 'lib/mongoid/relations/metadata.rb', line 780

def options
  self
end

- (Criterion::Complex?) order

Returns default order for this association.

Examples:

Get default order

.order

Returns:

  • (Criterion::Complex, nil)

    nil if doesn't set

Since:

  • 2.1.0



792
793
794
# File 'lib/mongoid/relations/metadata.rb', line 792

def order
  self[:order]
end

- (true, false) order?

Is a default order set?

Examples:

Is the order set?

.order?

Returns:

  • (true, false)

    If the order is set.

Since:

  • 2.1.0



804
805
806
# File 'lib/mongoid/relations/metadata.rb', line 804

def order?
  !!order
end

- (Object) path(document)

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Object)

    The atomic path calculator.

Since:

  • 2.1.0



665
666
667
# File 'lib/mongoid/relations/metadata.rb', line 665

def path(document)
  relation.path(document)
end

- (true, false) polymorphic?

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



677
678
679
# File 'lib/mongoid/relations/metadata.rb', line 677

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

- (String) primary_key

Get the primary key field for finding the related document.

Examples:

Get the primary key.

.primary_key

Returns:

  • (String)

    The primary key field.

Since:

  • 3.1.0



689
690
691
# File 'lib/mongoid/relations/metadata.rb', line 689

def primary_key
  @primary_key ||= (self[:primary_key] || "_id").to_s
end

- (Proxy) relation

Get the relation associated with this metadata.

Examples:

Get the relation.

.relation

Returns:

  • (Proxy)

    The relation proxy class.

Since:

  • 2.1.0



701
702
703
# File 'lib/mongoid/relations/metadata.rb', line 701

def relation
  self[:relation]
end

- (String) setter

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



714
715
716
# File 'lib/mongoid/relations/metadata.rb', line 714

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

- (String) store_as

Key where embedded document is save. By default is the name of relation

Returns:

  • (String)

    the name of key where save

Since:

  • 3.0.0



751
752
753
# File 'lib/mongoid/relations/metadata.rb', line 751

def store_as
  @store_as ||= (self[:store_as].try(:to_s) || name.to_s)
end

- (true, false) touchable?

Is this relation touchable?

Examples:

Is the relation touchable?

.touchable?

Returns:

  • (true, false)

    If the relation can be touched.

Since:

  • 3.0.0



816
817
818
# File 'lib/mongoid/relations/metadata.rb', line 816

def touchable?
  !!self[:touch]
end

- (String) type

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



727
728
729
# File 'lib/mongoid/relations/metadata.rb', line 727

def type
  @type ||= polymorphic? ? "#{as}_type" : nil
end

- (Hash) type_relation

Returns the metadata class types.

Examples:

Get the relation class types.

.type_relation

Returns:

  • (Hash)

    The hash with relation class types.

Since:

  • 3.1.0



828
829
830
# File 'lib/mongoid/relations/metadata.rb', line 828

def type_relation
  { _type: { "$in" => klass._types }}
end

- (String) type_setter

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



740
741
742
# File 'lib/mongoid/relations/metadata.rb', line 740

def type_setter
  @type_setter ||= type.__setter__
end

- (true, false) validate?

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



763
764
765
766
767
768
769
# File 'lib/mongoid/relations/metadata.rb', line 763

def validate?
  unless self[:validate].nil?
    self[:validate]
  else
    self[:validate] = relation.validation_default
  end
end