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:



29
30
31
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 29

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:



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

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



62
63
64
65
66
67
68
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 62

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



34
35
36
# File 'lib/puppet/pops/evaluator/puppet_proc.rb', line 34

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



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

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