Class: Puppet::Pops::Types::PObjectType::PAttribute

Inherits:
PAnnotatedMember show all
Defined in:
lib/puppet/pops/types/p_object_type.rb

Overview

Describes a named Attribute in an Object type

Constant Summary

Constants included from Annotatable

Annotatable::TYPE_ANNOTATIONS, Annotatable::TYPE_ANNOTATION_KEY_TYPE, Annotatable::TYPE_ANNOTATION_VALUE_TYPE

Instance Attribute Summary collapse

Attributes inherited from PAnnotatedMember

#container, #name, #type

Attributes included from Annotatable

#annotations

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnnotatedMember

#==, #accept, #assert_can_be_overridden, #assert_override, #create_dispatch, #feature_type, #final?, #hash, #invoke, #label, label, #override?

Methods included from Annotatable

#annotatable_accept, #init_annotatable

Constructor Details

#initialize(name, container, i12n_hash) ⇒ PAttribute

Returns a new instance of PAttribute.

Options Hash (i12n_hash):

  • 'type' (PAnyType)

    The attribute type (required)

  • 'value' (Object)

    The default value, must be an instanceof the given ‘type` (optional)

  • 'kind' (String)

    The attribute kind, matching #TYPE_ATTRIBUTE_KIND



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/puppet/pops/types/p_object_type.rb', line 248

def initialize(name, container, i12n_hash)
  super(name, container, TypeAsserter.assert_instance_of(nil, TYPE_ATTRIBUTE, i12n_hash) { "initializer for #{self.class.label(container, name)}" })
  @kind = i12n_hash[KEY_KIND]
  if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied
    if i12n_hash.include?(KEY_FINAL) && !@final
      raise Puppet::ParseError, "#{label} of kind 'constant' cannot be combined with final => false"
    end
    @final = true
  end

  if i12n_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"
    end
    v = i12n_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" if @kind == ATTRIBUTE_KIND_CONSTANT
    @value = :undef # Not to be confused with nil or :default
  end
end

Instance Attribute Details

#kindString? (readonly)

Returns The attribute kind as defined by #TYPE_ATTRIBUTE_KIND, or ‘nil` to indicate that.



239
240
241
# File 'lib/puppet/pops/types/p_object_type.rb', line 239

def kind
  @kind
end

Class Method Details

.feature_typeObject

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.



310
311
312
# File 'lib/puppet/pops/types/p_object_type.rb', line 310

def self.feature_type
  'attribute'
end

Instance Method Details

#callable_typeObject



270
271
272
# File 'lib/puppet/pops/types/p_object_type.rb', line 270

def callable_type
  TYPE_ATTRIBUTE_CALLABLE
end

#eql?(o) ⇒ Boolean



275
276
277
# File 'lib/puppet/pops/types/p_object_type.rb', line 275

def eql?(o)
  super && @kind == o.kind && @value == (o.value? ? o.value : :undef)
end

#i12n_hashHash{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



282
283
284
285
286
287
288
289
290
# File 'lib/puppet/pops/types/p_object_type.rb', line 282

def i12n_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

#valueObject

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`.

Raises:



303
304
305
306
307
# File 'lib/puppet/pops/types/p_object_type.rb', line 303

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.



293
294
295
# File 'lib/puppet/pops/types/p_object_type.rb', line 293

def value?
  @value != :undef
end