Class: Metasploit::Model::Visitation::Visitor

Inherits:
Base
  • Object
show all
Defined in:
app/models/metasploit/model/visitation/visitor.rb

Overview

Visits node in a tree, such as Search::Query#tree.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#valid!

Constructor Details

#initialize(attributes = {}) {|node| ... } ⇒ Visitor

Returns a new instance of Visitor.

Parameters:

  • attributes (Hash{Symbol => Object}) (defaults to: {})

Options Hash (attributes):

Yields:

  • (node)

    Block instance_exec'd on instance of :parent class.

Yield Parameters:

  • node (Object)

    node being visited.

Yield Returns:

  • (Object)

    translation of node.


49
50
51
52
53
54
# File 'app/models/metasploit/model/visitation/visitor.rb', line 49

def initialize(attributes={}, &block)
  attributes.assert_valid_keys(:module_name, :parent)

  @block = block
  super
end

Instance Attribute Details

#blockProc

Block that is instance_exec'd on instance of #parent and passed the node to visit.

Returns:

  • (Proc)

12
13
14
# File 'app/models/metasploit/model/visitation/visitor.rb', line 12

def block
  @block
end

#module_nameString

Name of Module (or Class) that can be visited by this visitor. This visitor is also assumed to be able to #visit any Class or Module that has the Module or Class with module_name as a Module#ancestor.

Returns:

  • (String)

19
20
21
# File 'app/models/metasploit/model/visitation/visitor.rb', line 19

def module_name
  @module_name
end

#parentClass

The Class on which this visitor was created.

Returns:

  • (Class)

25
26
27
# File 'app/models/metasploit/model/visitation/visitor.rb', line 25

def parent
  @parent
end

Instance Method Details

#visit(instance, node) ⇒ Object

Visit node with #block instance_exec'd on instance.

Parameters:

  • instance (Object)

    instance of #parent.

  • node (Object)

    node being visited.

Returns:

  • (Object)

Raises:

  • (TypeError)

    if instance is not a #parent.


62
63
64
65
66
67
68
# File 'app/models/metasploit/model/visitation/visitor.rb', line 62

def visit(instance, node)
  unless instance.is_a? parent
    raise TypeError, "#{instance} is not an instance of #{parent}, so it cannot be visited."
  end

  instance.instance_exec(node, &block)
end