class Puppet::Pops::Types::PObjectType::PAttribute
Describes a named Attribute in an Object
type @api public
Attributes
@return [String,nil] The attribute kind as defined by #TYPE_ATTRIBUTE_KIND, or `nil`
Public Class Methods
@api private
# File lib/puppet/pops/types/p_object_type.rb 360 def self.feature_type 361 'attribute' 362 end
@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
Puppet::Pops::Types::PObjectType::PAnnotatedMember::new
# File lib/puppet/pops/types/p_object_type.rb 285 def initialize(name, container, init_hash) 286 super(name, container, TypeAsserter.assert_instance_of(nil, TYPE_ATTRIBUTE, init_hash) { "initializer for #{self.class.label(container, name)}" }) 287 if name == Serialization::PCORE_TYPE_KEY || name == Serialization::PCORE_VALUE_KEY 288 raise Puppet::ParseError, _("The attribute '%{name}' is reserved and cannot be used") % { name: name} 289 end 290 @kind = init_hash[KEY_KIND] 291 if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied 292 if init_hash.include?(KEY_FINAL) && !@final 293 #TRANSLATOR 'final => false' is puppet syntax and should not be translated 294 raise Puppet::ParseError, _("%{label} of kind 'constant' cannot be combined with final => false") % { label: label } 295 end 296 @final = true 297 end 298 299 if init_hash.include?(KEY_VALUE) 300 if @kind == ATTRIBUTE_KIND_DERIVED || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED 301 raise Puppet::ParseError, _("%{label} of kind '%{kind}' cannot be combined with an attribute value") % { label: label, kind: @kind } 302 end 303 v = init_hash[KEY_VALUE] 304 @value = v == :default ? v : TypeAsserter.assert_instance_of(nil, type, v) {"#{label} #{KEY_VALUE}" } 305 else 306 raise Puppet::ParseError, _("%{label} of kind 'constant' requires a value") % { label: label } if @kind == ATTRIBUTE_KIND_CONSTANT 307 @value = :undef # Not to be confused with nil or :default 308 end 309 end
Public Instance Methods
Returns the member as a hash suitable as an argument for constructor. Name is excluded @return [Hash{String=>Object}] the hash @api private
# File lib/puppet/pops/types/p_object_type.rb 323 def _pcore_init_hash 324 hash = super 325 unless @kind.nil? 326 hash[KEY_KIND] = @kind 327 hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied 328 end 329 hash[KEY_VALUE] = @value unless @value == :undef 330 hash 331 end
# File lib/puppet/pops/types/p_object_type.rb 311 def callable_type 312 TYPE_ATTRIBUTE_CALLABLE 313 end
# File lib/puppet/pops/types/p_object_type.rb 333 def constant? 334 @kind == ATTRIBUTE_KIND_CONSTANT 335 end
@return [Booelan] true if the given value equals the default value for this attribute
# File lib/puppet/pops/types/p_object_type.rb 338 def default_value?(value) 339 @value == value 340 end
@api public
Puppet::Pops::Types::PObjectType::PAnnotatedMember#eql?
# File lib/puppet/pops/types/p_object_type.rb 316 def eql?(o) 317 super && @kind == o.kind && @value == (o.value? ? o.value : :undef) 318 end
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 353 def value 354 # An error must be raised here since `nil` is a valid value and it would be bad to leak the :undef symbol 355 raise Puppet::Error, "#{label} has no value" if @value == :undef 356 @value 357 end
@return [Boolean] `true` if a value has been defined for this attribute.
# File lib/puppet/pops/types/p_object_type.rb 343 def value? 344 @value != :undef 345 end