Method: Puppet::Pops::Types::StringConverter#string_PArrayType

Defined in:
lib/puppet/pops/types/string_converter.rb

#string_PArrayType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/puppet/pops/types/string_converter.rb', line 789

def string_PArrayType(val_type, val, format_map, indentation)
  format         = get_format(val_type, format_map)
  sep            = format.separator || DEFAULT_ARRAY_FORMAT.separator
  string_formats = format.container_string_formats || DEFAULT_CONTAINER_FORMATS
  delims         = format.delimiter_pair(DEFAULT_ARRAY_DELIMITERS)

  # Make indentation active, if array is in alternative format, or if nested in indenting
  indentation = indentation.indenting(format.alt? || indentation.is_indenting?)

  case format.format
  when :a, :s, :p
    buf = ''
    if indentation.breaks?
      buf << "\n"
      buf << indentation.padding
    end
    buf << delims[0]

    # Make a first pass to format each element
    children_indentation = indentation.increase(format.alt?) # tell children they are expected to indent
    mapped = val.map do |v|
      if children_indentation.first?
        children_indentation = children_indentation.subsequent
      end
      val_t = TypeCalculator.infer_set(v)
      _convert(val_t, v, is_container?(val_t) ? format_map : string_formats, children_indentation)
    end

    # compute widest run in the array, skip nested arrays and hashes
    # then if size > width, set flag if a break on each element should be performed
    if format.alt? && format.width
      widest = val.each_with_index.reduce([0]) do | memo, v_i |
        # array or hash breaks
        if is_a_or_h?(v_i[0])
          memo << 0
        else
          memo[-1] += mapped[v_i[1]].length
        end
        memo
      end
      widest = widest.max
      sz_break = widest > (format.width || Float::INFINITY)
    else
      sz_break = false
    end

    # output each element with breaks and padding
    children_indentation = indentation.increase(format.alt?)
    val.each_with_index do |v, i|
      str_val = mapped[i]
      if children_indentation.first?
        children_indentation = children_indentation.subsequent
        # if breaking, indent first element by one
        if sz_break && !is_a_or_h?(v)
          buf << ' '
        end
      else
        buf << sep
        # if break on each (and breaking will not occur because next is an array or hash)
        # or, if indenting, and previous was an array or hash, then break and continue on next line
        # indented.
        if (sz_break && !is_a_or_h?(v)) || (format.alt? && i > 0 && is_a_or_h?(val[i-1]) && !is_a_or_h?(v))
          buf << "\n"
          buf << children_indentation.padding
        elsif !(format.alt? && is_a_or_h?(v))
          buf << ' '
        end
      end
      buf << str_val
    end
    buf << delims[1]
    buf
  else
    raise FormatError.new('Array', format.format, 'asp')
  end
end