Class: Puppet::Pops::Types::PObjectType

Inherits:
PMetaType show all
Includes:
TypeWithMembers
Defined in:
lib/puppet/pops/types/p_object_type.rb

Defined Under Namespace

Classes: PAnnotatedMember, PAttribute, PFunction, PTypeParameter

Constant Summary collapse

ATTRIBUTE_KIND_CONSTANT =
'constant'
ATTRIBUTE_KIND_DERIVED =
'derived'
ATTRIBUTE_KIND_GIVEN_OR_DERIVED =
'given_or_derived'
ATTRIBUTE_KIND_REFERENCE =
'reference'
TYPE_ATTRIBUTE_KIND =
TypeFactory.enum(ATTRIBUTE_KIND_CONSTANT, ATTRIBUTE_KIND_DERIVED, ATTRIBUTE_KIND_GIVEN_OR_DERIVED, ATTRIBUTE_KIND_REFERENCE)
TYPE_OBJECT_NAME =
Pcore::TYPE_QUALIFIED_REFERENCE
TYPE_ATTRIBUTE =
TypeFactory
    .struct({
  KEY_TYPE => PTypeType::DEFAULT,
  TypeFactory.optional(KEY_FINAL) => PBooleanType::DEFAULT,
  TypeFactory.optional(KEY_OVERRIDE) => PBooleanType::DEFAULT,
  TypeFactory.optional(KEY_KIND) => TYPE_ATTRIBUTE_KIND,
  KEY_VALUE => PAnyType::DEFAULT,
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS
})
TYPE_PARAMETER =
TypeFactory
    .struct({
  KEY_TYPE => PTypeType::DEFAULT,
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS
})
TYPE_CONSTANTS =
TypeFactory.hash_kv(Pcore::TYPE_MEMBER_NAME, PAnyType::DEFAULT)
TYPE_ATTRIBUTES =
TypeFactory.hash_kv(Pcore::TYPE_MEMBER_NAME, TypeFactory.not_undef)
TYPE_PARAMETERS =
TypeFactory.hash_kv(Pcore::TYPE_MEMBER_NAME, TypeFactory.not_undef)
TYPE_ATTRIBUTE_CALLABLE =
TypeFactory.callable(0, 0)
TYPE_FUNCTION_TYPE =
PTypeType.new(PCallableType::DEFAULT)
TYPE_FUNCTION =
TypeFactory
    .struct({
  KEY_TYPE => TYPE_FUNCTION_TYPE,
  TypeFactory.optional(KEY_FINAL) => PBooleanType::DEFAULT,
  TypeFactory.optional(KEY_OVERRIDE) => PBooleanType::DEFAULT,
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS
})
TYPE_FUNCTIONS =
TypeFactory.hash_kv(PVariantType.new([Pcore::TYPE_MEMBER_NAME, PStringType.new('[]')]), TypeFactory.not_undef)
TYPE_EQUALITY =
TypeFactory.variant(Pcore::TYPE_MEMBER_NAME, TypeFactory.array_of(Pcore::TYPE_MEMBER_NAME))
TYPE_CHECKS =

TBD

PAnyType::DEFAULT
TYPE_OBJECT_I12N =
TypeFactory
    .struct({
  TypeFactory.optional(KEY_NAME) => TYPE_OBJECT_NAME,
  TypeFactory.optional(KEY_PARENT) => PTypeType::DEFAULT,
  TypeFactory.optional(KEY_TYPE_PARAMETERS) => TYPE_PARAMETERS,
  TypeFactory.optional(KEY_ATTRIBUTES) => TYPE_ATTRIBUTES,
  TypeFactory.optional(KEY_CONSTANTS) => TYPE_CONSTANTS,
  TypeFactory.optional(KEY_FUNCTIONS) => TYPE_FUNCTIONS,
  TypeFactory.optional(KEY_EQUALITY) => TYPE_EQUALITY,
  TypeFactory.optional(KEY_EQUALITY_INCLUDE_TYPE) => PBooleanType::DEFAULT,
  TypeFactory.optional(KEY_CHECKS) => TYPE_CHECKS,
  TypeFactory.optional(KEY_ANNOTATIONS) => TYPE_ANNOTATIONS
})
DEFAULT =
PObjectType.new(EMPTY_HASH)

Constants included from Annotatable

Annotatable::TYPE_ANNOTATIONS

Instance Attribute Summary collapse

Attributes inherited from PMetaType

#loader

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PMetaType

#resolve, #resolve_hash, #resolve_literal_hash, #resolve_type_refs, #resolved?, #to_s

Methods included from Annotatable

#annotatable_accept, #init_annotatable

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_with?, create, #generalize, #loader, new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_type, #to_s

Constructor Details

#initialize(name, init_hash_expression) ⇒ PObjectType #initialize(init_hash) ⇒ PObjectType

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.

Initialize an Object Type instance. The initialization will use either a name and an initialization hash expression, or a fully resolved initialization hash.

Overloads:

  • #initialize(name, init_hash_expression) ⇒ PObjectType

    Used when the Object type is loaded using a type alias expression. When that happens, it is important that the actual resolution of the expression is deferred until all definitions have been made known to the current loader. The object will then be resolved when it is loaded by the TypeParser. “resolved” here, means that the hash expression is fully resolved, and then passed to the #_pcore_init_from_hash method.

    Parameters:

    • name (String)

      The name of the object

    • init_hash_expression (Model::LiteralHash)

      The hash describing the Object features

  • #initialize(init_hash) ⇒ PObjectType

    Used when the object is created by the TypeFactory. The init_hash must be fully resolved.

    Parameters:

    • _pcore_init_hash (Hash{String=>Object})

      The hash describing the Object features

    • loader (Loaders::Loader, nil)

      the loader that loaded the type



441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/puppet/pops/types/p_object_type.rb', line 441

def initialize(_pcore_init_hash, init_hash_expression = nil) # rubocop:disable Lint/UnderscorePrefixedVariableName
  if _pcore_init_hash.is_a?(Hash)
    _pcore_init_from_hash(_pcore_init_hash)
    @loader = init_hash_expression unless init_hash_expression.nil?
  else
    @type_parameters = EMPTY_HASH
    @attributes = EMPTY_HASH
    @functions = EMPTY_HASH
    @name = TypeAsserter.assert_instance_of('object name', TYPE_OBJECT_NAME, _pcore_init_hash)
    @init_hash_expression = init_hash_expression
  end
end

Instance Attribute Details

#annotationsObject (readonly)



422
423
424
# File 'lib/puppet/pops/types/p_object_type.rb', line 422

def annotations
  @annotations
end

#checksObject (readonly)



421
422
423
# File 'lib/puppet/pops/types/p_object_type.rb', line 421

def checks
  @checks
end

#equalityObject (readonly)



420
421
422
# File 'lib/puppet/pops/types/p_object_type.rb', line 420

def equality
  @equality
end

#nameObject (readonly)



418
419
420
# File 'lib/puppet/pops/types/p_object_type.rb', line 418

def name
  @name
end

#parentObject (readonly)



419
420
421
# File 'lib/puppet/pops/types/p_object_type.rb', line 419

def parent
  @parent
end

Class Method Details

.from_hash(hash) ⇒ Object



682
683
684
# File 'lib/puppet/pops/types/p_object_type.rb', line 682

def self.from_hash(hash)
  new(hash, nil)
end

.register_ptype(loader, ir) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet/pops/types/p_object_type.rb', line 87

def self.register_ptype(loader, ir)
  type = create_ptype(loader, ir, 'AnyType', '_pcore_init_hash' => TYPE_OBJECT_I12N)

  # Now, when the Object type exists, add annotations with keys derived from Annotation and freeze the types.
  annotations = TypeFactory.optional(PHashType.new(PTypeType.new(Annotation._pcore_type), TypeFactory.hash_kv(Pcore::TYPE_MEMBER_NAME, PAnyType::DEFAULT)))
  TYPE_ATTRIBUTE.hashed_elements[KEY_ANNOTATIONS].replace_value_type(annotations)
  TYPE_FUNCTION.hashed_elements[KEY_ANNOTATIONS].replace_value_type(annotations)
  TYPE_OBJECT_I12N.hashed_elements[KEY_ANNOTATIONS].replace_value_type(annotations)
  PTypeSetType::TYPE_TYPESET_I12N.hashed_elements[KEY_ANNOTATIONS].replace_value_type(annotations)
  PTypeSetType::TYPE_TYPE_REFERENCE_I12N.hashed_elements[KEY_ANNOTATIONS].replace_value_type(annotations)
  type
end

Instance Method Details

#[](name) ⇒ Object



828
829
830
831
832
833
834
835
# File 'lib/puppet/pops/types/p_object_type.rb', line 828

def [](name)
  member = @attributes[name] || @functions[name]
  if member.nil?
    rp = resolved_parent
    member = rp[name] if rp.is_a?(PObjectType)
  end
  member
end

#_pcore_init_from_hash(init_hash) ⇒ 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.



687
688
689
690
691
692
693
694
695
696
697
698
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
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
# File 'lib/puppet/pops/types/p_object_type.rb', line 687

def _pcore_init_from_hash(init_hash)
  TypeAsserter.assert_instance_of('object initializer', TYPE_OBJECT_I12N, init_hash)
  @type_parameters = EMPTY_HASH
  @attributes = EMPTY_HASH
  @functions = EMPTY_HASH

  # Name given to the loader have higher precedence than a name declared in the type
  @name ||= init_hash[KEY_NAME]
  @name.freeze unless @name.nil?

  @parent = init_hash[KEY_PARENT]

  parent_members = EMPTY_HASH
  parent_type_params = EMPTY_HASH
  parent_object_type = nil
  unless @parent.nil?
    check_self_recursion(self)
    rp = resolved_parent
    raise Puppet::ParseError, _("reference to unresolved type '%{name}'") % { :name => rp.type_string } if rp.is_a?(PTypeReferenceType)

    if rp.is_a?(PObjectType)
      parent_object_type = rp
      parent_members = rp.members(true)
      parent_type_params = rp.type_parameters(true)
    end
  end

  type_parameters = init_hash[KEY_TYPE_PARAMETERS]
  unless type_parameters.nil? || type_parameters.empty?
    @type_parameters = {}
    type_parameters.each do |key, param_spec|
      param_value = :undef
      if param_spec.is_a?(Hash)
        param_type = param_spec[KEY_TYPE]
        param_value = param_spec[KEY_VALUE] if param_spec.include?(KEY_VALUE)
      else
        param_type = TypeAsserter.assert_instance_of(nil, PTypeType::DEFAULT, param_spec) { "type_parameter #{label}[#{key}]" }
      end
      param_type = POptionalType.new(param_type) unless param_type.is_a?(POptionalType)
      type_param = PTypeParameter.new(key, self, KEY_TYPE => param_type, KEY_VALUE => param_value).assert_override(parent_type_params)
      @type_parameters[key] = type_param
    end
  end

  constants = init_hash[KEY_CONSTANTS]
  attr_specs = init_hash[KEY_ATTRIBUTES]
  if attr_specs.nil?
    attr_specs = {}
  else
    # attr_specs might be frozen
    attr_specs = attr_specs.to_h
  end
  unless constants.nil? || constants.empty?
    constants.each do |key, value|
      if attr_specs.include?(key)
        raise Puppet::ParseError, _("attribute %{label}[%{key}] is defined as both a constant and an attribute") % { label: label, key: key }
      end

      attr_spec = {
        # Type must be generic here, or overrides would become impossible
        KEY_TYPE => TypeCalculator.infer(value).generalize,
        KEY_VALUE => value,
        KEY_KIND => ATTRIBUTE_KIND_CONSTANT
      }
      # Indicate override if parent member exists. Type check etc. will take place later on.
      attr_spec[KEY_OVERRIDE] = parent_members.include?(key)
      attr_specs[key] = attr_spec
    end
  end

  unless attr_specs.empty?
    @attributes = attr_specs.to_h do |key, attr_spec|
      unless attr_spec.is_a?(Hash)
        attr_type = TypeAsserter.assert_instance_of(nil, PTypeType::DEFAULT, attr_spec) { "attribute #{label}[#{key}]" }
        attr_spec = { KEY_TYPE => attr_type }
        attr_spec[KEY_VALUE] = nil if attr_type.is_a?(POptionalType)
      end
      attr = PAttribute.new(key, self, attr_spec)
      [attr.name, attr.assert_override(parent_members)]
    end.freeze
  end

  func_specs = init_hash[KEY_FUNCTIONS]
  unless func_specs.nil? || func_specs.empty?
    @functions = func_specs.to_h do |key, func_spec|
      func_spec = { KEY_TYPE => TypeAsserter.assert_instance_of(nil, TYPE_FUNCTION_TYPE, func_spec) { "function #{label}[#{key}]" } } unless func_spec.is_a?(Hash)
      func = PFunction.new(key, self, func_spec)
      name = func.name
      raise Puppet::ParseError, _("%{label} conflicts with attribute with the same name") % { label: func.label } if @attributes.include?(name)

      [name, func.assert_override(parent_members)]
    end.freeze
  end

  @equality_include_type = init_hash[KEY_EQUALITY_INCLUDE_TYPE]
  @equality_include_type = true if @equality_include_type.nil?

  equality = init_hash[KEY_EQUALITY]
  equality = [equality] if equality.is_a?(String)
  if equality.is_a?(Array)
    unless equality.empty?
      # TRANSLATORS equality_include_type = false should not be translated
      raise Puppet::ParseError, _('equality_include_type = false cannot be combined with non empty equality specification') unless @equality_include_type

      parent_eq_attrs = nil
      equality.each do |attr_name|
        attr = parent_members[attr_name]
        if attr.nil?
          attr = @attributes[attr_name] || @functions[attr_name]
        elsif attr.is_a?(PAttribute)
          # Assert that attribute is not already include by parent equality
          parent_eq_attrs ||= parent_object_type.equality_attributes
          if parent_eq_attrs.include?(attr_name)
            including_parent = find_equality_definer_of(attr)
            raise Puppet::ParseError, _("%{label} equality is referencing %{attribute} which is included in equality of %{including_parent}") %
                                      { label: label, attribute: attr.label, including_parent: including_parent.label }
          end
        end

        unless attr.is_a?(PAttribute)
          if attr.nil?
            raise Puppet::ParseError, _("%{label} equality is referencing non existent attribute '%{attribute}'") % { label: label, attribute: attr_name }
          end

          raise Puppet::ParseError, _("%{label} equality is referencing %{attribute}. Only attribute references are allowed") %
                                    { label: label, attribute: attr.label }
        end
        if attr.kind == ATTRIBUTE_KIND_CONSTANT
          raise Puppet::ParseError, _("%{label} equality is referencing constant %{attribute}.") % { label: label, attribute: attr.label } + ' ' +
                                    _("Reference to constant is not allowed in equality")
        end
      end
    end
    equality.freeze
  end
  @equality = equality

  @checks = init_hash[KEY_CHECKS]
  init_annotatable(init_hash)
end

#_pcore_init_hash(include_name = true) ⇒ Hash{String=>Object}

The init_hash is primarily intended for serialization and string representation purposes. It creates a hash suitable for passing to PObjectType#new(init_hash)

Returns:

  • (Hash{String=>Object})

    the features hash



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/puppet/pops/types/p_object_type.rb', line 894

def _pcore_init_hash(include_name = true)
  result = super()
  result[KEY_NAME] = @name if include_name && !@name.nil?
  result[KEY_PARENT] = @parent unless @parent.nil?
  result[KEY_TYPE_PARAMETERS] = compressed_members_hash(@type_parameters) unless @type_parameters.empty?
  unless @attributes.empty?
    # Divide attributes into constants and others
    tc = TypeCalculator.singleton
    constants, others = @attributes.partition do |_, a|
      a.kind == ATTRIBUTE_KIND_CONSTANT && a.type == tc.infer(a.value).generalize
    end.map(&:to_h)

    result[KEY_ATTRIBUTES] = compressed_members_hash(others) unless others.empty?
    unless constants.empty?
      # { kind => 'constant', type => <type of value>, value => <value> } becomes just <value>
      constants.each_pair { |key, a| constants[key] = a.value }
      result[KEY_CONSTANTS] = constants
    end
  end
  result[KEY_FUNCTIONS] = compressed_members_hash(@functions) unless @functions.empty?
  result[KEY_EQUALITY] = @equality unless @equality.nil?
  result[KEY_CHECKS] = @checks unless @checks.nil?
  result
end

#accept(visitor, guard) ⇒ Object



837
838
839
840
841
842
843
844
845
# File 'lib/puppet/pops/types/p_object_type.rb', line 837

def accept(visitor, guard)
  guarded_recursion(guard, nil) do |g|
    super(visitor, g)
    @parent.accept(visitor, g) unless parent.nil?
    @type_parameters.values.each { |p| p.accept(visitor, g) }
    @attributes.values.each { |a| a.accept(visitor, g) }
    @functions.values.each { |f| f.accept(visitor, g) }
  end
end

#allocateObject



859
860
861
# File 'lib/puppet/pops/types/p_object_type.rb', line 859

def allocate
  implementation_class.allocate
end

#attr_reader_name(se) ⇒ 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.



674
675
676
677
678
679
680
# File 'lib/puppet/pops/types/p_object_type.rb', line 674

def attr_reader_name(se)
  if se.value_type.is_a?(PBooleanType) || se.value_type.is_a?(POptionalType) && se.value_type.type.is_a?(PBooleanType)
    "#{se.name}?"
  else
    se.name
  end
end

#attributes(include_parent = false) ⇒ Hash{String=>PAttribute}

Returns the attributes of this ‘Object` type. If include_parent is `true`, then all inherited attributes will be included in the returned `Hash`.

Parameters:

  • include_parent (Boolean) (defaults to: false)

    ‘true` if inherited attributes should be included

Returns:

  • (Hash{String=>PAttribute})

    a hash with the attributes



970
971
972
# File 'lib/puppet/pops/types/p_object_type.rb', line 970

def attributes(include_parent = false)
  get_members(include_parent, :attributes)
end

#callable_args?(callable, guard) ⇒ Boolean

Returns:

  • (Boolean)


847
848
849
# File 'lib/puppet/pops/types/p_object_type.rb', line 847

def callable_args?(callable, guard)
  @parent.nil? ? false : @parent.callable_args?(callable, guard)
end

#check_self_recursion(originator) ⇒ 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.

Assert that this type does not inherit from itself



1003
1004
1005
1006
1007
1008
1009
# File 'lib/puppet/pops/types/p_object_type.rb', line 1003

def check_self_recursion(originator)
  unless @parent.nil?
    raise Puppet::Error, "The Object type '#{originator.label}' inherits from itself" if @parent.equal?(originator)

    @parent.check_self_recursion(originator)
  end
end

#create(*args) ⇒ Object



863
864
865
# File 'lib/puppet/pops/types/p_object_type.rb', line 863

def create(*args)
  implementation_class.create(*args)
end

#create_init_hash_typePStructType

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.

Creates the type that a initialization hash used for creating instances of this type must conform to.

Returns:



875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/puppet/pops/types/p_object_type.rb', line 875

def create_init_hash_type
  struct_elems = {}
  attributes(true).values.each do |attr|
    unless attr.kind == ATTRIBUTE_KIND_CONSTANT || attr.kind == ATTRIBUTE_KIND_DERIVED
      if attr.value?
        struct_elems[TypeFactory.optional(attr.name)] = attr.type
      else
        struct_elems[attr.name] = attr.type
      end
    end
  end
  TypeFactory.struct(struct_elems)
end

#create_new_functionObject

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.



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/puppet/pops/types/p_object_type.rb', line 504

def create_new_function
  impl_class = implementation_class
  return impl_class.create_new_function(self) if impl_class.respond_to?(:create_new_function)

  (param_names, param_types, required_param_count) = parameter_info(impl_class)

  # Create the callable with a size that reflects the required and optional parameters
  create_type = TypeFactory.callable(*param_types, required_param_count, param_names.size)
  from_hash_type = TypeFactory.callable(init_hash_type, 1, 1)

  # Create and return a #new_XXX function where the dispatchers are added programmatically.
  Puppet::Functions.create_loaded_function(:"new_#{name}", loader) do
    # The class that creates new instances must be available to the constructor methods
    # and is therefore declared as a variable and accessor on the class that represents
    # this added function.
    @impl_class = impl_class

    def self.impl_class
      @impl_class
    end

    # It's recommended that an implementor of an Object type provides the method #from_asserted_hash.
    # This method should accept a hash and assume that type assertion has been made already (it is made
    # by the dispatch added here).
    if impl_class.respond_to?(:from_asserted_hash)
      dispatcher.add(Functions::Dispatch.new(from_hash_type, :from_hash, ['hash']))
      def from_hash(hash)
        self.class.impl_class.from_asserted_hash(hash)
      end
    end

    # Add the dispatch that uses the standard #from_asserted_args or #new method on the class. It's assumed that the
    # method performs no assertions.
    dispatcher.add(Functions::Dispatch.new(create_type, :create, param_names))
    if impl_class.respond_to?(:from_asserted_args)
      def create(*args)
        self.class.impl_class.from_asserted_args(*args)
      end
    else
      def create(*args)
        self.class.impl_class.new(*args)
      end
    end
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


919
920
921
922
923
924
925
926
927
928
# File 'lib/puppet/pops/types/p_object_type.rb', line 919

def eql?(o)
  self.class == o.class &&
    @name == o.name &&
    @parent == o.parent &&
    @type_parameters == o.type_parameters &&
    @attributes == o.attributes &&
    @functions == o.functions &&
    @equality == o.equality &&
    @checks == o.checks
end

#equality_attributesHash{String=>PAttribute}

Returns the attributes that participate in equality comparison. Inherited equality attributes are included.

Returns:

  • (Hash{String=>PAttribute})

    a hash of attributes



978
979
980
981
982
# File 'lib/puppet/pops/types/p_object_type.rb', line 978

def equality_attributes
  all = {}
  collect_equality_attributes(all)
  all
end

#equality_include_type?Boolean

Returns ‘true` if this type is included when comparing instances.

Returns:

  • (Boolean)

    ‘true` if this type is included when comparing instances



986
987
988
# File 'lib/puppet/pops/types/p_object_type.rb', line 986

def equality_include_type?
  @equality_include_type
end

#extract_init_hash(o) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/puppet/pops/types/p_object_type.rb', line 595

def extract_init_hash(o)
  return o._pcore_init_hash if o.respond_to?(:_pcore_init_hash)

  result = {}
  pic = parameter_info(o.class)
  attrs = attributes(true)
  pic[0].each do |name|
    v = o.send(name)
    result[name] = v unless attrs[name].default_value?(v)
  end
  result
end

#from_hash(hash) ⇒ Object



867
868
869
# File 'lib/puppet/pops/types/p_object_type.rb', line 867

def from_hash(hash)
  implementation_class.from_hash(hash)
end

#functions(include_parent = false) ⇒ Hash{String=>PFunction}

Returns the functions of this ‘Object` type. If include_parent is `true`, then all inherited functions will be included in the returned `Hash`.

Parameters:

  • include_parent (Boolean) (defaults to: false)

    ‘true` if inherited functions should be included

Returns:

  • (Hash{String=>PFunction})

    a hash with the functions



996
997
998
# File 'lib/puppet/pops/types/p_object_type.rb', line 996

def functions(include_parent = false)
  get_members(include_parent, :functions)
end

#hashObject



930
931
932
# File 'lib/puppet/pops/types/p_object_type.rb', line 930

def hash
  @name.nil? ? [@parent, @type_parameters, @attributes, @functions].hash : @name.hash
end

#implementation_class(create = true) ⇒ 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.



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/puppet/pops/types/p_object_type.rb', line 551

def implementation_class(create = true)
  if @implementation_class.nil? && create
    ir = Loaders.implementation_registry
    class_name = ir.nil? ? nil : ir.module_name_for_type(self)
    if class_name.nil?
      # Use generator to create a default implementation
      @implementation_class = RubyGenerator.new.create_class(self)
      @implementation_class.class_eval(&@implementation_override) if instance_variable_defined?(:@implementation_override)
    else
      # Can the mapping be loaded?
      @implementation_class = ClassLoader.provide(class_name)

      raise Puppet::Error, "Unable to load class #{class_name}" if @implementation_class.nil?
      unless @implementation_class < PuppetObject || @implementation_class.respond_to?(:ecore)
        raise Puppet::Error, "Unable to create an instance of #{name}. #{class_name} does not include module #{PuppetObject.name}"
      end
    end
  end
  @implementation_class
end

#implementation_class=(cls) ⇒ 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.

Raises:

  • (ArgumentError)


573
574
575
576
577
# File 'lib/puppet/pops/types/p_object_type.rb', line 573

def implementation_class=(cls)
  raise ArgumentError, "attempt to redefine implementation class for #{label}" unless @implementation_class.nil?

  @implementation_class = cls
end

#implementation_override=(block) ⇒ 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.

The block passed to this method will be passed in a call to ‘#class_eval` on the dynamically generated class for this data type. It’s indended use is to complement or redefine the generated methods and attribute readers.

The method is normally called with the block passed to ‘#implementation` when a data type is defined using DataTypes::create_type.



587
588
589
590
591
592
593
# File 'lib/puppet/pops/types/p_object_type.rb', line 587

def implementation_override=(block)
  if !@implementation_class.nil? || instance_variable_defined?(:@implementation_override)
    raise ArgumentError, "attempt to redefine implementation override for #{label}"
  end

  @implementation_override = block
end

#init_hash_typePStructType

Returns the type that a initialization hash used for creating instances of this type must conform to.

Returns:



855
856
857
# File 'lib/puppet/pops/types/p_object_type.rb', line 855

def init_hash_type
  @init_hash_type ||= create_init_hash_type
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/puppet/pops/types/p_object_type.rb', line 454

def instance?(o, guard = nil)
  if o.is_a?(PuppetObject)
    assignable?(o._pcore_type, guard)
  else
    name = o.class.name
    return false if name.nil? # anonymous class that doesn't implement PuppetObject is not an instance

    ir = Loaders.implementation_registry
    type = ir.nil? ? nil : ir.type_for_module(name)
    !type.nil? && assignable?(type, guard)
  end
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


938
939
940
# File 'lib/puppet/pops/types/p_object_type.rb', line 938

def iterable?(guard = nil)
  @parent.nil? ? false : @parent.iterable?(guard)
end

#iterable_type(guard = nil) ⇒ Object



942
943
944
# File 'lib/puppet/pops/types/p_object_type.rb', line 942

def iterable_type(guard = nil)
  @parent.nil? ? false : @parent.iterable_type(guard)
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


934
935
936
# File 'lib/puppet/pops/types/p_object_type.rb', line 934

def kind_of_callable?(optional = true, guard = nil)
  @parent.nil? ? false : @parent.kind_of_callable?(optional, guard)
end

#labelObject

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.



1012
1013
1014
# File 'lib/puppet/pops/types/p_object_type.rb', line 1012

def label
  @name || 'Object'
end

#members(include_parent = false) ⇒ Hash{String=>PAnnotatedMember}

Returns the members (attributes and functions) of this ‘Object` type. If include_parent is `true`, then all inherited members will be included in the returned `Hash`.

Parameters:

  • include_parent (Boolean) (defaults to: false)

    ‘true` if inherited members should be included

Returns:



960
961
962
# File 'lib/puppet/pops/types/p_object_type.rb', line 960

def members(include_parent = false)
  get_members(include_parent, :both)
end

#new_functionObject

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.



468
469
470
# File 'lib/puppet/pops/types/p_object_type.rb', line 468

def new_function
  @new_function ||= create_new_function
end

#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

#parameterized?Boolean

Returns:

  • (Boolean)


946
947
948
949
950
951
952
# File 'lib/puppet/pops/types/p_object_type.rb', line 946

def parameterized?
  if @type_parameters.empty?
    @parent.is_a?(PObjectType) ? @parent.parameterized? : false
  else
    true
  end
end

#read(value_count, deserializer) ⇒ 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.

Read an instance of this type from a deserializer

Parameters:

  • value_count (Integer)

    the number attributes needed to create the instance

  • deserializer (Serialization::Deserializer)

    the deserializer to read from

Returns:

  • (Object)

    the created instance



491
492
493
# File 'lib/puppet/pops/types/p_object_type.rb', line 491

def read(value_count, deserializer)
  reader.read(self, implementation_class, value_count, deserializer)
end

#reader=(reader) ⇒ 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.

Assign a new instance reader to this type

Parameters:



475
476
477
# File 'lib/puppet/pops/types/p_object_type.rb', line 475

def reader=(reader)
  @reader = reader
end

#resolved_parentObject

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.



1017
1018
1019
1020
1021
1022
1023
# File 'lib/puppet/pops/types/p_object_type.rb', line 1017

def resolved_parent
  parent = @parent
  while parent.is_a?(PTypeAliasType)
    parent = parent.resolved_type
  end
  parent
end

#simple_nameObject



1025
1026
1027
# File 'lib/puppet/pops/types/p_object_type.rb', line 1025

def simple_name
  label.split(DOUBLE_COLON).last
end

#type_parameters(include_parent = false) ⇒ Hash{String=>PTypeParameter}

Returns the type_parameters of this ‘Object` type. If include_parent is `true`, then all inherited type_parameters will be included in the returned `Hash`.

Parameters:

  • include_parent (Boolean) (defaults to: false)

    ‘true` if inherited type_parameters should be included

Returns:



1035
1036
1037
1038
1039
# File 'lib/puppet/pops/types/p_object_type.rb', line 1035

def type_parameters(include_parent = false)
  all = {}
  collect_type_parameters(all, include_parent)
  all
end

#write(value, serializer) ⇒ 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.

Write an instance of this type using a serializer

Parameters:



499
500
501
# File 'lib/puppet/pops/types/p_object_type.rb', line 499

def write(value, serializer)
  writer.write(self, value, serializer)
end

#writer=(writer) ⇒ 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.

Assign a new instance write to this type

Parameters:



482
483
484
# File 'lib/puppet/pops/types/p_object_type.rb', line 482

def writer=(writer)
  @writer = writer
end