Class: Puppet::Pops::Binder::Producers::Producer Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/binder/producers.rb

Overview

This class is abstract.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(injector, binding, scope, options) ⇒ Producer

Creates a Producer. Derived classes should call this constructor to get support for transformer lambda.

Parameters:

Options Hash (options):



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, options)
  if transformer_lambda = options[: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

#transformerObject (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.

Parameters:

  • scope (Puppet::Parser:Scope)

    the scope to use for evaluation

  • args (Object)

    arguments to custom producers, always empty for implicit productions

Returns:

  • (Object)

    the produced instance (should never be nil).



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.

Parameters:

  • scope (Puppet::Parser:Scope)

    the scope to use for evaluation

Returns:

  • (Puppet::Pops::Binder::Producer)

    the producer to use

See Also:

  • for an example of implementation.


89
90
91
# File 'lib/puppet/pops/binder/producers.rb', line 89

def producer(scope)
  self
end