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:



168
169
170
171
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 168

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)



187
188
189
190
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 187

def abstract
  model.abstract = true
  self
end

#array_of(t) ⇒ Types::PArrayType

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

Parameters:

Returns:



292
293
294
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 292

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

#array_of_dataTypes::PArrayType

Sets the type of the binding to Array.

Returns:



284
285
286
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 284

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

#booleanTypes::PBooleanType

Sets the type of the binding to Boolean.

Returns:



249
250
251
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 249

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

#dataTypes::PDataType

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

Returns:



277
278
279
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 277

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

#finalObject

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



201
202
203
204
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 201

def final
  model.final = true
  self
end

#floatTypes::PFloatType

Sets the type of the binding to Float.

Returns:



242
243
244
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 242

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

#hash_of(t) ⇒ 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:



308
309
310
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 308

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

#hash_of_dataTypes::PHashType

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

Returns:



299
300
301
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 299

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



209
210
211
212
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 209

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



332
333
334
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 332

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

#integerTypes::PIntegerType

Sets the type of the binding to Integer.

Returns:



235
236
237
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 235

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.



176
177
178
179
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 176

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

#overrideObject

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



194
195
196
197
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 194

def override
  model.override = true
  self
end

#patternTypes::PRegexpType

Sets the type of the binding to Pattern.

Returns:



263
264
265
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 263

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.



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

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

#scalarTypes::PScalarType

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

Returns:



270
271
272
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 270

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

#stringTypes::PStringType

Sets the type of the binding to String.

Returns:



256
257
258
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 256

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:



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

def to(producer, *args)
  case producer
  when Class
    producer = BindingsFactory.instance_producer(producer.name, *args)
  when Model::Program
    # program is not an expression
    producer = BindingsFactory.evaluating_producer(producer.body)
  when Model::Expression
    producer = BindingsFactory.evaluating_producer(producer)
  when Bindings::ProducerDescriptor
  else
  # If given producer is not a producer, create a literal producer
    producer = 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)



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

def to_first_found(*list_of_lookups)
  producers = list_of_lookups.collect do |entry|
    if entry.is_a?(Array)
      case entry.size
      when 2
        BindingsFactory.lookup_producer(entry[0], entry[1])
      when 1
        BindingsFactory.lookup_producer(Types::TypeFactory.data(), entry[0])
      else
        raise ArgumentError, "Not an array of [type, name], name, or [name]"
      end
    else
      BindingsFactory.lookup_producer(T.data(), entry)
    end
  end
  model.producer = 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.



521
522
523
524
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 521

def to_hash_lookup_of(type, name, key)
  model.producer = 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



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

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 = 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



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

def to_lookup_of(type, name=nil)
  unless name
    name = type
    type = Types::TypeFactory.data()
  end
  model.producer = 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:



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

def to_producer(producer, *args)
  case producer
  when Class
    producer = BindingsFactory.instance_producer(producer.name, *args)
  when Bindings::ProducerDescriptor
  when Producers::Producer
    # a custom producer instance
    producer = BindingsFactory.literal_producer(producer)
  else
    raise ArgumentError, "Given producer argument is none of a producer descriptor, a class, or a producer"
  end
  metaproducer = 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:



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

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

  non_caching = 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



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

def to_series_of(producer, *args)
  case producer
  when Class
    producer = BindingsFactory.instance_producer(producer.name, *args)
  when Bindings::ProducerDescriptor
  else
  # If given producer is not a producer, create a literal producer
    producer = BindingsFactory.literal_producer(producer)
  end
  non_caching = 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:



227
228
229
230
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 227

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.



341
342
343
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 341

def type_factory
  Types::TypeFactory
end