summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorMarc Fournier <marc.fournier@camptocamp.com>2012-01-04 12:21:05 +0100
committerMarc Fournier <marc.fournier@camptocamp.com>2012-01-04 12:21:05 +0100
commit9418066cc806ca172c1214fb112c09ac23e41acb (patch)
tree0ab94727e5ed390c9386e55076022d16a6e87526 /lib/puppet/parser/functions
parent09a26a5df6489e068f288c4de84ae9e0d2ca5082 (diff)
couchdblookup: added case for map+reduce view.
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/couchdblookup.rb24
1 files changed, 16 insertions, 8 deletions
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 !"