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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ void

Initializes a property model.

Parameters:



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

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.



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

def doc
  @doc
end

#nameObject (readonly)

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



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

def name
  @name
end

#typeObject (readonly)

Gets the Puppet type of the property.



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

def type
  @type
end

Class Method Details

.get_puppet_type(property) ⇒ String

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.



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

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

Determines if this property is a namevar.

Returns:

  • (Boolean)

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



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

def is_namevar?
  @is_namevar
end