Class: Puppet::Pops::Evaluator::Closure

Inherits:
CallableSignature show all
Defined in:
lib/puppet/pops/evaluator/closure.rb

Overview

A Closure represents logic bound to a particular scope. As long as the runtime (basically the scope implementation) has the behaviour of Puppet 3x it is not safe to use this closure when the scope given to it when initialized goes “out of scope”.

Note that the implementation is backwards compatible in that the call method accepts a scope, but this scope is not used.

Note that this class is a CallableSignature, and the methods defined there should be used as the API for obtaining information in a callable implementation agnostic way.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CallableSignature

#args_range, #block_range, #block_type, #infinity?

Constructor Details

#initialize(evaluator, model, scope) ⇒ Closure

Returns a new instance of Closure.



17
18
19
20
21
# File 'lib/puppet/pops/evaluator/closure.rb', line 17

def initialize(evaluator, model, scope)
  @evaluator = evaluator
  @model = model
  @enclosing_scope = scope
end

Instance Attribute Details

#enclosing_scopeObject (readonly)



15
16
17
# File 'lib/puppet/pops/evaluator/closure.rb', line 15

def enclosing_scope
  @enclosing_scope
end

#evaluatorObject (readonly)



13
14
15
# File 'lib/puppet/pops/evaluator/closure.rb', line 13

def evaluator
  @evaluator
end

#modelObject (readonly)



14
15
16
# File 'lib/puppet/pops/evaluator/closure.rb', line 14

def model
  @model
end

Instance Method Details

#block_nameObject



76
77
78
79
# File 'lib/puppet/pops/evaluator/closure.rb', line 76

def block_name
  # TODO: Lambda's does not support blocks yet. This is a placeholder
  'unsupported_block'
end

#call(scope, *args) ⇒ Object

compatible with 3x AST::Lambda



32
33
34
# File 'lib/puppet/pops/evaluator/closure.rb', line 32

def call(scope, *args)
  @evaluator.call(self, args, @enclosing_scope)
end

#call_by_name(scope, args_hash, spill_over = false) ⇒ Object

Call closure with argument assignment by name



37
38
39
# File 'lib/puppet/pops/evaluator/closure.rb', line 37

def call_by_name(scope, args_hash, spill_over = false)
  @evaluator.call_by_name(self, args_hash, @enclosing_scope, spill_over)
end

#last_captures_rest?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/puppet/pops/evaluator/closure.rb', line 70

def last_captures_rest?
  # TODO: No support for this yet
  false
end

#optional_parameter_countInteger

Returns the number of optional parameters.

Returns:

  • (Integer)

    the number of optional accepted parameters



55
56
57
# File 'lib/puppet/pops/evaluator/closure.rb', line 55

def optional_parameter_count
  @model.parameters.count { |p| !p.value.nil? }
end

#parameter_countInteger

Returns the number of parameters (required and optional)

Returns:

  • (Integer)

    the total number of accepted parameters



48
49
50
51
# File 'lib/puppet/pops/evaluator/closure.rb', line 48

def parameter_count
  # yes, this is duplication of code, but it saves a method call
  (@model.parameters || []).size
end

#parameter_namesObject



60
61
62
# File 'lib/puppet/pops/evaluator/closure.rb', line 60

def parameter_names
  @model.parameters.collect {|p| p.name }
end

#parametersObject

incompatible with 3x except that it is an array of the same size



42
43
44
# File 'lib/puppet/pops/evaluator/closure.rb', line 42

def parameters()
  @model.parameters || []
end

#puppet_lambdaObject

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.

Deprecated.

Use the type system to query if an object is of Callable type, then use its signatures method for info

marker method checked with respond_to :puppet_lambda



26
27
28
# File 'lib/puppet/pops/evaluator/closure.rb', line 26

def puppet_lambda()
  true
end

#typeObject



65
66
67
# File 'lib/puppet/pops/evaluator/closure.rb', line 65

def type
  @callable || create_callable_type
end