Class: ActiveShepherd::Traversal

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shepherd/traversal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visitor, params = {}) ⇒ Traversal

Returns a new instance of Traversal.



4
5
6
7
8
# File 'lib/active_shepherd/traversal.rb', line 4

def initialize(visitor, params = {})
  @associations = params[:associations]
  @attributes   = params[:attributes]
  @visitor      = visitor
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



2
3
4
# File 'lib/active_shepherd/traversal.rb', line 2

def associations
  @associations
end

#attributesObject (readonly)

Returns the value of attribute attributes.



2
3
4
# File 'lib/active_shepherd/traversal.rb', line 2

def attributes
  @attributes
end

#visitorObject (readonly)

Returns the value of attribute visitor.



2
3
4
# File 'lib/active_shepherd/traversal.rb', line 2

def visitor
  @visitor
end

Instance Method Details

#traverseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_shepherd/traversal.rb', line 10

def traverse
  attributes.each do |attribute_name, object|
    visit :handle_attribute, attribute_name, object
  end

  associations.each do |name, (reflection, object)|
    if reflection.macro == :has_many
      visit :handle_has_many_association, reflection, object
    elsif reflection.macro == :has_one
      visit :handle_has_one_association, reflection, object
    end
  end
end

#visit(method_name, arg1, arg2) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/active_shepherd/traversal.rb', line 24

def visit(method_name, arg1, arg2)
  if visitor.respond_to? method_name
    if visitor.method(method_name).arity == 2
      visitor.public_send method_name, arg1, arg2
    else
      visitor.public_send method_name, arg1, *Array.wrap(arg2)
    end
  end
end