Module: Plumb::VisitorHandlers

Included in:
JSONSchemaVisitor, MermaidVisitor, MetadataVisitor
Defined in:
lib/plumb/visitor_handlers.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

NODE_NAME_FALLBACKS =

Node names introduced by the type/computation split, each mapped to the name the node was reported under before it existed. Plumb::Intersection used to be an And and Plumb::Union used to be an Or, so a visitor written against the old AST defines only on(:and) / on(:or) — without this it would raise "no handler" on a type it used to understand.

Consulted ONLY when the specific handler is absent, so a visitor that does distinguish the two (see JSONSchemaVisitor) keeps full control. Plumb's own visitors register both names explicitly; this is for visitors outside the gem.

{ intersection: :and, union: :or }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
# File 'lib/plumb/visitor_handlers.rb', line 16

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#on_missing_handler(node, _props, method_name) ⇒ Object



55
56
57
# File 'lib/plumb/visitor_handlers.rb', line 55

def on_missing_handler(node, _props, method_name)
  raise "No handler for #{node.inspect} with :#{method_name}"
end

#visit(node, props = BLANK_HASH) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/plumb/visitor_handlers.rb', line 31

def visit(node, props = BLANK_HASH)
  method_name = if node.respond_to?(:node_name)
                  node.node_name
                else
                  :"#{(node.is_a?(::Class) ? node : node.class)}_class"
                end

  clean_up_after_visit visit_name(method_name, node, props)
end

#visit_children(node, props = BLANK_HASH) ⇒ Object



59
60
61
62
63
# File 'lib/plumb/visitor_handlers.rb', line 59

def visit_children(node, props = BLANK_HASH)
  node.children.reduce(props) do |acc, child|
    visit(child, acc)
  end
end

#visit_name(method_name, node, props = BLANK_HASH) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/plumb/visitor_handlers.rb', line 41

def visit_name(method_name, node, props = BLANK_HASH)
  target = :"visit_#{method_name}"

  if !respond_to?(target) && (fallback = NODE_NAME_FALLBACKS[method_name])
    target = :"visit_#{fallback}"
  end

  if respond_to?(target)
    send(target, node, props)
  else
    on_missing_handler(node, props, target)
  end
end