Class: Ohai::ProvidesMap

Inherits:
Object
  • Object
show all
Defined in:
lib/ohai/provides_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProvidesMap

Returns a new instance of ProvidesMap.



31
32
33
# File 'lib/ohai/provides_map.rb', line 31

def initialize
  @map = Mash.new
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



29
30
31
# File 'lib/ohai/provides_map.rb', line 29

def map
  @map
end

Instance Method Details

#all_plugins(attribute_filter = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/ohai/provides_map.rb', line 128

def all_plugins(attribute_filter = nil)
  if attribute_filter.nil?
    collected = []
    collect_plugins_in(map, collected).uniq
  else
    deep_find_providers_for(Array(attribute_filter))
  end
end

#deep_find_providers_for(attributes) ⇒ Array

This function is used to fetch the plugins for the attributes specified in the CLI options to Ohai. It first attempts to find the plugins for the attributes or the sub attributes given. If it can’t find any, it looks for plugins that might provide the parents of a given attribute and returns the first parent found.

Parameters:

  • attributes (Array)

Returns:

  • (Array)

    plugin names



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ohai/provides_map.rb', line 87

def deep_find_providers_for(attributes)
  plugins = []
  attributes.each do |attribute|
    attrs = select_subtree(@map, attribute)

    unless attrs
      attrs = select_closest_subtree(@map, attribute)

      unless attrs
        raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'"
      end
    end

    collect_plugins_in(attrs, plugins)
  end

  plugins.uniq
end

#find_closest_providers_for(attributes) ⇒ Array

This function is used to fetch the plugins from ‘depends “languages”’ statements in plugins. It gathers plugins providing each of the attributes listed, or the plugins providing the closest parent attribute

Parameters:

  • attributes (Array)

Returns:

  • (Array)

    plugin names



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ohai/provides_map.rb', line 114

def find_closest_providers_for(attributes)
  plugins = []
  attributes.each do |attribute|
    parts = normalize_and_validate(attribute)
    raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless @map[parts[0]]

    attrs = select_closest_subtree(@map, attribute)
    raise Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: \'#{attribute}\'" unless attrs

    plugins += attrs[:_plugins]
  end
  plugins.uniq
end

#find_providers_for(attributes) ⇒ Array

gather plugins providing exactly the attributes listed

Parameters:

  • attributes (Array)

Returns:

  • (Array)

    plugin names



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ohai/provides_map.rb', line 64

def find_providers_for(attributes)
  plugins = []
  attributes.each do |attribute|
    attrs = select_subtree(@map, attribute)
    raise Ohai::Exceptions::AttributeNotFound, "No such attribute: \'#{attribute}\'" unless attrs
    raise Ohai::Exceptions::ProviderNotFound, "Cannot find plugin providing attribute: \'#{attribute}\'" unless attrs[:_plugins]

    plugins += attrs[:_plugins]
  end
  plugins.uniq
end

#set_providers_for(plugin, provided_attributes) ⇒ Object

Returns void.

Parameters:

Returns:

  • void



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ohai/provides_map.rb', line 40

def set_providers_for(plugin, provided_attributes)
  unless plugin.is_a?(Ohai::DSL::Plugin)
    raise ArgumentError, "set_providers_for only accepts Ohai Plugin classes (got: #{plugin})"
  end

  provided_attributes.each do |attribute|
    attrs = @map
    parts = normalize_and_validate(attribute)
    parts.each do |part|
      attrs[part] ||= Mash.new
      attrs = attrs[part]
    end
    attrs[:_plugins] ||= []
    attrs[:_plugins] << plugin
  end
end