Class: Puppet::Pops::Types::PObjectType::PAttribute
- Inherits:
-
PAnnotatedMember
- Object
- PAnnotatedMember
- Puppet::Pops::Types::PObjectType::PAttribute
- Defined in:
- lib/puppet/pops/types/p_object_type.rb
Overview
Describes a named Attribute in an Object type
Direct Known Subclasses
Constant Summary
Constants included from Annotatable
Instance Attribute Summary collapse
-
#kind ⇒ String?
readonly
The attribute kind as defined by #TYPE_ATTRIBUTE_KIND, or ‘nil`.
Attributes inherited from PAnnotatedMember
Class Method Summary collapse
- .feature_type ⇒ Object private
Instance Method Summary collapse
-
#_pcore_init_hash ⇒ Hash{String=>Object}
private
Returns the member as a hash suitable as an argument for constructor.
- #callable_type ⇒ Object
- #constant? ⇒ Boolean
-
#default_value?(value) ⇒ Booelan
True if the given value equals the default value for this attribute.
- #eql?(o) ⇒ Boolean
-
#initialize(name, container, init_hash) ⇒ PAttribute
constructor
A new instance of PAttribute.
-
#value ⇒ Object
Returns the value of this attribute, or raises an error if no value has been defined.
-
#value? ⇒ Boolean
‘true` if a value has been defined for this attribute.
Methods inherited from PAnnotatedMember
#==, #accept, #assert_can_be_overridden, #assert_override, #create_dispatch, #feature_type, #final?, #hash, #invoke, #label, label, #override?
Methods included from InvocableMember
Methods included from Annotatable
#annotatable_accept, #annotations, #init_annotatable
Constructor Details
#initialize(name, container, init_hash) ⇒ PAttribute
Returns a new instance of PAttribute.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 296 def initialize(name, container, init_hash) super(name, container, TypeAsserter.assert_instance_of(nil, TYPE_ATTRIBUTE, init_hash) { "initializer for #{self.class.label(container, name)}" }) if name == Serialization::PCORE_TYPE_KEY || name == Serialization::PCORE_VALUE_KEY raise Puppet::ParseError, _("The attribute '%{name}' is reserved and cannot be used") % { name: name } end @kind = init_hash[KEY_KIND] if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied if init_hash.include?(KEY_FINAL) && !@final # TRANSLATOR 'final => false' is puppet syntax and should not be translated raise Puppet::ParseError, _("%{label} of kind 'constant' cannot be combined with final => false") % { label: label } end @final = true end if init_hash.include?(KEY_VALUE) if @kind == ATTRIBUTE_KIND_DERIVED || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED raise Puppet::ParseError, _("%{label} of kind '%{kind}' cannot be combined with an attribute value") % { label: label, kind: @kind } end v = init_hash[KEY_VALUE] @value = v == :default ? v : TypeAsserter.assert_instance_of(nil, type, v) { "#{label} #{KEY_VALUE}" } else raise Puppet::ParseError, _("%{label} of kind 'constant' requires a value") % { label: label } if @kind == ATTRIBUTE_KIND_CONSTANT @value = :undef # Not to be confused with nil or :default end end |
Instance Attribute Details
#kind ⇒ String? (readonly)
Returns The attribute kind as defined by #TYPE_ATTRIBUTE_KIND, or ‘nil`.
287 288 289 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 287 def kind @kind end |
Class Method Details
.feature_type ⇒ 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.
376 377 378 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 376 def self.feature_type 'attribute' end |
Instance Method Details
#_pcore_init_hash ⇒ Hash{String=>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.
Returns the member as a hash suitable as an argument for constructor. Name is excluded
338 339 340 341 342 343 344 345 346 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 338 def _pcore_init_hash hash = super unless @kind.nil? hash[KEY_KIND] = @kind hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied end hash[KEY_VALUE] = @value unless @value == :undef hash end |
#callable_type ⇒ Object
326 327 328 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 326 def callable_type TYPE_ATTRIBUTE_CALLABLE end |
#constant? ⇒ Boolean
348 349 350 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 348 def constant? @kind == ATTRIBUTE_KIND_CONSTANT end |
#default_value?(value) ⇒ Booelan
Returns true if the given value equals the default value for this attribute.
353 354 355 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 353 def default_value?(value) @value == value end |
#eql?(o) ⇒ Boolean
331 332 333 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 331 def eql?(o) super && @kind == o.kind && @value == (o.value? ? o.value : :undef) end |
#value ⇒ Object
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`.
368 369 370 371 372 373 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 368 def value # An error must be raised here since `nil` is a valid value and it would be bad to leak the :undef symbol raise Puppet::Error, "#{label} has no value" if @value == :undef @value end |
#value? ⇒ Boolean
Returns ‘true` if a value has been defined for this attribute.
358 359 360 |
# File 'lib/puppet/pops/types/p_object_type.rb', line 358 def value? @value != :undef end |