class Puppet::Pops::Visitor
A Visitor performs delegation to a given receiver based on the configuration of the Visitor. A new visitor is created with a given receiver, a method prefix, min, and max argument counts. e.g.
visitor = Visitor.new(self, "visit_from", 1, 1)
will make the visitor call “self.visit_from_CLASS(x)” where CLASS is resolved to the given objects class, or one of is ancestors, the first class for which there is an implementation of a method will be selected.
Raises RuntimeError if there are too few or too many arguments, or if the receiver is not configured to handle a given visiting object.
Constants
- NO_ARGS
Attributes
Public Class Methods
# File lib/puppet/pops/visitor.rb 16 def initialize(receiver, message, min_args=0, max_args=nil) 17 raise ArgumentError.new("min_args must be >= 0") if min_args < 0 18 raise ArgumentError.new("max_args must be >= min_args or nil") if max_args && max_args < min_args 19 20 @receiver = receiver 21 @message = message 22 @min_args = min_args 23 @max_args = max_args 24 @cache = Hash.new 25 end
Public Instance Methods
Visit the configured receiver
# File lib/puppet/pops/visitor.rb 28 def visit(thing, *args) 29 visit_this(@receiver, thing, args) 30 end
Visit an explicit receiver
# File lib/puppet/pops/visitor.rb 35 def visit_this(receiver, thing, args) 36 raise "Visitor Error: Too few arguments passed. min = #{@min_args}" unless args.length >= @min_args 37 if @max_args 38 raise "Visitor Error: Too many arguments passed. max = #{@max_args}" unless args.length <= @max_args 39 end 40 method_name = @cache[thing.class] 41 if method_name 42 return receiver.send(method_name, thing, *args) 43 else 44 thing.class.ancestors().each do |ancestor| 45 name = ancestor.name 46 next if name.nil? 47 method_name = :"#{@message}_#{name.split(DOUBLE_COLON).last}" 48 next unless receiver.respond_to?(method_name, true) 49 @cache[thing.class] = method_name 50 return receiver.send(method_name, thing, *args) 51 end 52 end 53 raise "Visitor Error: the configured receiver (#{receiver.class}) can't handle instance of: #{thing.class}" 54 end
Visit an explicit receiver with 0 args (This is ~30% faster than calling the general method)
# File lib/puppet/pops/visitor.rb 81 def visit_this_0(receiver, thing) 82 method_name = @cache[thing.class] 83 if method_name 84 return receiver.send(method_name, thing) 85 end 86 visit_this(receiver, thing, NO_ARGS) 87 end
Visit an explicit receiver with 1 args (This is ~30% faster than calling the general method)
# File lib/puppet/pops/visitor.rb 92 def visit_this_1(receiver, thing, arg) 93 method_name = @cache[thing.class] 94 if method_name 95 return receiver.send(method_name, thing, arg) 96 end 97 visit_this(receiver, thing, [arg]) 98 end
Visit an explicit receiver with 2 args (This is ~30% faster than calling the general method)
# File lib/puppet/pops/visitor.rb 103 def visit_this_2(receiver, thing, arg1, arg2) 104 method_name = @cache[thing.class] 105 if method_name 106 return receiver.send(method_name, thing, arg1, arg2) 107 end 108 visit_this(receiver, thing, [arg1, arg2]) 109 end
Visit an explicit receiver with 3 args (This is ~30% faster than calling the general method)
# File lib/puppet/pops/visitor.rb 114 def visit_this_3(receiver, thing, arg1, arg2, arg3) 115 method_name = @cache[thing.class] 116 if method_name 117 return receiver.send(method_name, thing, arg1, arg2, arg3) 118 end 119 visit_this(receiver, thing, [arg1, arg2, arg3]) 120 end
Visit an explicit receiver
# File lib/puppet/pops/visitor.rb 57 def visit_this_class(receiver, clazz, args) 58 raise "Visitor Error: Too few arguments passed. min = #{@min_args}" unless args.length >= @min_args 59 if @max_args 60 raise "Visitor Error: Too many arguments passed. max = #{@max_args}" unless args.length <= @max_args 61 end 62 method_name = @cache[clazz] 63 if method_name 64 return receiver.send(method_name, clazz, *args) 65 else 66 clazz.ancestors().each do |ancestor| 67 name = ancestor.name 68 next if name.nil? 69 method_name = :"#{@message}_#{name.split(DOUBLE_COLON).last}" 70 next unless receiver.respond_to?(method_name, true) 71 @cache[clazz] = method_name 72 return receiver.send(method_name, clazz, *args) 73 end 74 end 75 raise "Visitor Error: the configured receiver (#{receiver.class}) can't handle instance of: #{clazz}" 76 end