Class: Puppet::Pops::Binder::Producers::HashMultibindProducer

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

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) ⇒ HashMultibindProducer

The hash multibind producer provides options to control conflict resolution. By default, the hash is produced using ‘:priority` resolution - the highest entry is selected, the rest are ignored unless they have the same priority which is an error.

Parameters:

Options Hash (options):

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

    a transformer of produced value

  • :conflict_resolution (Symbol, String) — default: :priority

    One of ‘:error`, `:merge`, `:append`, `:priority`, `:ignore` <ul><li> `ignore` the first found highest priority contribution is used, the rest are ignored</li> <li>`error` any duplicate key is an error</li> <li>`append` element type must be compatible with Array, makes elements be arrays and appends all found</li> <li>`merge` element type must be compatible with hash, merges hashes with retention of highest priority hash content</li> <li>`priority` the first found highest priority contribution is used, duplicates with same priority raises and error, the rest are

    ignored.</li></ul>
    
  • :flatten (Boolean, Integer) — default: false

    If appended should be flattened. Also see #flatten.

  • :uniq (Boolean) — default: false

    If appended result should be made unique.



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/puppet/pops/binder/producers.rb', line 699

def initialize(injector, binding, scope, options)
  super
  @conflict_resolution = options[:conflict_resolution].nil? ? :priority : options[:conflict_resolution]
  @uniq = !!options[:uniq]
  @flatten = options[:flatten]

  unless [:error, :merge, :append, :priority, :ignore].include?(@conflict_resolution)
    raise ArgumentError, "Unknown conflict_resolution for Multibind Hash: '#{@conflict_resolution}."
  end

  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

  if uniq || flatten || conflict_resolution.to_s == 'append'
    etype = binding.type.element_type
    unless etype.class == Puppet::Pops::Types::PDataType || etype.is_a?(Puppet::Pops::Types::PArrayType)
      detail = []
      detail << ":uniq" if uniq
      detail << ":flatten" if flatten
      detail << ":conflict_resolution => :append" if conflict_resolution.to_s == 'append'
      raise ArgumentError, ["Options #{detail.join(', and ')} cannot be used with a Multibind ",
        "of type #{injector.type_calculator.string(binding.type)}"].join()
    end
  end
end

Instance Attribute Details

#conflict_resolutionSymbol (readonly)

Returns One of ‘:error`, `:merge`, `:append`, `:priority`, `:ignore`.

Returns:

  • (Symbol)

    One of ‘:error`, `:merge`, `:append`, `:priority`, `:ignore`



669
670
671
# File 'lib/puppet/pops/binder/producers.rb', line 669

def conflict_resolution
  @conflict_resolution
end

#flattenBoolean, Integer (readonly)

Returns Flatten all if true, or none if false, or to given level (0 = none, -1 = all).

Returns:

  • (Boolean, Integer)

    Flatten all if true, or none if false, or to given level (0 = none, -1 = all)



677
678
679
# File 'lib/puppet/pops/binder/producers.rb', line 677

def flatten
  @flatten
end

#uniqBoolean (readonly)

Returns:

  • (Boolean)


673
674
675
# File 'lib/puppet/pops/binder/producers.rb', line 673

def uniq
  @uniq
end