Skip to content

Commit

Permalink
Rename variables/methods for ProvidesMap to provides_map
Browse files Browse the repository at this point in the history
The name "attributes" to refer to the map of attributes<->plugins
providing attributes was confusing because it was inaccurate and often
used near a local variable named "attributes" containing a very
different kind of data (such as an Array of strings referring to
attributes another plugin depended on). Renaming to "provides_map"
clears up the confusion.
  • Loading branch information
danielsdeleo committed Dec 4, 2013
1 parent c16c504 commit 449843c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 40 deletions.
7 changes: 3 additions & 4 deletions lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ def initialize(controller, source)
@has_run = false
end

# TODO: rename
def attributes
@controller.attributes
def provides_map
@controller.provides_map
end

def run
Expand Down Expand Up @@ -231,7 +230,7 @@ def self.collect_contents(contents)
end

def provides(*paths)
attributes.set_providers_for(self, paths)
provides_map.set_providers_for(self, paths)
end

def require_plugin(*args)
Expand Down
7 changes: 5 additions & 2 deletions lib/ohai/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class Loader

def initialize(controller)
@controller = controller
@attributes = controller.attributes
end

def provides_map
@controller.provides_map
end

# @note: plugin_name is used only by version 6 plugins and is the
Expand Down Expand Up @@ -68,7 +71,7 @@ def load_plugin(plugin_path, plugin_name=nil)

def collect_provides(plugin)
plugin_provides = plugin.class.provides_attrs
@attributes.set_providers_for(plugin, plugin_provides)
provides_map.set_providers_for(plugin, plugin_provides)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/ohai/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Runner
# safe_run: set to true if this runner will run plugins in
# safe-mode. default false.
def initialize(controller, safe_run = false)
@attributes = controller.attributes
@provides_map = controller.provides_map
@safe_run = safe_run
end

Expand Down Expand Up @@ -59,7 +59,7 @@ def run_plugin(plugin, force = false)

# returns a list of plugins which provide the given attributes
def fetch_plugins(attributes)
@attributes.find_providers_for(attributes)
@provides_map.find_providers_for(attributes)
end

# given a list of plugins and the first plugin in the cycle,
Expand Down
6 changes: 3 additions & 3 deletions lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ module Ohai

class System
attr_accessor :data
attr_reader :attributes
attr_reader :provides_map
attr_reader :hints
attr_reader :v6_dependency_solver

def initialize
@data = Mash.new
@attributes = ProvidesMap.new
@provides_map = ProvidesMap.new

@hints = Hash.new
@v6_dependency_solver = Hash.new
Expand Down Expand Up @@ -95,7 +95,7 @@ def run_plugins(safe = false, force = false)
end

# collect and run version 7 plugins
plugins = @attributes.all_plugins
plugins = @provides_map.all_plugins

begin
plugins.each { |plugin| @runner.run_plugin(plugin, force) }
Expand Down
9 changes: 4 additions & 5 deletions spec/ohai/dsl/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,23 @@
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
plugin.provides("attribute")

#@ohai.attributes.should have_key(:attribute)
@ohai.attributes.find_providers_for(["attribute"]).should eq([plugin])
@ohai.provides_map.find_providers_for(["attribute"]).should eq([plugin])
end

it "should collect a list of attributes" do
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
plugin.provides("attr1", "attr2", "attr3")

%w[attr1 attr2 attr3].each do |attr|
@ohai.attributes.find_providers_for([attr]).should eq([plugin])
@ohai.provides_map.find_providers_for([attr]).should eq([plugin])
end
end

it "should collect subattributes of an attribute" do
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
plugin.provides("attr/subattr")

@ohai.attributes.find_providers_for(["attr/subattr"]).should eq([plugin])
@ohai.provides_map.find_providers_for(["attr/subattr"]).should eq([plugin])
end

it "should collect all unique providers for an attribute" do
Expand All @@ -299,7 +298,7 @@
plugins << p
end

@ohai.attributes.find_providers_for(["attribute"]).should =~ plugins
@ohai.provides_map.find_providers_for(["attribute"]).should =~ plugins
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@
klass = Ohai.plugin(@name) { provides("attr") }
plugin = klass.new(@ohai, @path)
@loader.collect_provides(plugin)
@ohai.attributes.find_providers_for(["attr"]).should eq([plugin])
@ohai.provides_map.find_providers_for(["attr"]).should eq([plugin])
end

it "should add provided subattributes to Ohai" do
klass = Ohai.plugin(@name) { provides("attr/sub") }
plugin = klass.new(@ohai, @plath)
@loader.collect_provides(plugin)
@ohai.attributes.find_providers_for([ "attr/sub" ]).should include(plugin)
@ohai.provides_map.find_providers_for([ "attr/sub" ]).should include(plugin)
end

it "should collect the unique providers for an attribute" do
Expand All @@ -125,7 +125,7 @@
end

plugins.each { |plugin| @loader.collect_provides(plugin) }
@ohai.attributes.find_providers_for(["attr"]).should =~ plugins
@ohai.provides_map.find_providers_for(["attr"]).should =~ plugins
end
end
end
6 changes: 3 additions & 3 deletions spec/unit/plugins/fail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
it "should not have attribute keys" do
@loader.load_plugin("#{tmp}/plugins/fail.rb")
#@ohai.attributes.should_not have_key("fail")
lambda { @ohai.attributes.find_providers_for(["fail"]) }.should raise_error(Ohai::Exceptions::AttributeNotFound)
lambda { @ohai.provides_map.find_providers_for(["fail"]) }.should raise_error(Ohai::Exceptions::AttributeNotFound)
end

it "should not have source key" do
Expand Down Expand Up @@ -104,7 +104,7 @@

it "should have attribute keys" do
@loader.load_plugin("#{tmp}/plugins/fail.rb")
@ohai.attributes.should have_key("fail")
@ohai.provides_map.should have_key("fail")
end

it "should have source key" do
Expand Down Expand Up @@ -152,7 +152,7 @@

it "should not have new attribute keys" do
@loader.load_plugin("#{tmp}/plugins/fail.rb").new(@ohai).run
@ohai.attributes.should_not have_key("other")
@ohai.provides_map.should_not have_key("other")
end

it "should write to Ohai::Log" do
Expand Down
24 changes: 12 additions & 12 deletions spec/unit/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
end
@plugin1, @plugin2 = @plugins

@ohai.attributes.set_providers_for(@plugin1, ["thing"])
@ohai.provides_map.set_providers_for(@plugin1, ["thing"])
end

it "should run the plugins" do
Expand Down Expand Up @@ -134,8 +134,8 @@
end
@plugin1, @plugin2, @plugin3 = @plugins

@ohai.attributes.set_providers_for(@plugin1, ["thing"])
@ohai.attributes.set_providers_for(@plugin2, ["thing"])
@ohai.provides_map.set_providers_for(@plugin1, ["thing"])
@ohai.provides_map.set_providers_for(@plugin2, ["thing"])
end

it "should run the plugins" do
Expand Down Expand Up @@ -177,8 +177,8 @@
@plugins << klass.new(@ohai, "/tmp/plugins/number.rb")
end
@plugin1, @plugin2, @plugin3 = @plugins
@ohai.attributes.set_providers_for(@plugin1, ["one", "two"])
@ohai.attributes.set_providers_for(@plugin2, ["one", "two"])
@ohai.provides_map.set_providers_for(@plugin1, ["one", "two"])
@ohai.provides_map.set_providers_for(@plugin2, ["one", "two"])
end

it "should run the plugins" do
Expand Down Expand Up @@ -251,9 +251,9 @@
end

it "should not detect a cycle when B is the first provider returned" do
@ohai.attributes.set_providers_for(@pluginA, ["A"])
@ohai.attributes.set_providers_for(@pluginB, ["B"])
@ohai.attributes.set_providers_for(@pluginC, ["C"])
@ohai.provides_map.set_providers_for(@pluginA, ["A"])
@ohai.provides_map.set_providers_for(@pluginB, ["B"])
@ohai.provides_map.set_providers_for(@pluginC, ["C"])

Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
@runner.run_plugin(@pluginA)
Expand All @@ -264,9 +264,9 @@
end

it "should not detect a cycle when C is the first provider returned" do
@ohai.attributes.set_providers_for(@pluginA, ["A"])
@ohai.attributes.set_providers_for(@pluginC, ["C"])
@ohai.attributes.set_providers_for(@pluginB, ["B"])
@ohai.provides_map.set_providers_for(@pluginA, ["A"])
@ohai.provides_map.set_providers_for(@pluginC, ["C"])
@ohai.provides_map.set_providers_for(@pluginB, ["B"])

Ohai::Log.should_not_receive(:error).with(/DependencyCycleError/)
@runner.run_plugin(@pluginA)
Expand All @@ -287,7 +287,7 @@

it "should collect the provider" do
plugin = Ohai::DSL::Plugin.new(@ohai, "")
@ohai.attributes.set_providers_for(plugin, ["top/middle/bottom"])
@ohai.provides_map.set_providers_for(plugin, ["top/middle/bottom"])

dependency_providers = @runner.fetch_plugins(["top/middle/bottom"])
dependency_providers.should eql([plugin])
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
end

it "should set @attributes to a ProvidesMap" do
@ohai.attributes.should be_a_kind_of(Ohai::ProvidesMap)
@ohai.provides_map.should be_a_kind_of(Ohai::ProvidesMap)
end

it "should set @v6_dependency_solver to a Hash" do
Expand Down Expand Up @@ -128,7 +128,7 @@
@ohai = Ohai::System.new
klass = Ohai.plugin(:Empty) { }
plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
@ohai.attributes.should_receive(:all_plugins).and_return([plugin])
@ohai.provides_map.should_receive(:all_plugins).and_return([plugin])
end

describe "when AttributeNotFound is received" do
Expand Down Expand Up @@ -165,7 +165,7 @@
@plugins << klass.new(@ohai, "")
end

@ohai.attributes.should_receive(:all_plugins).and_return(@plugins)
@ohai.provides_map.should_receive(:all_plugins).and_return(@plugins)
end

it "should run each plugin once from Ohai::System" do
Expand Down Expand Up @@ -254,7 +254,7 @@
end

it "should find all the plugins providing attributes" do
provides_map = @ohai.attributes
provides_map = @ohai.provides_map
provides_map.set_providers_for(@plugins[0], ["zero"])
provides_map.set_providers_for(@plugins[1], ["one"])
provides_map.set_providers_for(@plugins[2], ["two"])
Expand Down Expand Up @@ -349,7 +349,7 @@

@ohai.v6_dependency_solver['v6plugin'] = @v6plugin
@ohai.v6_dependency_solver['v7plugin'] = @v7plugin
@ohai.attributes.set_providers_for(@v7plugin, ["message"])
@ohai.provides_map.set_providers_for(@v7plugin, ["message"])
end

it "should run the plugin it requires" do
Expand Down Expand Up @@ -394,7 +394,7 @@
vds['v7plugin'] = @v7plugin
vds['other'] = @other

dependency_map = @ohai.attributes
dependency_map = @ohai.provides_map
#dependency_map[:message][:_plugins] = [@v7plugin]
dependency_map.set_providers_for(@v7plugin, ["message"])
#dependency_map[:other][:_plugins] = [@other]
Expand Down

0 comments on commit 449843c

Please sign in to comment.