Module: Puppet::Pops::Binder::BindingsFactory

Defined in:
lib/puppet/pops/binder/bindings_factory.rb

Defined Under Namespace

Classes: AbstractBuilder, BindingsBuilder, BindingsContainerBuilder, MultibindingsBuilder

Constant Summary collapse

T =

Alias for the TypeFactory. This is also available as the method ‘type_factory`.

Types::TypeFactory

Class Method Summary collapse

Class Method Details

.contributed_bindings(name, named_bindings) ⇒ Object

Produces a ContributedBindings. A ContributedBindings is used by bindings providers to return a set of named bindings.

Parameters:



605
606
607
608
609
610
611
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 605

def self.contributed_bindings(name, named_bindings)
  cb = Bindings::ContributedBindings.new()
  cb.name = name
  named_bindings = [named_bindings] unless named_bindings.is_a?(Array)
  named_bindings.each {|b| cb.addBindings(b) }
  cb
end

.evaluating_producer(expression) ⇒ Bindings::ProducerDescriptor

Creates an evaluating producer that evaluates a puppet expression. A puppet expression is most conveniently created by using the EvaluatingParser as it performs all set up and validation of the parsed source. Two convenience methods are used to parse an expression, or parse a ruby string as a puppet string. See methods puppet_expression, puppet_string and parser for more information.

Examples:

producing a puppet expression

expr = puppet_string("Interpolated $fqdn", __FILE__)

Parameters:

  • expression (Model::Expression)

    a puppet DSL expression as producer by the eparser.

Returns:



754
755
756
757
758
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 754

def self.evaluating_producer(expression)
  p = Bindings::EvaluatingProducerDescriptor.new()
  p.expression = expression
  p
end

.first_found_producer(*producers) ⇒ Bindings::ProducerDescriptor

Creates a first-found producer that looks up from a given series of keys. The first found looked up value will be produced.

Parameters:

Returns:



736
737
738
739
740
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 736

def self.first_found_producer(*producers)
  p = Bindings::FirstFoundProducerDescriptor.new()
  producers.each {|p2| p.addProducers(p2) }
  p
end

.hash_lookup_producer(type, name, key) ⇒ Bindings::ProducerDescriptor

Creates a Hash lookup producer that looks up a hash value, and then a key in the hash.

Parameters:

  • type (Types::PAnyType)

    the type to lookup (i.e. a Hash of some key/value type).

  • name (String)

    the name to lookup

  • key (Object)

    the key to lookup in the looked up hash (type should comply with given key type).

Returns:



723
724
725
726
727
728
729
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 723

def self.hash_lookup_producer(type, name, key)
  p = Bindings::HashLookupProducerDescriptor.new()
  p.type = type
  p.name = name
  p.key = key
  p
end

.instance_producer(class_name, *args) ⇒ Bindings::ProducerDescriptor

Creates an instance producer An instance producer creates a new instance of a class. If the class implements the class method ‘inject` this method is called instead of `new` to allow further lookups to take place. This is referred to as *assisted inject*. If the class method `inject` is missing, the regular `new` method is called.

Parameters:

  • class_name (String)

    the name of the class

  • args (Object)

    arguments to the class’ ‘new` method.

Returns:



696
697
698
699
700
701
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 696

def self.instance_producer(class_name, *args)
  p = Bindings::InstanceProducerDescriptor.new()
  p.class_name = class_name
  args.each {|a| p.addArguments(a) }
  p
end

.layered_bindings(*named_layers) ⇒ Bindings::LayeredBindings

Create a LayeredBindings. This is used by the bindings system to create a model of all given layers.

Parameters:

Returns:



776
777
778
779
780
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 776

def self.layered_bindings(*named_layers)
  result = Bindings::LayeredBindings.new()
  named_layers.each {|b| result.addLayers(b) }
  result
end

.literal_producer(value) ⇒ Bindings::ProducerDescriptor

Creates a literal/constant producer

Parameters:

  • value (Object)

    the value to produce

Returns:



657
658
659
660
661
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 657

def self.literal_producer(value)
  producer = Bindings::ConstantProducerDescriptor.new()
  producer.value = value
  producer
end

.lookup_producer(type, name) ⇒ Bindings::ProducerDescriptor

Creates a Producer that looks up a value.

Parameters:

  • type (Types::PAnyType)

    the type to lookup

  • name (String)

    the name to lookup

Returns:



708
709
710
711
712
713
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 708

def self.lookup_producer(type, name)
  p = Bindings::LookupProducerDescriptor.new()
  p.type = type
  p.name = name
  p
end

.named_bindings(name, &block) ⇒ Object

Creates a named binding container, the top bindings model object. A NamedBindings is typically produced by a bindings provider.

The created container is wrapped in a BindingsContainerBuilder for further detailing. Unwrap the built result when done.



620
621
622
623
624
625
626
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 620

def self.named_bindings(name, &block)
  binding = Bindings::NamedBindings.new()
  binding.name = name
  builder = BindingsContainerBuilder.new(binding)
  builder.instance_eval(&block) if block_given?
  builder
end

.named_layer(name, *bindings) ⇒ Object

Creates a NamedLayer. This is used by the bindings system to create a model of the layers.



764
765
766
767
768
769
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 764

def self.named_layer(name, *bindings)
  result = Bindings::NamedLayer.new()
  result.name = name
  bindings.each { |b| result.addBindings(b) }
  result
end

.non_caching_producer(producer) ⇒ Bindings::ProducerDescriptor

Creates a non caching producer

Parameters:

  • producer (Bindings::Producer)

    the producer to make non caching

Returns:



668
669
670
671
672
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 668

def self.non_caching_producer(producer)
  p = Bindings::NonCachingProducerDescriptor.new()
  p.producer = producer
  p
end

.parserParser::EvaluatingParser

Returns a parser for puppet expressions.

Returns:



783
784
785
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 783

def self.parser
  @parser ||= Parser::EvaluatingParser.new()
end

.producer_producer(producer) ⇒ Bindings::ProducerDescriptor

Creates a producer producer

Parameters:

  • producer (Bindings::Producer)

    a producer producing a Producer.

Returns:



679
680
681
682
683
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 679

def self.producer_producer(producer)
  p = Bindings::ProducerProducerDescriptor.new()
  p.producer = producer
  p
end

.puppet_expression(string, source_file) ⇒ Model::Expression

Parses and produces a puppet expression from the given string.

Parameters:

  • string (String)

    puppet source e.g. “1 + 2”

  • source_file (String)

    the source location, typically ‘__File__`

Returns:



793
794
795
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 793

def self.puppet_expression(string, source_file)
  parser.parse_string(string, source_file).current
end

.puppet_string(string, source_file) ⇒ Model::Expression

Parses and produces a puppet string expression from the given string. The string will automatically be quoted and special characters escaped. As an example if given the (ruby) string “HinMary” it is transformed to the puppet string (illustrated with a ruby string) “"Hi\nMary”” before being parsed.

Parameters:

  • string (String)

    puppet source e.g. “On node ${fqdn}”

  • source_file (String)

    the source location, typically ‘__File__`

Returns:



808
809
810
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 808

def self.puppet_string(string, source_file)
  parser.parse_string(parser.quote(string), source_file).current
end

.safe_named_bindings(name, scope, &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 variant of named_bindings evaluates the given block as a method on an anonymous class, thus, if the block defines methods or do something with the class itself, this does not pollute the base class (BindingsContainerBuilder).



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 633

def self.safe_named_bindings(name, scope, &block)
  binding = Bindings::NamedBindings.new()
  binding.name = name
  anon = Class.new(BindingsContainerBuilder) do
    def initialize(b)
      super b
    end
  end
  anon.send(:define_method, :_produce, block)
  builder = anon.new(binding)
  case block.arity
  when 0
    builder._produce()
  when 1
    builder._produce(scope)
  end
  builder
end