module Eavi::Visitor

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

Public Class Methods

extended(visitor) click to toggle source
# File lib/eavi/visitor.rb, line 30
def extended(visitor)
  visitor.extend(DSL)
  visitor.extend(MethodsWhenIncludedAndExtended)
  visitor.extend(MethodsWhenExtended)
end
included(visitor) click to toggle source
# File lib/eavi/visitor.rb, line 24
def included(visitor)
  visitor.extend(DSL)
  visitor.extend(MethodsWhenIncludedAndExtended)
  visitor.extend(MethodsWhenIncluded)
end

Public Instance Methods

visit(object, *args, as: object.class) click to toggle source

Call the visit method associated with the type of object.

@param [Object] object The object to visit @param [Object] *args The arguments passed to the called visit method @param [Class] as: The class which the visit method is attached @returns The result of the called visit method

# 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