class Puppet::Pops::Types::PObjectType::PAttribute

Describes a named Attribute in an Object type @api public

Attributes

kind[R]

@return [String,nil] The attribute kind as defined by #TYPE_ATTRIBUTE_KIND, or `nil`

Public Class Methods

feature_type() click to toggle source

@api private

    # File lib/puppet/pops/types/p_object_type.rb
361 def self.feature_type
362   'attribute'
363 end
new(name, container, init_hash) click to toggle source

@param name [String] The name of the attribute @param container [PObjectType] The containing object type @param init_hash [Hash{String=>Object}] Hash containing attribute options @option init_hash [PAnyType] 'type' The attribute type (required) @option init_hash [Object] 'value' The default value, must be an instanceof the given `type` (optional) @option init_hash [String] 'kind' The attribute kind, matching #TYPE_ATTRIBUTE_KIND @api public

    # File lib/puppet/pops/types/p_object_type.rb
286 def initialize(name, container, init_hash)
287   super(name, container, TypeAsserter.assert_instance_of(nil, TYPE_ATTRIBUTE, init_hash) { "initializer for #{self.class.label(container, name)}" })
288   if name == Serialization::PCORE_TYPE_KEY || name == Serialization::PCORE_VALUE_KEY
289     raise Puppet::ParseError, _("The attribute '%{name}' is reserved and cannot be used") % { name: name}
290   end
291   @kind = init_hash[KEY_KIND]
292   if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied
293     if init_hash.include?(KEY_FINAL) && !@final
294       #TRANSLATOR 'final => false' is puppet syntax and should not be translated
295       raise Puppet::ParseError, _("%{label} of kind 'constant' cannot be combined with final => false") % { label: label }
296     end
297     @final = true
298   end
299 
300   if init_hash.include?(KEY_VALUE)
301     if @kind == ATTRIBUTE_KIND_DERIVED || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED
302       raise Puppet::ParseError, _("%{label} of kind '%{kind}' cannot be combined with an attribute value") % { label: label, kind: @kind }
303     end
304     v = init_hash[KEY_VALUE]
305     @value = v == :default ? v : TypeAsserter.assert_instance_of(nil, type, v) {"#{label} #{KEY_VALUE}" }
306   else
307     raise Puppet::ParseError, _("%{label} of kind 'constant' requires a value") % { label: label } if @kind == ATTRIBUTE_KIND_CONSTANT
308     @value = :undef # Not to be confused with nil or :default
309   end
310 end

Public Instance Methods

_pcore_init_hash() click to toggle source

Returns the member as a hash suitable as an argument for constructor. Name is excluded @return [Hash{String=>Object}] the hash @api private

Calls superclass method Puppet::Pops::Types::PObjectType::PAnnotatedMember#_pcore_init_hash
    # File lib/puppet/pops/types/p_object_type.rb
324 def _pcore_init_hash
325   hash = super
326   unless @kind.nil?
327     hash[KEY_KIND] = @kind
328     hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied
329   end
330   hash[KEY_VALUE] = @value unless @value == :undef
331   hash
332 end
callable_type() click to toggle source
    # File lib/puppet/pops/types/p_object_type.rb
312 def callable_type
313   TYPE_ATTRIBUTE_CALLABLE
314 end
constant?() click to toggle source
    # File lib/puppet/pops/types/p_object_type.rb
334 def constant?
335   @kind == ATTRIBUTE_KIND_CONSTANT
336 end
default_value?(value) click to toggle source

@return [Booelan] true if the given value equals the default value for this attribute

    # File lib/puppet/pops/types/p_object_type.rb
339 def default_value?(value)
340   @value == value
341 end
eql?(o) click to toggle source

@api public

    # File lib/puppet/pops/types/p_object_type.rb
317 def eql?(o)
318   super && @kind == o.kind && @value == (o.value? ? o.value : :undef)
319 end
value() click to toggle source

Returns the value of this attribute, or raises an error if no value has been defined. Raising an error is necessary since a defined value may be `nil`.

@return [Object] the value that has been defined for this attribute. @raise [Puppet::Error] if no value has been defined @api public

    # File lib/puppet/pops/types/p_object_type.rb
354 def value
355   # An error must be raised here since `nil` is a valid value and it would be bad to leak the :undef symbol
356   raise Puppet::Error, "#{label} has no value" if @value == :undef
357   @value
358 end
value?() click to toggle source

@return [Boolean] `true` if a value has been defined for this attribute.

    # File lib/puppet/pops/types/p_object_type.rb
344 def value?
345   @value != :undef
346 end