Method: Jinx::MatchVisitor#initialize

Defined in:
lib/jinx/resource/match_visitor.rb

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

Creates a new visitor which matches source and target domain object references. The domain attributes to visit are determined by calling the selector block given to this initializer. The selector arguments consist of the match source and target.

Parameters:

  • opts (Hash) (defaults to: nil)

    a customizable set of options

  • opts (Symbol, {Symbol => Object}) (defaults to: nil)

    the visit options

Options Hash (opts):

  • :mergeable (Proc)

    the block which determines which attributes are merged

  • :matchable (Proc)

    the block which determines which attributes to match (default is the visit selector)

  • :matcher (:match)

    an object which matches sources to targets

  • :copier (Proc)

    the block which copies an unmatched source

Yields:

  • (obj)

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

Yield Parameters:

  • source (Resource)

    the matched source object

Raises:

  • (ArgumentError)


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

def initialize(opts=nil)
  raise ArgumentError.new("Reference visitor missing domain reference selector") unless block_given?
  opts = Options.to_hash(opts)
  @matcher = opts.delete(:matcher) || DEF_MATCHER
  @matchable = opts.delete(:matchable)
  @copier = opts.delete(:copier)
  # the source => target matches
  @matches = {}
  # Apply a filter to the visited reference so that only a matched reference is visited.
  # the reference filter
  flt = opts[:filter]
  opts[:filter] = Proc.new do |src|
    (flt.nil? or flt.call(src)) and !!@matches[src]
  end
  # the class => {id => target} hash
  @id_mtchs = LazyHash.new { Hash.new }
  # Match the source references before navigating from the source to its references, since
  # only a matched reference is visited.
  super do |src|
    tgt = @matches[src]
    # the attributes to match on
    mas = yield(src)
    # match the attribute references
    match_references(src, tgt, mas)
    mas
  end
end