Method: Puppet::Pops::Types::PObjectType#parameter_info

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

#parameter_info(impl_class) ⇒ (Array<String>, Array<PAnyType>, Integer)

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.

Returns array of parameter names, array of parameter types, and a count reflecting the required number of parameters.

Returns:

  • ((Array<String>, Array<PAnyType>, Integer))

    array of parameter names, array of parameter types, and a count reflecting the required number of parameters



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/puppet/pops/types/p_object_type.rb', line 610

def parameter_info(impl_class)
  # Create a types and a names array where optional entries ends up last
  @parameter_info ||= {}
  pic = @parameter_info[impl_class]
  return pic if pic

  opt_types = []
  opt_names = []
  non_opt_types = []
  non_opt_names = []
  init_hash_type.elements.each do |se|
    if se.key_type.is_a?(POptionalType)
      opt_names << se.name
      opt_types << se.value_type
    else
      non_opt_names << se.name
      non_opt_types << se.value_type
    end
  end
  param_names = non_opt_names + opt_names
  param_types = non_opt_types + opt_types
  param_count = param_names.size

  init = impl_class.respond_to?(:from_asserted_args) ? impl_class.method(:from_asserted_args) : impl_class.instance_method(:initialize)
  init_non_opt_count = 0
  init_param_names = init.parameters.map do |p|
    init_non_opt_count += 1 if :req == p[0]
    n = p[1].to_s
    r = RubyGenerator.unprotect_reserved_name(n)
    unless r.equal?(n)
      # assert that the protected name wasn't a real name (names can start with underscore)
      n = r unless param_names.index(r).nil?
    end
    n
  end

  if init_param_names != param_names
    if init_param_names.size < param_count || init_non_opt_count > param_count
      raise Serialization::SerializationError, "Initializer for class #{impl_class.name} does not match the attributes of #{name}"
    end

    init_param_names = init_param_names[0, param_count] if init_param_names.size > param_count
    unless init_param_names == param_names
      # Reorder needed to match initialize method arguments
      new_param_types = []
      init_param_names.each do |ip|
        index = param_names.index(ip)
        if index.nil?
          raise Serialization::SerializationError,
                "Initializer for class #{impl_class.name} parameter '#{ip}' does not match any of the attributes of type #{name}"
        end
        new_param_types << param_types[index]
      end
      param_names = init_param_names
      param_types = new_param_types
    end
  end

  pic = [param_names.freeze, param_types.freeze, non_opt_types.size].freeze
  @parameter_info[impl_class] = pic
  pic
end