Module: Poise::Helpers::Inversion::Resource

Defined in:
lib/poise/helpers/inversion.rb

Overview

Resource implementation for Poise::Helpers::Inversion.

See Also:

Since:

  • 2.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_inversion_options_resource!(name)

This method returns an undefined value.

Create resource and provider classes for an options resource.

Parameters:

  • name (String, Symbol)

    DSL name for the base resource.

Since:

  • 2.0.0



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
# File 'lib/poise/helpers/inversion.rb', line 127

def create_inversion_options_resource!(name)
  enclosing_class = self
  options_resource_name = :"#{name}_options"
  # Create the resource class.
  @inversion_options_resource_class = Class.new(Chef::Resource) do
    include Poise::Helpers::Inversion::OptionsResource
    define_singleton_method(:name) do
      "#{enclosing_class}::OptionsResource"
    end
    define_singleton_method(:inversion_resource_class) do
      enclosing_class
    end
    provides(options_resource_name)
    inversion_resource(name)
  end
  # Create the provider class.
  @inversion_options_provider_class = Class.new(Chef::Provider) do
    include Poise::Helpers::Inversion::OptionsProvider
    define_singleton_method(:name) do
      "#{enclosing_class}::OptionsProvider"
    end
    define_singleton_method(:inversion_resource_class) do
      enclosing_class
    end
    provides(options_resource_name)
  end
end

.inversion_options_provider_classObject

Options provider class.

Since:

  • 2.0.0



108
109
110
# File 'lib/poise/helpers/inversion.rb', line 108

def inversion_options_provider_class
  @inversion_options_provider_class
end

.inversion_options_resourceBoolean .inversion_options_resource(val) ⇒ Boolean

Overloads:

  • .inversion_options_resourceBoolean

    Return the options resource mode for this class.

    Returns:

    • (Boolean)
  • .inversion_options_resource(val) ⇒ Boolean

    Set the options resource mode for this class. Set to true to automatically create an options resource. Defaults to true.

    Parameters:

    • val (Boolean)

      Enable/disable setting.

    Returns:

    • (Boolean)

Since:

  • 2.0.0



118
119
120
121
# File 'lib/poise/helpers/inversion.rb', line 118

def inversion_options_resource(val=nil)
  @poise_inversion_options_resource = val unless val.nil?
  @poise_inversion_options_resource
end

.inversion_options_resource_classObject

Options resource class.

Since:

  • 2.0.0



106
107
108
# File 'lib/poise/helpers/inversion.rb', line 106

def inversion_options_resource_class
  @inversion_options_resource_class
end

.provides(name, *args, &block) ⇒ Object

Wrap #provides() to create an options resource if desired.

return [void]

Parameters:

  • name (Symbol)

    Resource name

Since:

  • 2.0.0



159
160
161
162
# File 'lib/poise/helpers/inversion.rb', line 159

def provides(name, *args, &block)
  create_inversion_options_resource!(name) if inversion_options_resource
  super(name, *args, &block) if defined?(super)
end

Instance Method Details

#options(val = nil) ⇒ Hash #options(provider, val = nil) ⇒ Hash

Overloads:

  • #options(val = nil) ⇒ Hash

    Set or return provider options for all providers.

    Examples:

    my_resource 'thing_one' do
      options depends: 'thing_two'
    end

    Parameters:

    • val (Hash) (defaults to: nil)

      Provider options to set.

    Returns:

    • (Hash)
  • #options(provider, val = nil) ⇒ Hash

    Set or return provider options for a specific provider.

    Examples:

    my_resource 'thing_one' do
      options :my_provider, depends: 'thing_two'
    end

    Parameters:

    • provider (Symbol)

      Provider to set for.

    • val (Hash) (defaults to: nil)

      Provider options to set.

    Returns:

    • (Hash)

Since:

  • 2.0.0



60
61
62
63
64
65
66
67
68
# File 'lib/poise/helpers/inversion.rb', line 60

def options(provider=nil, val=nil)
  key = :options
  if !val && provider.is_a?(Hash)
    val = provider
  elsif provider
    key = :"options_#{provider}"
  end
  set_or_return(key, val ? Mash.new(val) : val, kind_of: Hash, default: lazy { Mash.new })
end

#provider(val = nil) ⇒ Class

Allow setting the provider directly using the same names as the attribute settings.

Examples:

my_resource 'thing_one' do
  provider :my_provider
end

Parameters:

  • val (String, Symbol, Class, nil) (defaults to: nil)

    Value to set the provider to.

Returns:

  • (Class)

Since:

  • 2.0.0



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/poise/helpers/inversion.rb', line 79

def provider(val=nil)
  if val && !val.is_a?(Class)
    resource_names = [resource_name]
    # If subclass_providers! might be in play, check for those names too.
    resource_names.concat(self.class.subclass_resource_equivalents) if self.class.respond_to?(:subclass_resource_equivalents)
    # Silly ruby tricks to find the first provider that exists and no more.
    provider_class = resource_names.lazy.map {|name| Poise::Helpers::Inversion.provider_for(name, node, val) }.select {|x| x }.first
    Poise.debug("[#{self}] Checking for an inversion provider for #{val}: #{provider_class && provider_class.name}")
    val = provider_class if provider_class
  end
  super
end

#provider_no_auto(val = nil) ⇒ Array<String>

Set or return the array of provider names to be blocked from auto-resolution.

Parameters:

  • val (String, Array<String>) (defaults to: nil)

    Value to set.

Returns:

  • (Array<String>)

Since:

  • 2.0.0



97
98
99
100
101
# File 'lib/poise/helpers/inversion.rb', line 97

def provider_no_auto(val=nil)
  # Coerce to an array.
  val = Array(val).map(&:to_s) if val
  set_or_return(:provider_no_auto, val, kind_of: Array, default: [])
end