diff options
author | Thom May <thom@may.lt> | 2016-11-29 18:21:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-29 18:21:47 +0000 |
commit | 3decd17bf730932e92746d8fdec7b4bc13d3d8ba (patch) | |
tree | 65da0fc289ac8d1c3e1a089389136cf934b086f0 | |
parent | d6eff109cdfabe99ee0be41cba443861bca6d851 (diff) | |
parent | 01313416df8a3b6c8f48fc71f17c4516bcdef119 (diff) | |
download | ohai-3decd17bf730932e92746d8fdec7b4bc13d3d8ba.tar.gz |
Merge pull request #822 from chef/OHAI-794/set-attribute
Extend set_attribute to set sub-attributes.
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 21 | ||||
-rw-r--r-- | spec/unit/dsl/plugin_spec.rb | 59 |
2 files changed, 71 insertions, 9 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index 1cbdbc2e..7d7456e1 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -154,8 +154,19 @@ module Ohai end end - def set_attribute(name, *values) - @data[name] = Array18(*values) + def set_attribute(name, *attrs, value) + # Initialize the path in the @data Mash with new Mashes, if needed. + # Will raise a TypeError if we hit a subattribute that is not a + # Hash, Mash, or Array. + keys = [name] + attrs + attribute = keys[0..-2].inject(@data) do |attrs, key| + attrs[key] ||= Mash.new + attrs[key] + end + + # Set the subattribute to the value. + attr_name = attrs.empty? ? name : attrs[-1] + attribute[attr_name] = value @data[name] end @@ -198,12 +209,6 @@ module Ohai # NoMethodError occurs when trying to access a key on nil nil end - - def Array18(*args) - return nil if args.empty? - return args.first if args.length == 1 - return *args - end end end end diff --git a/spec/unit/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb index 0b3c158a..a5f99920 100644 --- a/spec/unit/dsl/plugin_spec.rb +++ b/spec/unit/dsl/plugin_spec.rb @@ -90,11 +90,68 @@ shared_examples "Ohai::DSL::Plugin" do end end - context "when setting attributes" do + describe "set_attribute" do + it "raises an ArgumentError when given one argument" do + expect { plugin.set_attribute(:tea) }.to raise_error(ArgumentError) + end + it "lets you set an attribute" do plugin.set_attribute(:tea, "is soothing") expect(plugin.data["tea"]).to eql("is soothing") end + + it "lets you set an attribute to an empty array" do + plugin.set_attribute(:tea, []) + expect(plugin.data["tea"]).to eql([]) + end + + it "lets you set an attribute to an empty Mash" do + plugin.set_attribute(:tea, {}) + expect(plugin.data["tea"]).to eql({}) + end + + it "lets you modify an attribute" do + plugin.data["tea"] = "is soothing" + plugin.set_attribute(:tea, "tastes like plants") + expect(plugin.data["tea"]).to eql("tastes like plants") + end + + it "lets you set a subattribute" do + plugin.data["tea"] = Mash.new + plugin.set_attribute(:tea, :green, "tastes like green") + expect(plugin.data["tea"]["green"]).to eql("tastes like green") + end + + it "lets you set a subattribute to an empty array" do + plugin.set_attribute(:tea, :green, []) + expect(plugin.data["tea"]["green"]).to eql([]) + end + + it "lets you set a subattribute to an empty Mash" do + plugin.set_attribute(:tea, :green, {}) + expect(plugin.data["tea"]["green"]).to eql({}) + end + + it "lets you modify a subattribute" do + plugin.data["tea"] = Mash.new + plugin.data["tea"]["green"] = "tastes like green" + plugin.set_attribute(:tea, :green, "made from leaves") + expect(plugin.data["tea"]["green"]).to eql("made from leaves") + end + + it "raises a TypeError when modifying an attribute that is not a Mash" do + plugin.data["tea"] = Mash.new + plugin.data["tea"] = "is soothing" + expect { plugin.set_attribute(:tea, :green, "tastes like green") }.to raise_error(TypeError) + end + + it "lets you modify a subattribute with an array" do + green = { flavor: "tastes like green" } + black = { flavor: "tastes like black" } + plugin.data[:tea] = [green, black] + plugin.set_attribute(:tea, 0, :source, "made from leaves") + expect(plugin.data[:tea][0][:source]).to eq("made from leaves") + end end context "when getting attributes" do |