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.



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

def initialize
  @map = Mash.new
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



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

def map
  @map
end

Instance Method Details

#all_plugins(attribute_filter = nil) ⇒ Object



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

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



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

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



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

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



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

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



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

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