Module: Poise::Helpers::Inversion::Provider
- Includes:
- DefinedIn
- Defined in:
- lib/poise/helpers/inversion.rb
Overview
Provider implementation for Poise::Helpers::Inversion.
Class Method Summary collapse
-
.default_inversion_attributes(node) ⇒ Array<Array<String>>
Default attribute paths to check for inversion options.
-
.default_inversion_options(node, resource) ⇒ Hash
Default options data for this provider class.
- .inversion_attribute(val = nil) ⇒ Object
-
.inversion_options(node, resource) ⇒ Hash
Compile all the different levels of inversion options together.
- .inversion_resource(val = nil) ⇒ Object
-
.provides(name = nil, opts = {}, &block) ⇒ Object
Override the normal #provides to set the inversion provider name instead of adding to the normal provider map.
-
.provides_auto?(node, resource) ⇒ Boolean
Subclass hook to provide auto-detection for providers.
-
.resolve_inversion_attribute(node) ⇒ Chef::Node::Attribute
Resolve the node attribute used as the base for inversion options for this class.
-
.resolve_inversion_provider(node, resource) ⇒ String
Resolve which provider name should be used for a resource.
Instance Method Summary collapse
-
#options ⇒ Hash
Compile all the different levels of inversion options together.
Methods included from DefinedIn
poise_defined!, poise_defined_in, poise_defined_in_cookbook
Class Method Details
.default_inversion_attributes(node) ⇒ Array<Array<String>>
Default attribute paths to check for inversion options. Based on the cookbook this class and its superclasses are defined in.
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/poise/helpers/inversion.rb', line 214 def default_inversion_attributes(node) klass = self tried = [] while klass.respond_to?(:poise_defined_in_cookbook) cookbook = klass.poise_defined_in_cookbook(node.run_context) if node[cookbook] return [cookbook] end tried << cookbook klass = klass.superclass end raise Poise::Error.new("Unable to find inversion attributes, tried: #{tried.join(', ')}") end |
.default_inversion_options(node, resource) ⇒ Hash
Default options data for this provider class.
281 282 283 |
# File 'lib/poise/helpers/inversion.rb', line 281 def (node, resource) {} end |
.inversion_attribute ⇒ Array<String> .inversion_attribute(val) ⇒ Array<String>
200 201 202 203 204 205 206 207 |
# File 'lib/poise/helpers/inversion.rb', line 200 def inversion_attribute(val=nil) if val # Coerce to an array of strings. val = Array(val).map {|name| name.to_s } @poise_inversion_attribute = val end @poise_inversion_attribute || (superclass.respond_to?(:inversion_attribute) ? superclass.inversion_attribute : nil) end |
.inversion_options(node, resource) ⇒ Hash
Compile all the different levels of inversion options together.
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 |
# File 'lib/poise/helpers/inversion.rb', line 250 def (node, resource) Mash.new.tap do |opts| attrs = resolve_inversion_attribute(node) # Cast the run state to a Mash because string vs. symbol keys. I can # at least promise poise_inversion will be a str so cut down on the # amount of data to convert. run_state = Mash.new(node.run_state.fetch('poise_inversion', {}).fetch(inversion_resource, {}))[resource.name] || {} # Class-level defaults. opts.update((node, resource)) # Resource options for all providers. opts.update(resource.) # Global provider from node attributes. opts.update(provider: attrs['provider']) if attrs['provider'] # Attribute options for all providers. opts.update(attrs['options']) if attrs['options'] # Resource options for this provider. opts.update(resource.(provides)) # Attribute options for this resource name. opts.update(attrs[resource.name]) if attrs[resource.name] # Options resource options for all providers. opts.update(run_state['*']) if run_state['*'] # Options resource options for this provider. opts.update(run_state[provides]) if run_state[provides] end end |
.inversion_resource ⇒ Symbol .inversion_resource(val) ⇒ Symbol
181 182 183 184 185 186 187 188 |
# File 'lib/poise/helpers/inversion.rb', line 181 def inversion_resource(val=nil) if val val = val.resource_name if val.is_a?(Class) Chef::Log.debug("[#{self.name}] Setting inversion resource to #{val}") @poise_inversion_resource = val.to_sym end @poise_inversion_resource || (superclass.respond_to?(:inversion_resource) ? superclass.inversion_resource : nil) end |
.provides ⇒ Symbol .provides(name, opts = {}, &block) ⇒ Symbol
Override the normal #provides to set the inversion provider name instead of adding to the normal provider map.
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/poise/helpers/inversion.rb', line 306 def provides(name=nil, opts={}, &block) if name raise Poise::Error.new("Inversion resource name not set for #{self.name}") unless inversion_resource @poise_inversion_provider = name Chef::Log.debug("[#{self.name}] Setting inversion provider name to #{name}") Poise::Helpers::Inversion.provider_map(inversion_resource).set(name.to_sym, self, opts, &block) # Set the actual Chef-level provides name for DSL dispatch. super(inversion_resource) end @poise_inversion_provider end |
.provides_auto?(node, resource) ⇒ Boolean
Subclass hook to provide auto-detection for providers.
337 338 339 |
# File 'lib/poise/helpers/inversion.rb', line 337 def provides_auto?(node, resource) false end |
.resolve_inversion_attribute(node) ⇒ Chef::Node::Attribute
Resolve the node attribute used as the base for inversion options for this class. This can be set explicitly with inversion_attribute or the default is to use the name of the cookbook the provider is defined in.
235 236 237 238 239 240 241 242 243 |
# File 'lib/poise/helpers/inversion.rb', line 235 def resolve_inversion_attribute(node) # Default to using just the name of the cookbook. attribute_names = inversion_attribute || default_inversion_attributes(node) attribute_names.inject(node) do |memo, key| memo[key] || begin raise Poise::Error.new("Attribute #{key} not set when expanding inversion attribute for #{self.name}: #{memo}") end end end |
.resolve_inversion_provider(node, resource) ⇒ String
Resolve which provider name should be used for a resource.
290 291 292 |
# File 'lib/poise/helpers/inversion.rb', line 290 def resolve_inversion_provider(node, resource) (node, resource)['provider'] || 'auto' end |
Instance Method Details
#options ⇒ Hash
Compile all the different levels of inversion options together.
165 166 167 |
# File 'lib/poise/helpers/inversion.rb', line 165 def @options ||= self.class.(node, new_resource) end |