class Puppet::Pops::Types::PObjectType::PAnnotatedMember
@abstract Encapsulates behavior common to {PAttribute} and {PFunction} @api public
Attributes
@return [PObjectType] the object type containing this member @api public
@return [String] the name of this member @api public
@return [PAnyType] the type of this member @api public
Public Class Methods
@api private
# File lib/puppet/pops/types/p_object_type.rb 262 def self.feature_type 263 raise NotImplementedError, "'#{self.class.name}' should implement #feature_type" 264 end
# File lib/puppet/pops/types/p_object_type.rb 266 def self.label(container, name) 267 "#{feature_type} #{container.label}[#{name}]" 268 end
@param name [String] The name of the member @param container [PObjectType] The containing object type @param init_hash [Hash{String=>Object}] Hash containing feature options @option init_hash [PAnyType] 'type' The member type (required) @option init_hash [Boolean] 'override' `true` if this feature must override an inherited feature. Default is `false`. @option init_hash [Boolean] 'final' `true` if this feature cannot be overridden. Default is `false`. @option init_hash [Hash{PTypeType => Hash}] 'annotations' Annotations hash. Default is `nil`. @api public
# File lib/puppet/pops/types/p_object_type.rb 116 def initialize(name, container, init_hash) 117 @name = name 118 @container = container 119 @type = init_hash[KEY_TYPE] 120 @override = init_hash[KEY_OVERRIDE] 121 @override = false if @override.nil? 122 @final = init_hash[KEY_FINAL] 123 @final = false if @final.nil? 124 init_annotatable(init_hash) 125 end
Public Instance Methods
@api public
# File lib/puppet/pops/types/p_object_type.rb 206 def ==(o) 207 eql?(o) 208 end
Returns the member as a hash suitable as an argument for constructor. Name is excluded @return [Hash{String=>Object}] the initialization hash @api private
# File lib/puppet/pops/types/p_object_type.rb 213 def _pcore_init_hash 214 hash = { KEY_TYPE => @type } 215 hash[KEY_FINAL] = true if @final 216 hash[KEY_OVERRIDE] = true if @override 217 hash[KEY_ANNOTATIONS] = @annotations unless @annotations.nil? 218 hash 219 end
Delegates to the contained type @param visitor [TypeAcceptor] the visitor @param guard [RecursionGuard] guard against recursion. Only used by internal calls @api public
# File lib/puppet/pops/types/p_object_type.rb 131 def accept(visitor, guard) 132 annotatable_accept(visitor, guard) 133 @type.accept(visitor, guard) 134 end
Checks if the given member can override this member.
@param member [PAnnotatedMember] the overriding member @return [PAnnotatedMember] its argument @raises [Puppet::ParseError] if the assertion fails @api private
# File lib/puppet/pops/types/p_object_type.rb 162 def assert_can_be_overridden(member) 163 unless self.class == member.class 164 raise Puppet::ParseError, _("%{member} attempts to override %{label}") % { member: member.label, label: label } 165 end 166 if @final && !(constant? && member.constant?) 167 raise Puppet::ParseError, _("%{member} attempts to override final %{label}") % { member: member.label, label: label } 168 end 169 unless member.override? 170 #TRANSLATOR 'override => true' is a puppet syntax and should not be translated 171 raise Puppet::ParseError, _("%{member} attempts to override %{label} without having override => true") % { member: member.label, label: label } 172 end 173 unless @type.assignable?(member.type) 174 raise Puppet::ParseError, _("%{member} attempts to override %{label} with a type that does not match") % { member: member.label, label: label } 175 end 176 member 177 end
Checks if the this member overrides an inherited member, and if so, that this member is declared with override = true and that the inherited member accepts to be overridden by this member.
@param parent_members [Hash{String=>PAnnotatedMember}] the hash of inherited members @return [PAnnotatedMember] this instance @raises [Puppet::ParseError] if the assertion fails @api private
# File lib/puppet/pops/types/p_object_type.rb 143 def assert_override(parent_members) 144 parent_member = parent_members[@name] 145 if parent_member.nil? 146 if @override 147 raise Puppet::ParseError, _("expected %{label} to override an inherited %{feature_type}, but no such %{feature_type} was found") % 148 { label: label, feature_type: feature_type } 149 end 150 self 151 else 152 parent_member.assert_can_be_overridden(self) 153 end 154 end
# File lib/puppet/pops/types/p_object_type.rb 179 def constant? 180 false 181 end
@api private
# File lib/puppet/pops/types/p_object_type.rb 250 def create_dispatch(instance) 251 # TODO: Assumes Ruby implementation for now 252 if(callable_type.is_a?(PVariantType)) 253 callable_type.types.map do |ct| 254 Functions::Dispatch.new(ct, RubyGenerator.protect_reserved_name(name), [], false, ct.block_type.nil? ? nil : 'block') 255 end 256 else 257 [Functions::Dispatch.new(callable_type, RubyGenerator.protect_reserved_name(name), [], false, callable_type.block_type.nil? ? nil : 'block')] 258 end 259 end
@api public
# File lib/puppet/pops/types/p_object_type.rb 201 def eql?(o) 202 self.class == o.class && @name == o.name && @type == o.type && @override == o.override? && @final == o.final? 203 end
@api private
# File lib/puppet/pops/types/p_object_type.rb 222 def feature_type 223 self.class.feature_type 224 end
@return [Boolean] `true` if this feature cannot be overridden @api public
# File lib/puppet/pops/types/p_object_type.rb 185 def final? 186 @final 187 end
@api public
# File lib/puppet/pops/types/p_object_type.rb 196 def hash 197 @name.hash ^ @type.hash 198 end
Performs type checking of arguments and invokes the method that corresponds to this method. The result of the invocation is returned
@param receiver [Object] The receiver of the call @param scope [Puppet::Parser::Scope] The caller scope @param args [Array] Array of arguments. @return [Object] The result returned by the member function or attribute
@api private
# File lib/puppet/pops/types/p_object_type.rb 240 def invoke(receiver, scope, args, &block) 241 @dispatch ||= create_dispatch(receiver) 242 243 args_type = TypeCalculator.infer_set(block_given? ? args + [block] : args) 244 found = @dispatch.find { |d| d.type.callable?(args_type) } 245 raise ArgumentError, TypeMismatchDescriber.describe_signatures(label, @dispatch, args_type) if found.nil? 246 found.invoke(receiver, scope, args, &block) 247 end
@api private
# File lib/puppet/pops/types/p_object_type.rb 227 def label 228 self.class.label(@container, @name) 229 end
@return [Boolean] `true` if this feature must override an inherited feature @api public
# File lib/puppet/pops/types/p_object_type.rb 191 def override? 192 @override 193 end