Class: Puppet::Pops::Binder::Producers::Producer Abstract
- Defined in:
- lib/puppet/pops/binder/producers.rb
Overview
Producer is an abstract base class representing the base contract for a bound producer. Typically, when a lookup is performed it is the value that is returned (via a producer), but it is also possible to lookup the producer, and ask it to produce the value (the producer may return a series of values, which makes this especially useful).
When looking up a producer, it is of importance to only use the API of the Producer class unless it is known that a particular custom producer class has been bound.
Custom Producers
The intent is that this class is derived for custom producers that require additional options/arguments when producing an instance. Such a custom producer may raise an error if called with too few arguments, or may implement specific ‘produce` methods and always raise an error on #produce indicating that this producer requires custom calls and that it can not be used as an implicit producer.
Features of Producer
The Producer class is abstract, but offers the ability to transform the produced result by passing the option ‘:transformer` which should be a Puppet Lambda Expression taking one argument and producing the transformed (wanted) result.
Direct Known Subclasses
AbstractArgumentedProducer, AbstractValueProducer, AssistedInjectProducer, EvaluatingProducer, FirstFoundProducer, ProducerProducer, SingletonProducerProducer
Instance Attribute Summary collapse
-
#transformer ⇒ Object
readonly
A Puppet 3 AST Lambda Expression.
Instance Method Summary collapse
-
#initialize(injector, binding, scope, options) ⇒ Producer
constructor
Creates a Producer.
-
#produce(scope, *args) ⇒ Object
Produces an instance.
-
#producer(scope) ⇒ Puppet::Pops::Binder::Producer
Returns the producer after possibly having recreated an internal/wrapped producer.
Constructor Details
#initialize(injector, binding, scope, options) ⇒ Producer
Creates a Producer. Derived classes should call this constructor to get support for transformer lambda.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/puppet/pops/binder/producers.rb', line 57 def initialize(injector, binding, scope, ) if transformer_lambda = [:transformer] if transformer_lambda.is_a?(Proc) raise ArgumentError, "Transformer Proc must take two arguments; scope, value." unless transformer_lambda.arity == 2 @transformer = transformer_lambda else raise ArgumentError, "Transformer must be a LambdaExpression" unless transformer_lambda.is_a?(Puppet::Pops::Model::LambdaExpression) raise ArgumentError, "Transformer lambda must take one argument; value." unless transformer_lambda.parameters.size() == 1 @transformer = Puppet::Pops::Parser::EvaluatingParser.new.closure(transformer_lambda, scope) end end end |
Instance Attribute Details
#transformer ⇒ Object (readonly)
A Puppet 3 AST Lambda Expression
46 47 48 |
# File 'lib/puppet/pops/binder/producers.rb', line 46 def transformer @transformer end |
Instance Method Details
#produce(scope, *args) ⇒ Object
Produces an instance.
76 77 78 |
# File 'lib/puppet/pops/binder/producers.rb', line 76 def produce(scope, *args) do_transformation(scope, internal_produce(scope)) end |
#producer(scope) ⇒ Puppet::Pops::Binder::Producer
Returns the producer after possibly having recreated an internal/wrapped producer. This implementation returns ‘self`. A derived class may want to override this method to perform initialization/refresh of its internal state. This method is called when a producer is requested.
89 90 91 |
# File 'lib/puppet/pops/binder/producers.rb', line 89 def producer(scope) self end |