Class: DevStructure::Puppet::Resource

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

Overview

A Puppet resource is basically a named hash. The name is unique the Puppet catalog (which may contain any number of manifests in any number of modules). The attributes that are expected vary by the resource’s actual type. This implementation uses the class name to determine the type, so do not instantiate ‘Resource` directly.

Direct Known Subclasses

Class, Exec, File, Package

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Resource

Returns a new instance of Resource.



185
186
187
188
189
190
191
192
# File 'lib/devstructure/puppet.rb', line 185

def initialize(name, options={})
  super nil
  clear
  options.each { |k, v| self[k.to_sym] = v }
  @type = self.class.to_s.downcase[/[^:]+$/]
  @name = name && name.to_s
  @style = :complete
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



183
184
185
# File 'lib/devstructure/puppet.rb', line 183

def name
  @name
end

#styleObject

Returns the value of attribute style.



183
184
185
# File 'lib/devstructure/puppet.rb', line 183

def style
  @style
end

#typeObject (readonly)

Returns the value of attribute type.



182
183
184
# File 'lib/devstructure/puppet.rb', line 182

def type
  @type
end

Class Method Details

.[](*args) ⇒ Object

Resources that are just being listed as dependencies don’t need to have all their attributes. This method exists as syntactic sugar for declaring dependencies because it results in something that looks quite a bit like the Puppet language’s resource references.



198
199
200
201
202
203
204
# File 'lib/devstructure/puppet.rb', line 198

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

.defaults(options) ⇒ Object

In the Puppet language, a capitalized resource type can be used to provide default values for a type. ‘DevStructure::Blueprint` sets defaults to provide some inherent order to the Puppet run.



209
210
211
212
213
# File 'lib/devstructure/puppet.rb', line 209

def self.defaults(options)
  resource = self.new(nil, options)
  resource.style = :defaults
  resource
end

Instance Method Details

#inspectObject

Resources themselves can appear in attributes as dependencies. Their inspected value matches the Puppet resource reference syntax.



294
295
296
# File 'lib/devstructure/puppet.rb', line 294

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

#pretty_print(pp) ⇒ Object



297
298
299
# File 'lib/devstructure/puppet.rb', line 297

def pretty_print(pp)
  pp.text inspect
end

#to_s(tab = "") ⇒ 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`.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/devstructure/puppet.rb', line 218

def to_s(tab="")
  out = []

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

  # Handle a non-empty set of attributes in alphabetical order and
  # in compliance with the Puppet coding standards.  This is a bit
  # conservative and so uses more vertical space than absolutely
  # necessary but it also won't get obnoxiously long.
  if 0 < length

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

    # Map options to Puppet code strings.  `Symbol`s become unquoted
    # strings, `nil`s become `undef`s, and arrays are flattened.
    # Unless the value is a `Symbol`, #inspect is called on the value.
    out += 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_params}#{k} => #{v},"
    end

    # Close this resource to match the opening.
    case @style
    when :complete
      out << "#{tab}}"
    when :partial
      out << "#{out.pop.chop};"
    when :defaults
      out << "#{tab}}"
    end

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

  end
  out.join "\n"
end