Class: Puppet::Pops::Binder::BindingsFactory::BindingsBuilder
- Inherits:
-
AbstractBuilder
- Object
- AbstractBuilder
- Puppet::Pops::Binder::BindingsFactory::BindingsBuilder
- Defined in:
- lib/puppet/pops/binder/bindings_factory.rb
Overview
Builds a Binding via convenience methods.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from AbstractBuilder
Instance Method Summary collapse
-
#abstract ⇒ Object
Sets the binding to be abstract (it must be overridden).
-
#array_of(t) ⇒ Types::PArrayType
Sets the type of the binding to Array, where T is given.
-
#array_of_data ⇒ Types::PArrayType
Sets the type of the binding to Array.
-
#boolean ⇒ Types::PBooleanType
Sets the type of the binding to Boolean.
-
#data ⇒ Types::PDataType
Sets the type of the binding to the abstract type Data.
-
#final ⇒ Object
Sets the binding to be final (it may not be overridden).
-
#float ⇒ Types::PFloatType
Sets the type of the binding to Float.
-
#hash_of(t) ⇒ Types::PHashType
Sets type of the binding to ‘Hash[Literal, t]`.
-
#hash_of_data ⇒ Types::PHashType
Sets the type of the binding to Hash[Literal, Data].
-
#in_multibind(id) ⇒ Object
Makes the binding a multibind contribution to the given multibind id.
-
#initialize(binding) ⇒ BindingsBuilder
constructor
A new instance of BindingsBuilder.
-
#instance_of(t) ⇒ Object
Sets the type of the binding based on the given argument.
-
#integer ⇒ Types::PIntegerType
Sets the type of the binding to Integer.
-
#name(name) ⇒ Object
(also: #named)
Sets the name of the binding.
-
#override ⇒ Object
Sets the binding to be override (it must override something).
-
#pattern ⇒ Types::PRegexpType
Sets the type of the binding to Pattern.
-
#producer_options(options) ⇒ Object
Sets options to the producer.
-
#scalar ⇒ Types::PScalarType
Sets the type of the binding to the abstract type Scalar.
-
#string ⇒ Types::PStringType
Sets the type of the binding to String.
-
#to(producer, *args) ⇒ BindingsBuilder
Sets the binding’s producer to a singleton producer, if given argument is a value, a literal producer is created for it.
-
#to_first_found(*list_of_lookups) ⇒ Object
Sets the binding’s producer to one that produces the first found lookup of another key.
-
#to_hash_lookup_of(type, name, key) ⇒ Object
Sets the binding’s producer to a one that performs a lookup of another key and they applies hash lookup on the result.
-
#to_instance(type, *args) ⇒ Object
Sets the binding’s producer to a producer of an instance of given class (a String class name, or a Class instance).
-
#to_lookup_of(type, name = nil) ⇒ Object
Sets the binding’s producer to one that performs a lookup of another key.
-
#to_producer(producer, *args) ⇒ Object
Sets the binding’s producer to a singleton producer.
-
#to_producer_series(producer, *args) ⇒ Object
Sets the binding’s producer to a series of producers.
-
#to_series_of(producer, *args) ⇒ Object
Sets the binding’s producer to a “non singleton” producer (each call to produce produces a new instance/copy).
-
#type(type) ⇒ Object
Sets the type of the binding to the given type.
-
#type_factory ⇒ Object
Provides convenient access to the type factory.
Methods inherited from AbstractBuilder
Constructor Details
#initialize(binding) ⇒ BindingsBuilder
Returns a new instance of BindingsBuilder.
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
#abstract ⇒ Object
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.
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_data ⇒ Types::PArrayType
Sets the type of the binding to Array.
284 285 286 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 284 def array_of_data() type(T.array_of_data()) end |
#boolean ⇒ Types::PBooleanType
Sets the type of the binding to Boolean.
249 250 251 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 249 def boolean() type(T.boolean()) end |
#data ⇒ Types::PDataType
Sets the type of the binding to the abstract type Data.
277 278 279 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 277 def data() type(T.data()) end |
#final ⇒ Object
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 |
#float ⇒ Types::PFloatType
Sets the type of the binding to Float.
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)`.
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_data ⇒ Types::PHashType
Sets the type of the binding to Hash[Literal, Data].
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
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.
332 333 334 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 332 def instance_of(t) type(T.type_of(t)) end |
#integer ⇒ Types::PIntegerType
Sets the type of the binding to Integer.
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.
176 177 178 179 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 176 def name(name) model.name = name self end |
#override ⇒ Object
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 |
#pattern ⇒ Types::PRegexpType
Sets the type of the binding to Pattern.
263 264 265 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 263 def pattern() type(T.pattern()) end |
#producer_options(options) ⇒ Object
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 () .each do |k, v| arg = Bindings::NamedArgument.new() arg.name = k.to_s arg.value = v model.addProducer_args(arg) end self end |
#scalar ⇒ Types::PScalarType
Sets the type of the binding to the abstract type Scalar.
270 271 272 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 270 def scalar() type(T.scalar()) end |
#string ⇒ Types::PStringType
Sets the type of the binding to String.
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.
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
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.
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
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 = BindingsFactory.producer_producer(producer) model.producer = 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
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 = BindingsFactory.producer_producer(non_caching) non_caching = Bindings::NonCachingProducerDescriptor.new() non_caching.producer = 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).
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
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.
227 228 229 230 |
# File 'lib/puppet/pops/binder/bindings_factory.rb', line 227 def type(type) model.type = type self end |
#type_factory ⇒ Object
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 |