Class: DevStructure::Puppet::Resource
- Inherits:
-
Hash
- Object
- Hash
- DevStructure::Puppet::Resource
- 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.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#style ⇒ Object
Returns the value of attribute style.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.[](*args) ⇒ Object
A resource with only a name, typically being listed as a dependency.
-
.defaults(options) ⇒ Object
A set of defaults for a resource type.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Resource
constructor
A resource.
-
#inspect ⇒ Object
Resources themselves appear in options as dependencies so they are represented in Puppet’s reference notation.
- #pretty_print(pp) ⇒ Object
-
#to_s(tab = "") ⇒ Object
Return Puppet code for this resource.
Constructor Details
#initialize(name, options = {}) ⇒ Resource
A resource.
67 68 69 70 71 72 73 74 |
# File 'lib/devstructure/puppet.rb', line 67 def initialize(name, ={}) super nil clear .each { |k, v| self[k.to_s] = v } @type = self.class.to_s.downcase[/[^:]+$/] @name = name.to_s.downcase @style = :complete end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
64 65 66 |
# File 'lib/devstructure/puppet.rb', line 64 def name @name end |
#style ⇒ Object
Returns the value of attribute style.
64 65 66 |
# File 'lib/devstructure/puppet.rb', line 64 def style @style end |
#type ⇒ Object
Returns the value of attribute type.
64 65 66 |
# File 'lib/devstructure/puppet.rb', line 64 def type @type end |
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.
79 80 81 82 83 84 85 |
# File 'lib/devstructure/puppet.rb', line 79 def self.[](*args) if 1 == args.length self.new args[0] else args.collect { |arg| self[arg] } end end |
.defaults(options) ⇒ Object
A set of defaults for a resource type.
88 89 90 91 92 |
# File 'lib/devstructure/puppet.rb', line 88 def self.defaults() resource = self.new(nil, ) resource.style = :defaults resource end |
Instance Method Details
#inspect ⇒ Object
Resources themselves appear in options as dependencies so they are represented in Puppet’s reference notation.
170 171 172 |
# File 'lib/devstructure/puppet.rb', line 170 def inspect "#{@type.capitalize}[\"#{@name}\"]" end |
#pretty_print(pp) ⇒ Object
173 174 175 |
# File 'lib/devstructure/puppet.rb', line 173 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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/devstructure/puppet.rb', line 97 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 the options Hash. 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. Symbols become unquoted # strings, nils become undefs, 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. 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. else case @style when :complete out << "#{out.pop} }" when :partial out << "#{out.pop};" when :defaults out << "#{out.pop}}" end end out.join "\n" end |