Class: DevStructure::Puppet::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/devstructure/puppet.rb

Overview

A generic Puppet resource to be subclassed. The name of the class dictates how the resource will be printed so do not instantiate this class directly.

Direct Known Subclasses

Exec, File, Package

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object

A resource with only a name, typically being listed as a dependency. This method enables syntax that looks a lot like the Puppet code that will be generated.



35
36
37
38
39
40
41
# File 'lib/devstructure/puppet.rb', line 35

def self.[](*args)
  if 1 == args.length
    self.new args[0], :complete
  else
    args.collect { |arg| self[arg] }
  end
end

.complete(name, options = {}) ⇒ Object

A resource that will (by default) provide its complete representation.



18
19
20
# File 'lib/devstructure/puppet.rb', line 18

def self.complete(name, options={})
  self.new name, :complete, options
end

.defaults(options = {}) ⇒ Object

A resource that will provide defaults for its resource type.



28
29
30
# File 'lib/devstructure/puppet.rb', line 28

def self.defaults(options={})
  self.new "", :defaults, options
end

.partial(name, options = {}) ⇒ Object

A resource that will (by default) provide its partial representation.



23
24
25
# File 'lib/devstructure/puppet.rb', line 23

def self.partial(name, options={})
  self.new name, :partial, options
end

Instance Method Details

#inspectObject

Resources themselves appear in options as dependencies so they are represented in Puppet’s reference notation.



120
121
122
# File 'lib/devstructure/puppet.rb', line 120

def inspect
  "#{@type.capitalize}[\"#{@name}\"]"
end

#to_s(style = nil) ⇒ Object

Return Puppet code for this resource. Whether a complete, partial, or defaults representation is returned depends on which class method instantiated this resource but can be overridden by passing a Symbol.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/devstructure/puppet.rb', line 46

def to_s(style=nil)
  style ||= @style
  out = []

  # Settle on the indentation level for the rest of the resource.
  # Raise an exception if the style is invalid.
  tab = case style
  when :complete
    out << "\t#{@type} { \"#{@name}\":"
    "\t"
  when :partial
    out << "\t\t\"#{@name}\":"
    "\t\t"
  when :defaults
    out << "\t#{@type.capitalize} {"
    "\t"
  else
    raise ArgumentError
  end

  # Handle the options Hash.
  if 0 < @options.length

    # Note the longest option name so we can line up => operators as
    # per the Puppet coding standards.
    l = @options.collect { |k, v| k.to_s.length }.max

    # Map options to Puppet code strings.  Symbols become unquoted
    # strings, nils become undefs, and Arrays are flattened.  Unless
    # the value is a Symbol, #inspect is called on the value.
    out += @options.sort.collect do |k, v|
      k = "#{k.to_s}#{" " * (l - k.to_s.length)}"
      v = if v.respond_to?(:flatten)
        if 1 == v.length
          v[0]
        else
          v.flatten
        end
      else
        v
      end
      v = :undef if v.nil?
      v = v.inspect.gsub(/\$/, "\\$") unless Symbol == v.class
      "\t#{tab}#{k} => #{v},"
    end

    # Close this resource.
    case style
    when :complete
      out << "\t}"
    when :partial
      out << "#{out.pop.chop};"
    when :defaults
      out << "\t}"
    end

  # Don't bother with an empty options Hash.  Go ahead and close this
  # resource.
  else
    case style
    when :complete
      out << "#{out.pop} }"
    when :partial
      out << "#{out.pop};"
    when :defaults
      out << "#{out.pop}}"
    end

  end
  out.join "\n"
end