Module: Eavi::Visitor

Defined in:
lib/eavi/visitor.rb

Overview

Extend a module/class or include a class with Visitor to make it a dynamic visitor (see the OOP visitor pattern).

Defined Under Namespace

Modules: DSL, MethodsWhenExtended, MethodsWhenIncluded, MethodsWhenIncludedAndExtended

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(visitor) ⇒ Object



30
31
32
33
34
# File 'lib/eavi/visitor.rb', line 30

def extended(visitor)
  visitor.extend(DSL)
  visitor.extend(MethodsWhenIncludedAndExtended)
  visitor.extend(MethodsWhenExtended)
end

.included(visitor) ⇒ Object



24
25
26
27
28
# File 'lib/eavi/visitor.rb', line 24

def included(visitor)
  visitor.extend(DSL)
  visitor.extend(MethodsWhenIncludedAndExtended)
  visitor.extend(MethodsWhenIncluded)
end

Instance Method Details

#visit(object, *args, as: object.class) ⇒ Object

Call the visit method associated with the type of object.

Parameters:

  • object (Object)

    The object to visit

  • *args (Object)

    The arguments passed to the called visit method

  • as: (Class) (defaults to: object.class)

    The class which the visit method is attached

Raises:



14
15
16
17
18
19
20
21
# File 'lib/eavi/visitor.rb', line 14

def visit(object, *args, as: object.class)
  as.ancestors.each do |type|
    visit_method_name = VisitMethodHelper.gen_name(type)
    next unless respond_to?(visit_method_name)
    return send(visit_method_name, object, *args)
  end
  raise NoVisitMethodError.new(self, object, as)
end