Class: Puppet::Pops::Evaluator::Closure
- Inherits:
-
CallableSignature
- Object
- CallableSignature
- Puppet::Pops::Evaluator::Closure
- 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
- #enclosing_scope ⇒ Object readonly
- #evaluator ⇒ Object readonly
- #model ⇒ Object readonly
Instance Method Summary collapse
- #block_name ⇒ Object
-
#call(scope, *args) ⇒ Object
compatible with 3x AST::Lambda.
-
#call_by_name(scope, args_hash, spill_over = false) ⇒ Object
Call closure with argument assignment by name.
-
#initialize(evaluator, model, scope) ⇒ Closure
constructor
A new instance of Closure.
- #last_captures_rest? ⇒ Boolean
-
#optional_parameter_count ⇒ Integer
Returns the number of optional parameters.
-
#parameter_count ⇒ Integer
Returns the number of parameters (required and optional).
- #parameter_names ⇒ Object
-
#parameters ⇒ Object
incompatible with 3x except that it is an array of the same size.
-
#puppet_lambda ⇒ Object
deprecated
private
Deprecated.
Use the type system to query if an object is of Callable type, then use its signatures method for info
- #type ⇒ Object
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_scope ⇒ Object (readonly)
15 16 17 |
# File 'lib/puppet/pops/evaluator/closure.rb', line 15 def enclosing_scope @enclosing_scope end |
#evaluator ⇒ Object (readonly)
13 14 15 |
# File 'lib/puppet/pops/evaluator/closure.rb', line 13 def evaluator @evaluator end |
#model ⇒ Object (readonly)
14 15 16 |
# File 'lib/puppet/pops/evaluator/closure.rb', line 14 def model @model end |
Instance Method Details
#block_name ⇒ Object
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
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_count ⇒ Integer
Returns the number of optional 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_count ⇒ Integer
Returns the number of parameters (required and optional)
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_names ⇒ Object
60 61 62 |
# File 'lib/puppet/pops/evaluator/closure.rb', line 60 def parameter_names @model.parameters.collect {|p| p.name } end |
#parameters ⇒ Object
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_lambda ⇒ 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.
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 |
#type ⇒ Object
65 66 67 |
# File 'lib/puppet/pops/evaluator/closure.rb', line 65 def type @callable || create_callable_type end |