Class: Mongify::Mongoid::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/mongoid/model/relation.rb

Overview

This class defines a relation for an association on a mongoid model

Constant Summary collapse

EMBEDS_ONE =

Embeds one relation name

"embeds_one"
EMBEDS_MANY =

Embeds many relation name

"embeds_many"
EMBEDDED_IN =

Embedded in relation name

"embedded_in"
HAS_ONE =

Has one relation name

"has_one"
HAS_MANY =

Has many relation name

"has_many"
HAS_AND_BELONGS_TO_MANY =

Has and belongs to many relation name

"has_and_belongs_to_many"
BELONGS_TO =

Belongs to relation name

"belongs_to"
VALID_RELATIONS =

Holds a list of all allowed relations

[
  EMBEDS_ONE,
  EMBEDS_MANY,
  EMBEDDED_IN,
  HAS_ONE,
  HAS_MANY,
  HAS_AND_BELONGS_TO_MANY,
  BELONGS_TO
]
SINGULARIZE_RELATIONS =

List of fields that need to be singularized

[
  BELONGS_TO,
  EMBEDDED_IN,
  EMBEDS_ONE
]
OPTION_KEYS =

Valid Option key values currently not used

%w(class_name inverse_of)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, association, options = {}) ⇒ Relation

Returns a new instance of Relation.



47
48
49
50
51
52
53
54
55
56
# File 'lib/mongify/mongoid/model/relation.rb', line 47

def initialize(name, association, options = {})
  @name, @association, @options =  name.to_s, association.to_s, options
  unless VALID_RELATIONS.include?(@name)
    raise Mongify::Mongoid::InvalidRelation, "Mongoid does not support the relation #{name} for model associations"
  end

  #Singularize association if belongs_to or embedded_in
  self.association = association.singularize if SINGULARIZE_RELATIONS.include? name 
  
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



45
46
47
# File 'lib/mongify/mongoid/model/relation.rb', line 45

def association
  @association
end

#nameObject

Returns the value of attribute name.



45
46
47
# File 'lib/mongify/mongoid/model/relation.rb', line 45

def name
  @name
end

#optionsObject

Returns the value of attribute options.



45
46
47
# File 'lib/mongify/mongoid/model/relation.rb', line 45

def options
  @options
end