Class: Puppet::Pops::Evaluator::PuppetProc

Inherits:
Proc
  • Object
show all
Defined in:
lib/puppet/pops/evaluator/puppet_proc.rb

Overview

Complies with Proc API by mapping a Puppet::Pops::Evaluator::Closure to a ruby Proc. Creating and passing an instance of this class instead of just a plain block makes it possible to inherit the parameter info and arity from the closure. Advanced users may also access the closure itself. The Puppet::Pops::Functions::Dispatcher uses this when it needs to get the Callable type of the closure.

The class is part of the Puppet Function API for Ruby and thus public API but a user should never create an instance of this class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closurePuppet::Pops::Evaluator::Closure (readonly)

Returns the mapped closure.

Returns:



31
32
33
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 31

def closure
  @closure
end

Class Method Details

.new(closure, &block) ⇒ 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.

Creates a new instance from a closure and a block that will dispatch all parameters to the closure. The block must be similar to:

{ |*args| closure.call(*args) }

Parameters:



23
24
25
26
27
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 23

def self.new(closure, &block)
  proc = super(&block)
  proc.instance_variable_set(:@closure, closure)
  proc
end

Instance Method Details

#arityInteger

Returns the arity of the block.

Returns:

  • (Integer)

    the arity of the block



64
65
66
67
68
69
70
71
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 64

def arity
  parameters.reduce(0) do |memo, param|
    count = memo + 1
    break -count unless param[0] == :req

    count
  end
end

#lambda?Boolean

Returns always false since this proc doesn’t do the Ruby lambda magic.

Returns:

  • (Boolean)

    always false since this proc doesn’t do the Ruby lambda magic



36
37
38
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 36

def lambda?
  false
end

#parametersArray<Array<Symbol>>

Maps the closure parameters to standard Block parameter info where each parameter is represented as a two element Array where the first element is :req, :opt, or :rest and the second element is the name of the parameter.

Returns:

  • (Array<Array<Symbol>>)

    array of parameter info pairs



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 48

def parameters
  @closure.parameters.map do |param|
    sym = param.name.to_sym
    if param.captures_rest
      [:rest, sym]
    elsif param.value
      [:opt, sym]
    else
      [:req, sym]
    end
  end
end