Module: Couch::DocExtensions
- Included in:
- Document
- Defined in:
- lib/couch/doc_view.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#count(conditions = {}) ⇒ Object
conditions might be empty, or might contain :key => required value(s) entries.
- #view(mode, *keys) ⇒ Object
Class Method Details
.condition_keys(v) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/couch/doc_view.rb', line 66 def self.condition_keys(v) case v when Range { :startkey => v.first, :endkey => v.last } else { :startkey => v, :endkey => v } end end |
Instance Method Details
#count(conditions = {}) ⇒ Object
conditions might be empty, or might contain :key => required value(s) entries
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/couch/doc_view.rb', line 52 def count(conditions = {}) case conditions.length when 0 r = view(:count, *conditions.keys).all r.inject(0) do |sum, row| sum + row[:value] end when 1 startkey = endkey = conditions.values.first r = view(:count, *conditions.keys).all(Couch::DocExtensions.condition_keys(conditions.values.first)) r.inject(0) do |sum, row| sum + row[:value] end else raise "More than one key is currently not supported" end end |
#view(mode, *keys) ⇒ Object
45 46 47 |
# File 'lib/couch/doc_view.rb', line 45 def view(mode, *keys) Couch::DocView.find_or_create(self, :mode => mode, :keys => keys) end |