Class: Puppet::Pops::Evaluator::Closure
Overview
A Closure represents logic bound to a particular scope. As long as the runtime (basically the scope implementation) has the behavior of Puppet 3x it is not safe to return and later use this closure.
The 3x scope is essentially a named scope with an additional internal local/ephemeral nested scope state. In 3x there is no way to directly refer to the nested scopes, instead, the named scope must be in a particular state. Specifically, closures that require a local/ephemeral scope to exist at a later point will fail. It is safe to call a closure (even with 3x scope) from the very same place it was defined, but not returning it and expecting the closure to reference the scope’s state at the point it was created.
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.
Defined Under Namespace
Classes: Dynamic, Named
Constant Summary
collapse
- CLOSURE_NAME =
'lambda'
Instance Attribute Summary collapse
Instance Method Summary
collapse
#args_range, #argument_mismatch_handler?, #block_range, #block_type, #infinity?
Constructor Details
#initialize(evaluator, model) ⇒ Closure
Returns a new instance of Closure.
55
56
57
58
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 55
def initialize(evaluator, model)
@evaluator = evaluator
@model = model
end
|
Instance Attribute Details
#enclosing_scope ⇒ Object
53
54
55
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 53
def enclosing_scope
@enclosing_scope
end
|
#evaluator ⇒ Object
51
52
53
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 51
def evaluator
@evaluator
end
|
52
53
54
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 52
def model
@model
end
|
Instance Method Details
#block_name ⇒ Object
158
159
160
161
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 158
def block_name
'unsupported_block'
end
|
#call(*args) ⇒ Object
Evaluates a closure in its enclosing scope after having matched given arguments with parameters (from left to right)
62
63
64
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 62
def call(*args)
call_with_scope(enclosing_scope, args)
end
|
#call_by_name(args_hash, enforce_parameters) ⇒ Object
80
81
82
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 80
def call_by_name(args_hash, enforce_parameters)
call_by_name_internal(enclosing_scope, args_hash, enforce_parameters)
end
|
#call_by_name_with_scope(scope, args_hash, enforce_parameters) ⇒ Object
76
77
78
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 76
def call_by_name_with_scope(scope, args_hash, enforce_parameters)
call_by_name_internal(scope, args_hash, enforce_parameters)
end
|
#closure_name ⇒ Object
166
167
168
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 166
def closure_name
CLOSURE_NAME
end
|
#invoke(instance, calling_scope, args, &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.
This method makes a Closure compatible with a Dispatch. This is used when the closure is wrapped in a Function and the function is called. (Saves an extra Dispatch that just delegates to a Closure and avoids having two checks of the argument type/arity validity).
70
71
72
73
74
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 70
def invoke(instance, calling_scope, args, &block)
enclosing_scope.with_global_scope do |global_scope|
call_with_scope(global_scope, args, &block)
end
end
|
#last_captures_rest? ⇒ Boolean
152
153
154
155
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 152
def last_captures_rest?
last = @model.parameters[-1]
last && last.captures_rest
end
|
#parameter_count ⇒ Integer
Returns the number of parameters (required and optional)
125
126
127
128
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 125
def parameter_count
@model.parameters.size
end
|
#parameter_names ⇒ Object
131
132
133
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 131
def parameter_names
@model.parameters.collect(&:name)
end
|
#parameters ⇒ Object
119
120
121
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 119
def parameters
@model.parameters
end
|
#params_struct ⇒ Object
147
148
149
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 147
def params_struct
@params_struct ||= create_params_struct
end
|
#return_type ⇒ Object
135
136
137
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 135
def return_type
@return_type ||= create_return_type
end
|
rubocop:disable Naming/MemoizedInstanceVariableName
141
142
143
|
# File 'lib/puppet/pops/evaluator/closure.rb', line 141
def type
@callable ||= create_callable_type
end
|