Created
August 8, 2011 15:36
-
-
Save codesnik/1131971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def changenode hash, keys, value | |
endkey = keys.pop | |
keys.reduce(hash) { |hash, key| | |
hash[key].tap {|h| h.is_a?(Hash) or raise("key '#{key}' is not a hash") } | |
}[endkey] = value | |
end | |
describe "changenode" do | |
let(:node) do | |
{ "foo" => | |
{ "bar" => 4, | |
"baz" => 5, | |
"deeper" => { | |
"foobar" => 3 | |
} | |
}, | |
"test" => 3 | |
} | |
end | |
it "should work for simple case" do | |
changenode node, %W[test], 5 | |
node["test"].should == 5 | |
end | |
it "should work for deeper case" do | |
changenode node, %W[foo bar], 5 | |
node["foo"]["bar"].should == 5 | |
end | |
it "should work for a very deep case" do | |
changenode node, %W[foo deeper foobar], 5 | |
node["foo"]["deeper"]["foobar"].should == 5 | |
end | |
it "should fail somehow for missing mode" do | |
expect { changenode node, %W[foo doesntexist foobar], 5 }.to raise_error | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment