Class: Puppet::Pops::Binder::BindingsFactory::BindingsBuilder

Inherits:
AbstractBuilder show all
Defined in:
lib/puppet/pops/binder/bindings_factory.rb

Overview

Builds a Binding via convenience methods.

Direct Known Subclasses

MultibindingsBuilder

Instance Attribute Summary

Attributes inherited from AbstractBuilder

#model

Instance Method Summary collapse

Methods inherited from AbstractBuilder

#method_missing

Constructor Details

#initialize(binding) ⇒ BindingsBuilder

Returns a new instance of BindingsBuilder.

Parameters:



166
167
168
169
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 166

def initialize(binding)
  super binding
  data()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Puppet::Pops::Binder::BindingsFactory::AbstractBuilder

Instance Method Details

#abstractObject

Sets the binding to be abstract (it must be overridden)



185
186
187
188
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 185

def abstract
  model.abstract = true
  self
end

#array_of(t) ⇒ Puppet::Pops::Types::PArrayType

Sets the type of the binding to Array, where T is given.

Parameters:

Returns:



290
291
292
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 290

def array_of(t)
  type(T.array_of(t))
end

#array_of_dataPuppet::Pops::Types::PArrayType

Sets the type of the binding to Array.

Returns:



282
283
284
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 282

def array_of_data()
  type(T.array_of_data())
end

#booleanPuppet::Pops::Types::PBooleanType

Sets the type of the binding to Boolean.

Returns:



247
248
249
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 247

def boolean()
  type(T.boolean())
end

#dataPuppet::Pops::Types::PDataType

Sets the type of the binding to the abstract type Data.

Returns:



275
276
277
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 275

def data()
  type(T.data())
end

#finalObject

Sets the binding to be final (it may not be overridden)



199
200
201
202
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 199

def final
  model.final = true
  self
end

#floatPuppet::Pops::Types::PFloatType

Sets the type of the binding to Float.

Returns:



240
241
242
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 240

def float()
  type(T.float())
end

#hash_of(t) ⇒ Puppet::Pops::Types::PHashType

Sets type of the binding to ‘Hash[Literal, t]`. To also limit the key type, use #type and give it a fully specified hash using #type_factory and then `hash_of(value_type, key_type)`.

Returns:



306
307
308
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 306

def hash_of(t)
  type(T.hash_of(t))
end

#hash_of_dataPuppet::Pops::Types::PHashType

Sets the type of the binding to Hash[Literal, Data].

Returns:



297
298
299
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 297

def hash_of_data()
  type(T.hash_of_data())
end

#in_multibind(id) ⇒ Object

Makes the binding a multibind contribution to the given multibind id

Parameters:

  • id (String)

    the multibind id to contribute this binding to



207
208
209
210
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 207

def in_multibind(id)
  model.multibind_id = id
  self
end

#instance_of(t) ⇒ Object #instance_of(o) ⇒ Object #instance_of(c) ⇒ Object #instance_of(s) ⇒ Object

Sets the type of the binding based on the given argument.

Overloads:

  • #instance_of(t) ⇒ Object

    The same as calling #type with ‘t`.

    Parameters:

  • #instance_of(o) ⇒ Object

    Infers the type from the given Ruby object and sets that as the type - i.e. “set the type of the binding to be that of the given data object”.

    Parameters:

    • o (Object)

      the object to infer the type from

  • #instance_of(c) ⇒ Object

    Sets the type based on the given ruby class. The result is one of the specific puppet types if the class can be represented by a specific type, or the open ended PRuntimeType otherwise.

    Parameters:

    • c (Class)

      the Class to base the type on.

  • #instance_of(s) ⇒ Object

    The same as using a class, but instead of giving a class instance, the class is expressed using its fully qualified name. This method of specifying the type allows late binding (the class does not have to be loaded before it can be used in a binding).

    Parameters:

    • s (String)

      the fully qualified classname to base the type on.

Returns:

  • the resulting type



330
331
332
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 330

def instance_of(t)
  type(T.type_of(t))
end

#integerPuppet::Pops::Types::PIntegerType

Sets the type of the binding to Integer.

Returns:



233
234
235
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 233

def integer()
  type(T.integer())
end

#name(name) ⇒ Object Also known as: named

Sets the name of the binding.

Parameters:

  • name (String)

    the name to bind.



174
175
176
177
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 174

def name(name)
  model.name = name
  self
end

#overrideObject

Sets the binding to be override (it must override something)



192
193
194
195
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 192

def override
  model.override = true
  self
end

#patternPuppet::Pops::Types::PRegexpType

Sets the type of the binding to Pattern.

Returns:



261
262
263
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 261

def pattern()
  type(T.pattern())
end

#producer_options(options) ⇒ Object

Note:

A Ruby lambda is not cross platform safe. Use a puppet lambda if you want a bindings model that is.

Sets options to the producer. See the respective producer for the options it supports. All producers supports the option ‘:transformer`, a puppet or ruby lambda that is evaluated with the produced result as an argument. The ruby lambda gets scope and value as arguments.



561
562
563
564
565
566
567
568
569
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 561

def producer_options(options)
  options.each do |k, v|
    arg = Puppet::Pops::Binder::Bindings::NamedArgument.new()
    arg.name = k.to_s
    arg.value = v
    model.addProducer_args(arg)
  end
  self
end

#scalarPuppet::Pops::Types::PScalarType

Sets the type of the binding to the abstract type Scalar.

Returns:



268
269
270
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 268

def scalar()
  type(T.scalar())
end

#stringPuppet::Pops::Types::PStringType

Sets the type of the binding to String.

Returns:



254
255
256
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 254

def string()
  type(T.string())
end

#to(a_literal) ⇒ BindingsBuilder #to(a_class, *args) ⇒ BindingsBuilder #to(a_producer_descriptor) ⇒ BindingsBuilder

Sets the binding’s producer to a singleton producer, if given argument is a value, a literal producer is created for it. To create a producer producing an instance of a class with lazy loading of the class, use #to_instance.

Overloads:

  • #to(a_literal) ⇒ BindingsBuilder

    Sets a constant producer in the binding.

  • #to(a_class, *args) ⇒ BindingsBuilder

    Sets an Instantiating producer (producing an instance of the given class)

  • #to(a_producer_descriptor) ⇒ BindingsBuilder

    Sets the producer from the given producer descriptor

Returns:



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 355

def to(producer, *args)
  case producer
  when Class
    producer = Puppet::Pops::Binder::BindingsFactory.instance_producer(producer.name, *args)
  when Puppet::Pops::Model::Program
    # program is not an expression
    producer = Puppet::Pops::Binder::BindingsFactory.evaluating_producer(producer.body)
  when Puppet::Pops::Model::Expression
    producer = Puppet::Pops::Binder::BindingsFactory.evaluating_producer(producer)
  when Puppet::Pops::Binder::Bindings::ProducerDescriptor
  else
  # If given producer is not a producer, create a literal producer
    producer = Puppet::Pops::Binder::BindingsFactory.literal_producer(producer)
  end
  model.producer = producer
  self
end

#to_first_found(*list_of_lookups) ⇒ Object

Sets the binding’s producer to one that produces the first found lookup of another key

Examples:

binder.bind().name('foo').to_first_found('fee', 'fum', 'extended-bar')
binder.bind().name('foo').to_first_found(
  [T.ruby(ThisClass), 'fee'],
  [T.ruby(ThatClass), 'fum'],
  'extended-bar')

Parameters:

  • list_of_lookups (Array)

    array of arrays [type name], or just name (implies data)



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 534

def to_first_found(*list_of_lookups)
  producers = list_of_lookups.collect do |entry|
    if entry.is_a?(Array)
      case entry.size
      when 2
        Puppet::Pops::Binder::BindingsFactory.lookup_producer(entry[0], entry[1])
      when 1
        Puppet::Pops::Binder::BindingsFactory.lookup_producer(Puppet::Pops::Types::TypeFactory.data(), entry[0])
      else
        raise ArgumentError, "Not an array of [type, name], name, or [name]"
      end
    else
      Puppet::Pops::Binder::BindingsFactory.lookup_producer(T.data(), entry)
    end
  end
  model.producer = Puppet::Pops::Binder::BindingsFactory.first_found_producer(*producers)
  self
end

#to_lookup_of(type, name) ⇒ Object #to_lookup_of(name) ⇒ Object

Sets the binding’s producer to a one that performs a lookup of another key and they applies hash lookup on the result.



519
520
521
522
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 519

def to_hash_lookup_of(type, name, key)
  model.producer = Puppet::Pops::Binder::BindingsFactory.hash_lookup_producer(type, name, key)
  self
end

#to_instance(class_name, *args) ⇒ Object #to_instance(a_class) ⇒ Object

Sets the binding’s producer to a producer of an instance of given class (a String class name, or a Class instance). Use a string class name when lazy loading of the class is wanted.

Overloads:

  • #to_instance(class_name, *args) ⇒ Object

    Parameters:

    • class_name (String)

      the name of the class to instantiate

    • args (Object)

      optional arguments to the constructor

  • #to_instance(a_class) ⇒ Object

    Parameters:

    • a_class (Class)

      the class to instantiate

    • args (Object)

      optional arguments to the constructor



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 383

def to_instance(type, *args)
  class_name = case type
  when Class
    type.name
  when String
    type
  else
    raise ArgumentError, "to_instance accepts String (a class name), or a Class.*args got: #{type.class}."
  end

  # Help by setting the type - since if an to_instance is bound, the type is know. This avoids having
  # to specify the same thing twice.
  self.instance_of(type)
  model.producer = Puppet::Pops::Binder::BindingsFactory.instance_producer(class_name, *args)
end

#to_lookup_of(type, name) ⇒ Object #to_lookup_of(name) ⇒ Object

Sets the binding’s producer to one that performs a lookup of another key



503
504
505
506
507
508
509
510
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 503

def to_lookup_of(type, name=nil)
  unless name
    name = type
    type = Puppet::Pops::Types::TypeFactory.data()
  end
  model.producer = Puppet::Pops::Binder::BindingsFactory.lookup_producer(type, name)
  self
end

#to_producer(a_producer) ⇒ Object #to_producer(a_class, *args) ⇒ Object #to_producer(a_producer_descriptor) ⇒ Object

Sets the binding’s producer to a singleton producer

Overloads:

  • #to_producer(a_producer) ⇒ Object

    Sets the producer to an instantiated producer. The resulting model can not be serialized as a consequence as there is no meta-model describing the specialized producer. Use this only in exceptional cases, or where there is never the need to serialize the model.

    Parameters:

  • #to_producer(a_class, *args) ⇒ Object

    Parameters:

    • a_class (Class)

      the class to create an instance of

    • args (Object)

      the arguments to the given class’ new

  • #to_producer(a_producer_descriptor) ⇒ Object

    Parameters:



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 416

def to_producer(producer, *args)
  case producer
  when Class
    producer = Puppet::Pops::Binder::BindingsFactory.instance_producer(producer.name, *args)
  when Puppet::Pops::Binder::Bindings::ProducerDescriptor
  when Puppet::Pops::Binder::Producers::Producer
    # a custom producer instance
    producer = Puppet::Pops::Binder::BindingsFactory.literal_producer(producer)
  else
    raise ArgumentError, "Given producer argument is none of a producer descriptor, a class, or a producer"
  end
  metaproducer = Puppet::Pops::Binder::BindingsFactory.producer_producer(producer)
  model.producer = metaproducer
  self
end

#to_producer(a_producer) ⇒ Object #to_producer(a_class, *args) ⇒ Object #to_producer(a_producer_descriptor) ⇒ Object

Sets the binding’s producer to a series of producers. Use this when you want to produce a different producer on each request for a producer

Overloads:

  • #to_producer(a_producer) ⇒ Object

    Sets the producer to an instantiated producer. The resulting model can not be serialized as a consequence as there is no meta-model describing the specialized producer. Use this only in exceptional cases, or where there is never the need to serialize the model.

    Parameters:

  • #to_producer(a_class, *args) ⇒ Object

    Parameters:

    • a_class (Class)

      the class to create an instance of

    • args (Object)

      the arguments to the given class’ new

  • #to_producer(a_producer_descriptor) ⇒ Object

    Parameters:



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 451

def to_producer_series(producer, *args)
  case producer
  when Class
    producer = Puppet::Pops::Binder::BindingsFactory.instance_producer(producer.name, *args)
  when Puppet::Pops::Binder::Bindings::ProducerDescriptor
  when Puppet::Pops::Binder::Producers::Producer
    # a custom producer instance
    producer = Puppet::Pops::Binder::BindingsFactory.literal_producer(producer)
  else
    raise ArgumentError, "Given producer argument is none of a producer descriptor, a class, or a producer"
  end
  non_caching = Puppet::Pops::Binder::Bindings::NonCachingProducerDescriptor.new()
  non_caching.producer = producer
  metaproducer = Puppet::Pops::Binder::BindingsFactory.producer_producer(non_caching)

  non_caching = Puppet::Pops::Binder::Bindings::NonCachingProducerDescriptor.new()
  non_caching.producer = metaproducer

  model.producer = non_caching
  self
end

#to_series_of(a_literal) ⇒ Object #to_series_of(a_class, *args) ⇒ Object #to_series_of(a_producer_descriptor) ⇒ Object

Sets the binding’s producer to a “non singleton” producer (each call to produce produces a new instance/copy).

Overloads:

  • #to_series_of(a_literal) ⇒ Object

    a constant producer

  • #to_series_of(a_class, *args) ⇒ Object

    Instantiating producer

  • #to_series_of(a_producer_descriptor) ⇒ Object

    a given producer



483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 483

def to_series_of(producer, *args)
  case producer
  when Class
    producer = Puppet::Pops::Binder::BindingsFactory.instance_producer(producer.name, *args)
  when Puppet::Pops::Binder::Bindings::ProducerDescriptor
  else
  # If given producer is not a producer, create a literal producer
    producer = Puppet::Pops::Binder::BindingsFactory.literal_producer(producer)
  end
  non_caching = Puppet::Pops::Binder::Bindings::NonCachingProducerDescriptor.new()
  non_caching.producer = producer
  model.producer = non_caching
  self
end

#type(type) ⇒ Object

Note:

This is only needed if something other than the default type ‘Data` is wanted, or if the wanted type is not provided by one of the convenience methods #array_of_data, #boolean, #float, #hash_of_data, #integer, #scalar, #pattern, #string, or one of the collection methods #array_of, or #hash_of.

Sets the type of the binding to the given type. To create a type, use the method #type_factory, to obtain the type.

Examples:

creating a Hash with Integer key and Array element type

tc = type_factory
type(tc.hash(tc.array_of(tc.integer), tc.integer)

Parameters:



225
226
227
228
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 225

def type(type)
  model.type = type
  self
end

#type_factoryObject

Note:

The type factory is also available via the constant T.

Provides convenient access to the type factory. This is intended to be used when methods taking a type as argument i.e. #type, #array_of, #hash_of, and #instance_of.



339
340
341
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 339

def type_factory
  Puppet::Pops::Types::TypeFactory
end