From 9418066cc806ca172c1214fb112c09ac23e41acb Mon Sep 17 00:00:00 2001 From: Marc Fournier Date: Wed, 4 Jan 2012 12:21:05 +0100 Subject: couchdblookup: added case for map+reduce view. --- lib/puppet/parser/functions/couchdblookup.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/puppet/parser/functions/couchdblookup.rb b/lib/puppet/parser/functions/couchdblookup.rb index 2e1545e..8c4bc21 100644 --- a/lib/puppet/parser/functions/couchdblookup.rb +++ b/lib/puppet/parser/functions/couchdblookup.rb @@ -20,15 +20,23 @@ module Puppet::Parser::Functions end result = nil - if json.has_key?("rows") and json['total_rows'] > 0 and json['rows'][0].has_key?(key) - result = Array.new - json['rows'].each do |x| - result.push(x[key]) - end - else - if json.has_key?(key) - result = json[key] + + if json.has_key?("rows") + + if json['rows'].length > 1 + arr = json['rows'].collect do |x| + x[key] if x.is_a?(Hash) and x.has_key?(key) + end + arr.compact! + result = arr unless arr.empty? + + elsif json['rows'].length == 1 + hash = json['rows'].pop + result = hash[key] if hash.is_a?(Hash) end + + elsif json.has_key?(key) + result = json[key] end result or raise Puppet::ParseError, "couchdblookup(): key '#{key}' not found in JSON object !" -- cgit v1.2.3