Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 7f2cbfd13cb4ea70dfdcd45c9e1ef11456019183 1 parent c9ef3db
@ripienaar ripienaar authored
View
4 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
View
5 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,
View
4 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
View
12 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]
View
7 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 = {}
View
1  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|
Please sign in to comment.
Something went wrong with that request. Please try again.