Class: Puppet::Pops::Visitor Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
configured to handle a given visiting object.
Constant Summary collapse
- NO_ARGS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
EMPTY_ARRAY
Instance Attribute Summary collapse
- #cache ⇒ Object readonly private
- #max_args ⇒ Object readonly private
- #message ⇒ Object readonly private
- #min_args ⇒ Object readonly private
- #receiver ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(receiver, message, min_args = 0, max_args = nil) ⇒ Visitor
constructor
private
A new instance of Visitor.
-
#visit(thing, *args) ⇒ Object
private
Visit the configured receiver.
-
#visit_this(receiver, thing, args) ⇒ Object
private
Visit an explicit receiver.
-
#visit_this_0(receiver, thing) ⇒ Object
private
Visit an explicit receiver with 0 args (This is ~30% faster than calling the general method).
-
#visit_this_1(receiver, thing, arg) ⇒ Object
private
Visit an explicit receiver with 1 args (This is ~30% faster than calling the general method).
-
#visit_this_2(receiver, thing, arg1, arg2) ⇒ Object
private
Visit an explicit receiver with 2 args (This is ~30% faster than calling the general method).
-
#visit_this_3(receiver, thing, arg1, arg2, arg3) ⇒ Object
private
Visit an explicit receiver with 3 args (This is ~30% faster than calling the general method).
-
#visit_this_class(receiver, clazz, args) ⇒ Object
private
Visit an explicit receiver.
Constructor Details
#initialize(receiver, message, min_args = 0, max_args = nil) ⇒ Visitor
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Visitor.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/puppet/pops/visitor.rb', line 18 def initialize(receiver, , min_args = 0, max_args = nil) raise ArgumentError, "min_args must be >= 0" if min_args < 0 raise ArgumentError, "max_args must be >= min_args or nil" if max_args && max_args < min_args @receiver = receiver @message = @min_args = min_args @max_args = max_args @cache = Hash.new end |
Instance Attribute Details
#cache ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/puppet/pops/visitor.rb', line 16 def cache @cache end |
#max_args ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/puppet/pops/visitor.rb', line 16 def max_args @max_args end |
#message ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/puppet/pops/visitor.rb', line 16 def @message end |
#min_args ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/puppet/pops/visitor.rb', line 16 def min_args @min_args end |
#receiver ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/puppet/pops/visitor.rb', line 16 def receiver @receiver end |
Instance Method Details
#visit(thing, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit the configured receiver
30 31 32 |
# File 'lib/puppet/pops/visitor.rb', line 30 def visit(thing, *args) visit_this(@receiver, thing, args) end |
#visit_this(receiver, thing, args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/puppet/pops/visitor.rb', line 37 def visit_this(receiver, thing, args) raise "Visitor Error: Too few arguments passed. min = #{@min_args}" unless args.length >= @min_args if @max_args raise "Visitor Error: Too many arguments passed. max = #{@max_args}" unless args.length <= @max_args end method_name = @cache[thing.class] if method_name return receiver.send(method_name, thing, *args) else thing.class.ancestors().each do |ancestor| name = ancestor.name next if name.nil? method_name = :"#{@message}_#{name.split(DOUBLE_COLON).last}" next unless receiver.respond_to?(method_name, true) @cache[thing.class] = method_name return receiver.send(method_name, thing, *args) end end raise "Visitor Error: the configured receiver (#{receiver.class}) can't handle instance of: #{thing.class}" end |
#visit_this_0(receiver, thing) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver with 0 args (This is ~30% faster than calling the general method)
91 92 93 94 95 96 97 98 |
# File 'lib/puppet/pops/visitor.rb', line 91 def visit_this_0(receiver, thing) method_name = @cache[thing.class] if method_name return receiver.send(method_name, thing) end visit_this(receiver, thing, NO_ARGS) end |
#visit_this_1(receiver, thing, arg) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver with 1 args (This is ~30% faster than calling the general method)
103 104 105 106 107 108 109 110 |
# File 'lib/puppet/pops/visitor.rb', line 103 def visit_this_1(receiver, thing, arg) method_name = @cache[thing.class] if method_name return receiver.send(method_name, thing, arg) end visit_this(receiver, thing, [arg]) end |
#visit_this_2(receiver, thing, arg1, arg2) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver with 2 args (This is ~30% faster than calling the general method)
115 116 117 118 119 120 121 122 |
# File 'lib/puppet/pops/visitor.rb', line 115 def visit_this_2(receiver, thing, arg1, arg2) method_name = @cache[thing.class] if method_name return receiver.send(method_name, thing, arg1, arg2) end visit_this(receiver, thing, [arg1, arg2]) end |
#visit_this_3(receiver, thing, arg1, arg2, arg3) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver with 3 args (This is ~30% faster than calling the general method)
127 128 129 130 131 132 133 134 |
# File 'lib/puppet/pops/visitor.rb', line 127 def visit_this_3(receiver, thing, arg1, arg2, arg3) method_name = @cache[thing.class] if method_name return receiver.send(method_name, thing, arg1, arg2, arg3) end visit_this(receiver, thing, [arg1, arg2, arg3]) end |
#visit_this_class(receiver, clazz, args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Visit an explicit receiver
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/puppet/pops/visitor.rb', line 63 def visit_this_class(receiver, clazz, args) raise "Visitor Error: Too few arguments passed. min = #{@min_args}" unless args.length >= @min_args if @max_args raise "Visitor Error: Too many arguments passed. max = #{@max_args}" unless args.length <= @max_args end method_name = @cache[clazz] if method_name return receiver.send(method_name, clazz, *args) else clazz.ancestors().each do |ancestor| name = ancestor.name next if name.nil? method_name = :"#{@message}_#{name.split(DOUBLE_COLON).last}" next unless receiver.respond_to?(method_name, true) @cache[clazz] = method_name return receiver.send(method_name, clazz, *args) end end raise "Visitor Error: the configured receiver (#{receiver.class}) can't handle instance of: #{clazz}" end |