Module: Puppet::Util::ClassGen
Overview
This is a utility module for generating classes.
Constant Summary
Instance Method Summary (collapse)
-
- (Object) genconst_string(name, options)
private
Generates the constant to create or remove.
-
- (Object) genthing(name, type, options, block)
private
This does the actual work of creating our class or module.
-
- (Object) handleclassconst(klass, name, options)
private
Handle the setting and/or removing of the associated constant.
-
- (Object) initclass(klass, options)
private
Perform the initializations on the class.
-
- (Boolean) is_constant_defined?(const)
private
const_defined? in Ruby 1.9 behaves differently in terms of which class hierarchy it polls for nested namespaces.
-
- (Object) name2const(name)
private
Convert our name to a constant.
-
- (Object) storeclass(klass, klassname, options)
private
Store the class in the appropriate places.
Methods included from Puppet::Util
exit_on_fail, #exit_on_fail, which, #which
Instance Method Details
- (Object) genconst_string(name, options) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates the constant to create or remove.
88 89 90 91 92 93 94 95 |
# File 'lib/puppet/util/classgen.rb', line 88 def genconst_string(name, ) unless const = [:constant] prefix = [:prefix] || "" const = prefix + name2const(name) end const end |
- (Object) genthing(name, type, options, block) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This does the actual work of creating our class or module. It’s just a slightly abstract version of genclass.
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 |
# File 'lib/puppet/util/classgen.rb', line 100 def genthing(name, type, , block) = () name = name.to_s.downcase.intern if type == Module #evalmethod = :module_eval evalmethod = :class_eval # Create the class, with the correct name. klass = Module.new do class << self attr_reader :name end @name = name end else [:parent] ||= self evalmethod = :class_eval # Create the class, with the correct name. klass = Class.new([:parent]) do @name = name end end # Create the constant as appropriation. handleclassconst(klass, name, ) # Initialize any necessary variables. initclass(klass, ) block ||= [:block] # Evaluate the passed block if there is one. This should usually # define all of the work. klass.send(evalmethod, &block) if block klass.postinit if klass.respond_to? :postinit # Store the class in hashes or arrays or whatever. storeclass(klass, name, ) klass end |
- (Object) handleclassconst(klass, name, options) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Handle the setting and/or removing of the associated constant.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/puppet/util/classgen.rb', line 161 def handleclassconst(klass, name, ) const = genconst_string(name, ) if is_constant_defined?(const) if [:overwrite] Puppet.info "Redefining #{name} in #{self}" remove_const(const) else raise Puppet::ConstantAlreadyDefined, "Class #{const} is already defined in #{self}" end end const_set(const, klass) const end |
- (Object) initclass(klass, options) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Perform the initializations on the class.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/puppet/util/classgen.rb', line 181 def initclass(klass, ) klass.initvars if klass.respond_to? :initvars if attrs = [:attributes] attrs.each do |param, value| method = param.to_s + "=" klass.send(method, value) if klass.respond_to? method end end [:include, :extend].each do |method| if set = [method] set = [set] unless set.is_a?(Array) set.each do |mod| klass.send(method, mod) end end end klass.preinit if klass.respond_to? :preinit end |
- (Boolean) is_constant_defined?(const) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
const_defined? in Ruby 1.9 behaves differently in terms of which class hierarchy it polls for nested namespaces
See http://redmine.ruby-lang.org/issues/show/1915
150 151 152 153 154 155 156 |
# File 'lib/puppet/util/classgen.rb', line 150 def is_constant_defined?(const) if ::RUBY_VERSION =~ /1.9/ const_defined?(const, false) else const_defined?(const) end end |
- (Object) name2const(name) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert our name to a constant.
205 206 207 |
# File 'lib/puppet/util/classgen.rb', line 205 def name2const(name) name.to_s.capitalize end |
- (Object) storeclass(klass, klassname, options) (private) private
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Store the class in the appropriate places.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/puppet/util/classgen.rb', line 211 def storeclass(klass, klassname, ) if hash = [:hash] if hash.include? klassname and ! [:overwrite] raise Puppet::SubclassAlreadyDefined, "Already a generated class named #{klassname}" end hash[klassname] = klass end # If we were told to stick it in a hash, then do so if array = [:array] if (klass.respond_to? :name and array.find { |c| c.name == klassname } and ! [:overwrite]) raise Puppet::SubclassAlreadyDefined, "Already a generated class named #{klassname}" end array << klass end end |