Class: Jinx::ReferenceVisitor

Inherits:
Visitor show all
Defined in:
lib/jinx/resource/reference_visitor.rb

Overview

A ReferenceVisitor traverses reference attributes.

Direct Known Subclasses

MatchVisitor, ReferencePathVisitor

Instance Attribute Summary

Attributes inherited from Visitor

#lineage, #options, #visited

Instance Method Summary collapse

Methods inherited from Visitor

#clear, #current, #cyclic_nodes, #depth_first?, #filter, #from, #node_children, #root, #sync, #to_enum, #visit, #visit_children, #visit_node_and_children, #visit_recursive, #visit_root, #visited?

Constructor Details

#initialize(opts = nil) {|obj| ... } ⇒ ReferenceVisitor

Creates a new ReferenceVisitor on domain reference attributes.

The required selector block given to this initializer determines which attributes to visit. The references to visit next thus consist of the current domain object’s selector attributes’ values. If the :filter option is set, then the given filter block is applied to each selected attribute reference to determine which domain objects will be visited.

Parameters:

Options Hash (opts):

  • :filter (Proc)

    an optional filter on the reference to visit

Yields:

  • (obj)

    returns the AttributeEnumerator of attributes to visit next from the current domain object

Yield Parameters:

  • obj (Resource)

    the current domain object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jinx/resource/reference_visitor.rb', line 24

def initialize(opts=nil, &selector)
  raise ArgumentError.new("Reference visitor missing domain reference selector") unless block_given?
  # the property selector
  @selector = selector
  # the reference filter
  @filter = Options.get(:filter, opts)
  # Initialize the Visitor with a reference enumerator which selects the reference
  # attributes and applies the optional filter if necessary.
  @ref_enums = {}
  super do |obj|
    # the reference property filter
    ras = attributes_to_visit(obj)
    if ras then
      logger.debug { "#{qp} visiting #{obj} attributes #{ras.pp_s(:single_line)}..." } if @verbose
      # an enumerator on the reference properties
      enum = ReferenceEnumerator.new(obj, ras.properties)
      # If there is a reference filter, then apply it to the enum references.
      @filter ? enum.filter(&@filter) : enum
    end
  end
end

Instance Method Details

#attributes_to_visit(obj) ⇒ Propertied::Filter (private)

Returns the attributes to visit.

Parameters:

  • obj (Resource)

    the visiting object

Returns:

  • (Propertied::Filter)

    the attributes to visit



50
51
52
# File 'lib/jinx/resource/reference_visitor.rb', line 50

def attributes_to_visit(obj)
  @selector.call(obj)
end