Class: Puppet::Pops::Types::PObjectTypeExtension
- Inherits:
-
PAnyType
- Object
- TypedModelObject
- PAnyType
- Puppet::Pops::Types::PObjectTypeExtension
- Includes:
- TypeWithMembers
- Defined in:
- lib/puppet/pops/types/p_object_type_extension.rb
Overview
Base class for Parameterized Object implementations. The wrapper impersonates the base object and extends it with methods to filter assignable types and instances based on parameter values.
Constant Summary
Constants inherited from PAnyType
Puppet::Pops::Types::PAnyType::DEFAULT
Instance Attribute Summary collapse
- #base_type ⇒ Object readonly
- #parameters ⇒ Object readonly
Class Method Summary collapse
- .create(base_type, init_parameters) ⇒ Object private
-
.create_from_instance(base_type, instance) ⇒ PObjectTypeExtension
private
Creates an array of type parameters from the attributes of the given instance that matches the type parameters by name.
- .register_ptype(loader, ir) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
- #check_param(type_param, v) ⇒ Object
- #check_self_recursion(originator) ⇒ Object private
- #create(*args) ⇒ Object private
- #eql?(o) ⇒ Boolean private
- #generalize ⇒ Object private
- #hash ⇒ Object private
-
#init_parameters ⇒ Array
private
Return the parameter values as positional arguments with unset values as :default.
-
#initialize(base_type, init_parameters) ⇒ PObjectTypeExtension
constructor
private
A new instance of PObjectTypeExtension.
- #instance?(o, guard = nil) ⇒ Boolean private
- #loader ⇒ Object private
- #new_function ⇒ Object private
- #simple_name ⇒ Object private
Methods inherited from PAnyType
#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #iterable?, #iterable_type, #kind_of_callable?, #name, 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 Visitable
Methods included from PuppetObject
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s
Constructor Details
#initialize(base_type, init_parameters) ⇒ PObjectTypeExtension
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 a new instance of PObjectTypeExtension.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 53 def initialize(base_type, init_parameters) pts = base_type.type_parameters(true) raise Puppet::ParseError, _('The %{label}-Type cannot be parameterized using []') % { label: base_type.label } if pts.empty? @base_type = base_type named_args = init_parameters.size == 1 && init_parameters[0].is_a?(Hash) if named_args # Catch case when first parameter is an assignable Hash named_args = pts.size >= 1 && !pts.values[0].type.instance?(init_parameters[0]) end by_name = {} if named_args hash = init_parameters[0] hash.each_pair do |pn, pv| tp = pts[pn] if tp.nil? raise Puppet::ParseError, _("'%{pn}' is not a known type parameter for %{label}-Type") % { pn: pn, label: base_type.label } end by_name[pn] = check_param(tp, pv) unless pv == :default end else pts.values.each_with_index do |tp, idx| if idx < init_parameters.size pv = init_parameters[idx] by_name[tp.name] = check_param(tp, pv) unless pv == :default end end end if by_name.empty? raise Puppet::ParseError, _('The %{label}-Type cannot be parameterized using an empty parameter list') % { label: base_type.label } end @parameters = by_name end |
Instance Attribute Details
#base_type ⇒ Object (readonly)
23 24 25 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 23 def base_type @base_type end |
#parameters ⇒ Object (readonly)
23 24 25 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 23 def parameters @parameters end |
Class Method Details
.create(base_type, init_parameters) ⇒ 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.
26 27 28 29 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 26 def self.create(base_type, init_parameters) impl_class = Loaders.implementation_registry.module_for_type("#{base_type.name}TypeExtension") || self impl_class.new(base_type, init_parameters) end |
.create_from_instance(base_type, instance) ⇒ PObjectTypeExtension
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 an array of type parameters from the attributes of the given instance that matches the type parameters by name. Type parameters for which there is no matching attribute will have ‘nil` in their corresponding position on the array. The array is then passed as the `init_parameters` argument in a call to `create`
38 39 40 41 42 43 44 45 46 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 38 def self.create_from_instance(base_type, instance) type_parameters = base_type.type_parameters(true) attrs = base_type.attributes(true) params = type_parameters.keys.map do |pn| attr = attrs[pn] attr.nil? ? nil : instance.send(pn) end create(base_type, params) end |
.register_ptype(loader, ir) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 12 def self.register_ptype(loader, ir) create_ptype(loader, ir, 'AnyType', 'base_type' => { KEY_TYPE => PTypeType::DEFAULT }, 'init_parameters' => { KEY_TYPE => PArrayType::DEFAULT } ) end |
Instance Method Details
#[](name) ⇒ Object
48 49 50 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 48 def [](name) @base_type[name] end |
#check_param(type_param, v) ⇒ Object
88 89 90 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 88 def check_param(type_param, v) TypeAsserter.assert_instance_of(nil, type_param.type, v) { type_param.label } 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.
132 133 134 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 132 def check_self_recursion(originator) @base_type.check_self_recursion(originator) end |
#create(*args) ⇒ 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.
137 138 139 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 137 def create(*args) @base_type.create(*args) end |
#eql?(o) ⇒ Boolean
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.
112 113 114 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 112 def eql?(o) super(o) && @base_type.eql?(o.base_type) && @parameters.eql?(o.parameters) end |
#generalize ⇒ 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.
117 118 119 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 117 def generalize @base_type end |
#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.
122 123 124 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 122 def hash @base_type.hash ^ @parameters.hash end |
#init_parameters ⇒ Array
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.
Return the parameter values as positional arguments with unset values as :default. The array is stripped from trailing :default values
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 96 def init_parameters pts = @base_type.type_parameters(true) if pts.size > 2 @parameters else result = pts.values.map do |tp| pn = tp.name @parameters.include?(pn) ? @parameters[pn] : :default end # Remove trailing defaults result.pop while result.last == :default result end end |
#instance?(o, guard = nil) ⇒ Boolean
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.
142 143 144 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 142 def instance?(o, guard = nil) @base_type.instance?(o, guard) && test_instance?(o, guard) end |
#loader ⇒ 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.
127 128 129 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 127 def loader @base_type.loader end |
#new_function ⇒ 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.
147 148 149 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 147 def new_function @base_type.new_function end |
#simple_name ⇒ 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.
152 153 154 |
# File 'lib/puppet/pops/types/p_object_type_extension.rb', line 152 def simple_name @base_type.simple_name end |