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

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

Overview

A model for resource type properties and parameters.

API:

  • public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ void

Initializes a property model.

Parameters:

  • The Puppet property to model.

API:

  • public



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

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)

Gets the doc string of the property.

API:

  • public



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

def doc
  @doc
end

#nameObject (readonly)

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

API:

  • public



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

def name
  @name
end

#typeObject (readonly)

Gets the Puppet type of the property.

API:

  • public



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

def type
  @type
end

Class Method Details

.get_puppet_type(property) ⇒ String

Gets the Puppet type for a property.

Parameters:

  • The Puppet property to get the Puppet type for.

Returns:

  • Returns the string representing the Puppet type.

API:

  • public



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
67
68
# File 'lib/puppet/generate/models/type/property.rb', line 37

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].compact
  return variant[0] if variant.size == 1

  "Variant[#{variant.join(', ')}]"
end

Instance Method Details

#is_namevar?Boolean

Determines if this property is a namevar.

Returns:

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

API:

  • public



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

def is_namevar?
  @is_namevar
end