Class: CouchRest::Pager
- Inherits:
-
Object
- Object
- CouchRest::Pager
- Defined in:
- lib/pager.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
-
#initialize(db) ⇒ Pager
constructor
A new instance of Pager.
- #key_reduce(view, count, firstkey = nil, lastkey = nil, &block) ⇒ Object
- #request_view(view, count = nil, startkey = nil, endkey = nil) ⇒ Object
Constructor Details
#initialize(db) ⇒ Pager
Returns a new instance of Pager.
24 25 26 |
# File 'lib/pager.rb', line 24 def initialize db @db = db end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
23 24 25 |
# File 'lib/pager.rb', line 23 def db @db end |
Instance Method Details
#key_reduce(view, count, firstkey = nil, lastkey = nil, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pager.rb', line 28 def key_reduce(view, count, firstkey = nil, lastkey = nil, &block) # start with no keys startkey = firstkey # lastprocessedkey = nil keepgoing = true while keepgoing && viewrows = request_view(view, count, startkey) startkey = viewrows.first['key'] endkey = viewrows.last['key'] if (startkey == endkey) # we need to rerequest to get a bigger page # so we know we have all the rows for that key viewrows = @db.view(view, :key => startkey)['rows'] # we need to do an offset thing to find the next startkey # otherwise we just get stuck lastdocid = viewrows.last['id'] fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :count => 2)['rows'] newendkey = fornextloop.last['key'] if (newendkey == endkey) keepgoing = false else startkey = newendkey end rows = viewrows else rows = [] for r in viewrows if (lastkey && r['key'] == lastkey) keepgoing = false break end break if (r['key'] == endkey) rows << r end startkey = endkey end grouped = rows.group_by{|r|r['key']} grouped.each do |k, rs| vs = rs.collect{|r|r['value']} yield(k,vs) end # lastprocessedkey = rows.last['key'] end end |
#request_view(view, count = nil, startkey = nil, endkey = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pager.rb', line 77 def request_view view, count = nil, startkey = nil, endkey = nil opts = {} opts[:count] = count if count opts[:startkey] = startkey if startkey opts[:endkey] = endkey if endkey results = @db.view(view, opts) rows = results['rows'] rows unless rows.length == 0 end |