From 0b1e60f69a03e6525e1a9eefd748c59b4fc5261c Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 6 Oct 2008 19:06:21 -0500 Subject: Adding an array indexer method to Puppet::Util::Metric as requested in #1633. Signed-off-by: Luke Kanies --- lib/puppet/util/metric.rb | 9 +++++++++ spec/unit/util/metric.rb | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/puppet/util/metric.rb b/lib/puppet/util/metric.rb index 722277e33..e6d7678aa 100644 --- a/lib/puppet/util/metric.rb +++ b/lib/puppet/util/metric.rb @@ -14,6 +14,15 @@ class Puppet::Util::Metric attr_writer :basedir + # Return a specific value + def [](name) + if value = @values.find { |v| v[0] == name } + return value[2] + else + return nil + end + end + def basedir if defined? @basedir @basedir diff --git a/spec/unit/util/metric.rb b/spec/unit/util/metric.rb index 7aa04486c..3501aac90 100755 --- a/spec/unit/util/metric.rb +++ b/spec/unit/util/metric.rb @@ -75,6 +75,15 @@ describe Puppet::Util::Metric do @metric.values.should == [[:bar, "a", 10], [:foo, "b", 10]] end + it "should use an array indexer method to retrieve individual values" do + @metric.newvalue(:foo, 10) + @metric[:foo].should == 10 + end + + it "should return nil if the named value cannot be found" do + @metric[:foo].should be_nil + end + # LAK: I'm not taking the time to develop these tests right now. # I expect they should actually be extracted into a separate class # anyway. -- cgit