Module: Amfetamine::Relationships

Included in:
Base
Defined in:
lib/amfetamine/relationships.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/amfetamine/relationships.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#belongs_to_relationship?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/amfetamine/relationships.rb', line 28

def belongs_to_relationship?
  self.class._relationship_parents && self.class._relationship_parents.any?
end

#belongs_to_relationshipsObject



32
33
34
35
36
37
38
# File 'lib/amfetamine/relationships.rb', line 32

def belongs_to_relationships
  if self.class._relationship_parents
    self.class._relationship_parents.collect { |rel| self.send(rel[:class_name]) }
  else
    []
  end
end

#initialize(args = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/amfetamine/relationships.rb', line 7

def initialize(args={})
  #super(args)
  if self.class._relationship_children
    self.class._relationship_children.each do |rel|
      resource_name = rel[:resource_name]
      class_name    = rel[:class_name]
      foreign_key   = rel[:foreign_key]
      instance_variable_set("@#{resource_name}", Amfetamine::Relationship.new(on_resource_name: resource_name, on_class_name: class_name, foreign_key: foreign_key, from: self, type: :has_many))
    end
  end

  if self.class._relationship_parents
    self.class._relationship_parents.each do |rel|
      resource_name = rel[:resource_name]
      class_name    = rel[:class_name]
      foreign_key   = rel[:foreign_key]
      instance_variable_set("@#{resource_name}", Amfetamine::Relationship.new(on_resource_name: resource_name, on_class_name: class_name, foreign_key: foreign_key, from: self, type: :belongs_to))
    end
  end
end