Class: Vorpal::AggregateTraversal

Inherits:
Object
  • Object
show all
Defined in:
lib/vorpal/aggregate_traversal.rb

Instance Method Summary collapse

Constructor Details

#initialize(configs) ⇒ AggregateTraversal

Returns a new instance of AggregateTraversal.



4
5
6
# File 'lib/vorpal/aggregate_traversal.rb', line 4

def initialize(configs)
  @configs = configs
end

Instance Method Details

#accept(object, visitor, already_visited = []) ⇒ Object

Traversal should always begin with an object that is known to be able to reach all other objects in the aggregate (like the root!)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vorpal/aggregate_traversal.rb', line 10

def accept(object, visitor, already_visited=[])
  return if object.nil?

  config = @configs.config_for(object.class)
  return if config.nil?

  return if already_visited.include?(object)
  already_visited << object

  visitor.visit_object(object, config)

  config.belongs_tos.each do |belongs_to_config|
    associate = belongs_to_config.get_associated(object)
    accept(associate, visitor, already_visited) if visitor.continue_traversal?(belongs_to_config)
  end

  config.has_ones.each do |has_one_config|
    associate = has_one_config.get_associated(object)
    accept(associate, visitor, already_visited) if visitor.continue_traversal?(has_one_config)
  end

  config.has_manys.each do |has_many_config|
    associates = has_many_config.get_associated(object)
    raise InvariantViolated.new("#{has_many_config.pretty_name} was set to nil. Use an empty array instead.") if associates.nil?
    associates.each do |associate|
      accept(associate, visitor, already_visited) if visitor.continue_traversal?(has_many_config)
    end
  end
end