Class: Chef::Resource

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

Overview

This is a generic Chef resource. One would be wise to use the helpers available in the ‘Cookbook` class.

Instance Method Summary collapse

Constructor Details

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

A Chef resource has a type, a name, and some options. The type is simply the name of the method that will be called in the recipe file. the name is the only argument to that method. The options will be converted to method calls and arguments within the block attached to each resource.



136
137
138
139
140
141
# File 'lib/devstructure/chef.rb', line 136

def initialize(type, name, options={})
  super nil
  clear
  options.each { |k, v| self[k.to_s] = v }
  @type, @name = type, name
end

Instance Method Details

#to_sObject

Stringify differently depending on the number of options so the output always looks like Ruby code should look. Parentheses are always employed here due to grammatical inconsistencies when using braces surrounding a block.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/devstructure/chef.rb', line 147

def to_s
  case length
  when 0
    "#{@type}(#{@name.inspect})\n"
  when 1
    key, value = dup.shift
    "#{@type}(#{@name.inspect}) { #{key} #{value.inspect} }\n"
  else
    out = ["#{@type}(#{@name.inspect}) do\n"]
    out += sort.collect do |key, value|
      "\t#{key} #{value.inspect}\n"
    end
    out << "end\n"
    out.join("")
  end
end