From 7f2cbfd13cb4ea70dfdcd45c9e1ef11456019183 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Tue, 4 Sep 2012 10:42:34 +0100 Subject: [PATCH] 16222 - Add fetch function for request object in agents This commit adds a fetch method to both the request and result objects that will help users create more deterministic code in the face of missing data --- lib/mcollective/rpc/reply.rb | 4 ++++ lib/mcollective/rpc/request.rb | 5 +++++ lib/mcollective/rpc/result.rb | 4 ++++ spec/unit/rpc/request_spec.rb | 12 ++++++++++++ spec/unit/rpc/result_spec.rb | 7 +++++++ website/changelog.md | 1 + 6 files changed, 33 insertions(+) diff --git a/lib/mcollective/rpc/reply.rb b/lib/mcollective/rpc/reply.rb index 328e9327..574be55e 100644 --- a/lib/mcollective/rpc/reply.rb +++ b/lib/mcollective/rpc/reply.rb @@ -69,6 +69,10 @@ def [](key) @data[key] end + def fetch(key, default) + @data.fetch(key, default) + end + # Returns a compliant Hash of the reply that should be sent # over the middleware def to_hash diff --git a/lib/mcollective/rpc/request.rb b/lib/mcollective/rpc/request.rb index 443ce9e7..52439787 100644 --- a/lib/mcollective/rpc/request.rb +++ b/lib/mcollective/rpc/request.rb @@ -36,6 +36,11 @@ def [](key) return @data[key] end + def fetch(key, default) + return nil unless @data.is_a?(Hash) + return @data.fetch(key, default) + end + def to_hash return {:agent => @agent, :action => @action, diff --git a/lib/mcollective/rpc/result.rb b/lib/mcollective/rpc/result.rb index 86052423..291fd69c 100644 --- a/lib/mcollective/rpc/result.rb +++ b/lib/mcollective/rpc/result.rb @@ -24,6 +24,10 @@ def []=(idx, item) @results[idx] = item end + def fetch(key, default) + @results.fetch(key, default) + end + def each @results.each_pair {|k,v| yield(k,v) } end diff --git a/spec/unit/rpc/request_spec.rb b/spec/unit/rpc/request_spec.rb index 0245f1e3..a121abae 100755 --- a/spec/unit/rpc/request_spec.rb +++ b/spec/unit/rpc/request_spec.rb @@ -100,6 +100,18 @@ module RPC end end + describe "#fetch" do + it "should return nil for non hash data" do + @req[:body][:data] = "foo" + Request.new(@req, @ddl)["foo"].should == nil + end + + it "should fetch data with the correct default behavior" do + @request.fetch(:foo, "default").should == "bar" + @request.fetch(:rspec, "default").should == "default" + end + end + describe "#to_hash" do it "should have the correct keys" do @request.to_hash.keys.sort.should == [:action, :agent, :data] diff --git a/spec/unit/rpc/result_spec.rb b/spec/unit/rpc/result_spec.rb index b3066786..22c0b414 100755 --- a/spec/unit/rpc/result_spec.rb +++ b/spec/unit/rpc/result_spec.rb @@ -44,6 +44,13 @@ module RPC end end + describe "#fetch" do + it "should fetch data with the correct default behavior" do + @result.fetch(:foo, "default").should == "bar" + @result.fetch(:rspec, "default").should == "default" + end + end + describe "#each" do it "should itterate all the pairs" do data = {} diff --git a/website/changelog.md b/website/changelog.md index 748db41f..931dc6d8 100644 --- a/website/changelog.md +++ b/website/changelog.md @@ -8,6 +8,7 @@ toc: false |Date|Description|Ticket| |----|-----------|------| +|2012/09/04|Add a fetch method that mimic Hash#fetch to RPC Results and Requests|16222| |2012/09/04|Include the required mcollective version in packages that include the requirement|16173| |2012/08/29|Add a RabbitMQ specific connector plugin|16168| |2012/08/22|DDL files can now specify which is the minimal version of mcollective they require|15850|