Class: Puppet::Generate::Models::Type::Property Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/generate/models/type/property.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A model for resource type properties and parameters.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ void

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.

Initializes a property model.

Parameters:



20
21
22
23
24
25
# File 'lib/puppet/generate/models/type/property.rb', line 20

def initialize(property)
  @name = Puppet::Pops::Types::StringConverter.convert(property.name.to_s, '%p')
  @type = self.class.get_puppet_type(property)
  @doc = property.doc.strip
  @is_namevar = property.isnamevar?
end

Instance Attribute Details

#docObject (readonly)

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.

Gets the doc string of the property.



15
16
17
# File 'lib/puppet/generate/models/type/property.rb', line 15

def doc
  @doc
end

#nameObject (readonly)

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.

Gets the name of the property as a Puppet string literal.



9
10
11
# File 'lib/puppet/generate/models/type/property.rb', line 9

def name
  @name
end

#typeObject (readonly)

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.

Gets the Puppet type of the property.



12
13
14
# File 'lib/puppet/generate/models/type/property.rb', line 12

def type
  @type
end

Class Method Details

.get_puppet_type(property) ⇒ String

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.

Gets the Puppet type for a property.

Parameters:

  • property (Puppet::Property)

    The Puppet property to get the Puppet type for.

Returns:

  • (String)

    Returns the string representing the Puppet type.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/generate/models/type/property.rb', line 36

def self.get_puppet_type(property)
  # HACK: the value collection does not expose the underlying value information at all
  #       thus this horribleness to get the underlying values hash
  regexes = []
  strings = []
  values = property.value_collection.instance_variable_get('@values') || {}
  values.each do |_, value|
    if value.regex?
      regexes << Puppet::Pops::Types::StringConverter.convert(value.name, '%p')
      next
    end

    strings << Puppet::Pops::Types::StringConverter.convert(value.name.to_s, '%p')
    value.aliases.each do |a|
      strings << Puppet::Pops::Types::StringConverter.convert(a.to_s, '%p')
    end
  end

  # If no string or regexes, default to Any type
  return 'Any' if strings.empty? && regexes.empty?

  # Calculate a variant of supported values
  # Note that boolean strings are mapped to Variant[Boolean, Enum['true', 'false']]
  # because of tech debt...
  enum    = strings.empty? ? nil : "Enum[#{strings.join(', ')}]"
  pattern = regexes.empty? ? nil : "Pattern[#{regexes.join(', ')}]"
  boolean = strings.include?('\'true\'') || strings.include?('\'false\'') ? 'Boolean' : nil
  variant = [boolean, enum, pattern].reject { |t| t.nil? }
  return variant[0] if variant.size == 1
  "Variant[#{variant.join(', ')}]"
end

Instance Method Details

#is_namevar?Boolean

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.

Determines if this property is a namevar.

Returns:

  • (Boolean)

    Returns true if the property is a namevar or false if not.



29
30
31
# File 'lib/puppet/generate/models/type/property.rb', line 29

def is_namevar?
  @is_namevar
end