Module: FAP::Mixins::Relations

Defined in:
lib/fap/mixins/relations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/fap/mixins/relations.rb', line 8

def self.included base
  base.class_eval <<-EOS, __FILE__, __LINE__ + 1
    extlib_inheritable_accessor(:relations) unless self.respond_to?(:relations)
    self.relations ||= []
  EOS
  base.extend ClassMethods
end

Instance Method Details

#_load_belongs_to(relation) ⇒ Object



37
38
39
# File 'lib/fap/mixins/relations.rb', line 37

def _load_belongs_to relation
  nil
end

#_load_has_many(relation) ⇒ Object



32
33
34
35
# File 'lib/fap/mixins/relations.rb', line 32

def _load_has_many relation
  relation.from = self
  FAP::Collection.new relation, @_node
end

#_load_relation(name) ⇒ FAP::Collection, FAP::Paw

Load a relation by its nane

Returns:

  • (FAP::Collection)

    for “has_many” relations

  • (FAP::Paw)

    “father” object for “belongs_to” relations



21
22
23
24
25
26
27
28
29
30
# File 'lib/fap/mixins/relations.rb', line 21

def _load_relation name
  relation = self.relations.select { |rel| rel.name == name }.first
  if relation.type == :has_many
    _load_has_many relation
  elsif relation.type == :belongs_to
    _load_belongs_to relation
  else
    raise "Unkown relation type"
  end
end