class Object

Public Instance Methods

capture_named_plugins!() click to toggle source

Ohai plugins are defined with a class, which is a constant, and they are added to the this module. Capture any currently loaded plugins before we add any new plugins.

# File lib/chefspec/ohai.rb, line 160
def capture_named_plugins!
  @named_plugins = Ohai::NamedPlugin.constants
end
plugin_attribute(attribute) click to toggle source

To make the process of verifying the attributes a little more streamlined you can use this helper to request the attributes from the plugin itself.

The attributes are not loaded into the plugin until the plugin is run. Then the plugin will provide a top-level method that matches the first element in the ohai 'provides' String.

The remaining elements within the 'provides' are elements within the Mash that must be traversed to get to the value desired. That is done here and then returned.

# File lib/chefspec/ohai.rb, line 229
def plugin_attribute(attribute)
  begin
    plugin.run
  rescue Exception => e
    raise PluginFailedToRunError.new(plugin.name,plugin_file,e)
  end

  components = attribute.split('/')
  top_level_mash_name = components.first
  attributes = components[1..-1]

  top_level_mash = plugin.send(top_level_mash_name)

  if top_level_mash.nil? && !attributes.empty?
    raise PluginAttributeUndefinedError.new(attribute,{})
  end

  attributes.inject(top_level_mash) do |mash,child|
    begin
      mash[child]
    rescue Exception => e
      raise PluginAttributeUndefinedError.new(attribute, { top_level_mash_name => top_level_mash })
    end
  end
end
restore_named_plugins!() click to toggle source

Examine the current list of plugins loaded and compare it to what was found when we started. Remove all the plugins that have been added since then.

# File lib/chefspec/ohai.rb, line 167
def restore_named_plugins!
  diff_plugins = Ohai::NamedPlugin.constants - @named_plugins
  diff_plugins.each do |plugin|
    Ohai::NamedPlugin.send(:remove_const,plugin)
  end
end