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



105
106
107
108
109
110
111
112
# File 'lib/ohai/provides_map.rb', line 105

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) ⇒ Object

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ohai/provides_map.rb', line 70

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) ⇒ Object

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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ohai/provides_map.rb', line 93

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) ⇒ Object

gather plugins providing exactly the attributes listed



52
53
54
55
56
57
58
59
60
61
# File 'lib/ohai/provides_map.rb', line 52

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ohai/provides_map.rb', line 34

def set_providers_for(plugin, provided_attributes)
  unless plugin.kind_of?(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