Class: Puppet::Pops::Binder::Producers::ArrayMultibindProducer

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

Overview

Note:

Collection accepts elements that comply with the array’s element type, or the entire type (i.e. Array). If the type is restrictive - e.g. Array and an Array is contributed, the result will not be type compliant without also using the ‘:flatten` option, and a type error will be raised. For an array with relaxed typing i.e. Array, it is valid to produce a result such as `[’a’, [‘b’, ‘c’], ‘d’]‘ and no flattening is required and no error is raised (but using the array needs to be aware of potential array, non-array entries. The use of the option `:flatten` controls how the result is flattened.

A configurable multibind producer for Array type multibindings.

This implementation collects all contributions to the multibind and then combines them using the following rules:

  • all unnamed entries are added unless the option ‘:priority_on_unnamed` is set to true, in which case the unnamed contribution with the highest priority is added, and the rest are ignored (unless they have the same priority in which case an error is raised).

  • all named entries are handled the same way as unnamed but the option ‘:priority_on_named` controls their handling.

  • the option ‘:uniq` post processes the result to only contain unique entries

  • the option ‘:flatten` post processes the result by flattening all nested arrays.

  • If both ‘:flatten` and `:uniq` are true, flattening is done first.

Instance Attribute Summary collapse

Attributes inherited from MultibindProducer

#contributions_key

Attributes inherited from AbstractArgumentedProducer

#binding, #injector

Attributes inherited from Producer

#transformer

Instance Method Summary collapse

Methods inherited from MultibindProducer

#type_error_detail

Methods inherited from Producer

#produce, #producer

Constructor Details

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

Returns a new instance of ArrayMultibindProducer.

Parameters:

Options Hash (options):

  • :transformer (Puppet::Pops::Model::LambdaExpression) — default: nil

    a transformer of produced value

  • :uniq (Boolean) — default: false

    if collected result should be post-processed to contain only unique entries

  • :flatten (Boolean, Integer) — default: false

    if collected result should be post-processed so all contained arrays are flattened. May be set to an Integer value to indicate the level of recursion (-1 is endless, 0 is none).

  • :priority_on_named (Boolean) — default: true

    if highest precedented named element should win or if all should be included

  • :priority_on_unnamed (Boolean) — default: false

    if highest precedented unnamed element should win or if all should be included



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/puppet/pops/binder/producers.rb', line 592

def initialize(injector, binding, scope, options)
  super
  @uniq = !!options[:uniq]
  @flatten = options[:flatten]
  @priority_on_named = options[:priority_on_named].nil? ? true : options[:priority_on_name]
  @priority_on_unnamed = !!options[:priority_on_unnamed]

  case @flatten
  when Integer
  when true
    @flatten = -1
  when false
    @flatten = nil
  when NilClass
    @flatten = nil
  else
    raise ArgumentError, "Option :flatten must be nil, Boolean, or an integer value" unless @flatten.is_a?(Integer)
  end
end

Instance Attribute Details

#flattenBoolean, Integer (readonly)

Returns If result should be flattened (true), or not (false), or flattened to given level (0 = none, -1 = all).

Returns:

  • (Boolean, Integer)

    If result should be flattened (true), or not (false), or flattened to given level (0 = none, -1 = all)



571
572
573
# File 'lib/puppet/pops/binder/producers.rb', line 571

def flatten
  @flatten
end

#priority_on_namedBoolean (readonly)

Returns whether priority should be considered for named contributions.

Returns:

  • (Boolean)

    whether priority should be considered for named contributions



575
576
577
# File 'lib/puppet/pops/binder/producers.rb', line 575

def priority_on_named
  @priority_on_named
end

#priority_on_unnamedBoolean (readonly)

Returns whether priority should be considered for unnamed contributions.

Returns:

  • (Boolean)

    whether priority should be considered for unnamed contributions



579
580
581
# File 'lib/puppet/pops/binder/producers.rb', line 579

def priority_on_unnamed
  @priority_on_unnamed
end

#uniqBoolean (readonly)

Returns whether the result should be made contain unique (non-equal) entries or not.

Returns:

  • (Boolean)

    whether the result should be made contain unique (non-equal) entries or not



567
568
569
# File 'lib/puppet/pops/binder/producers.rb', line 567

def uniq
  @uniq
end