Class: RelaxDB::Paginator
- Inherits:
-
Object
- Object
- RelaxDB::Paginator
- Defined in:
- lib/relaxdb/paginator.rb
Instance Attribute Summary collapse
-
#paginate_params ⇒ Object
readonly
Returns the value of attribute paginate_params.
Instance Method Summary collapse
-
#add_next_and_prev(docs, view_name, view_keys) ⇒ Object
view_keys are used to determine the params for the prev and next links.
- #add_next_and_prev_meths(docs, next_params, prev_params) ⇒ Object
- #add_q_next_and_prev(docs, view_name, view_keys) ⇒ Object
- #create_next(doc, view_keys) ⇒ Object
- #create_prev(doc, view_keys) ⇒ Object
-
#initialize(paginate_params, page_params) ⇒ Paginator
constructor
A new instance of Paginator.
- #orig_offset(view_name) ⇒ Object
- #total_doc_count(view_name) ⇒ Object
- #view_keys_to_vals(doc, view_keys) ⇒ Object
Constructor Details
#initialize(paginate_params, page_params) ⇒ Paginator
Returns a new instance of Paginator.
7 8 9 10 11 12 13 14 |
# File 'lib/relaxdb/paginator.rb', line 7 def initialize(paginate_params, page_params) @paginate_params = paginate_params @orig_paginate_params = @paginate_params.clone @page_params = page_params.is_a?(String) ? JSON.parse(page_params).to_mash : page_params # Where the magic happens - the original params are updated with the page specific params @paginate_params.update(@page_params) end |
Instance Attribute Details
#paginate_params ⇒ Object (readonly)
Returns the value of attribute paginate_params.
5 6 7 |
# File 'lib/relaxdb/paginator.rb', line 5 def paginate_params @paginate_params end |
Instance Method Details
#add_next_and_prev(docs, view_name, view_keys) ⇒ Object
view_keys are used to determine the params for the prev and next links. If a view_key is a symbol the key value will be the result of invoking the method named by the symbol on the first / last element in the result set. If a view_key is not a symbol, its value will be used directly.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/relaxdb/paginator.rb', line 40 def add_next_and_prev(docs, view_name, view_keys) unless docs.empty? no_docs = docs.size offset = docs.offset orig_offset = orig_offset(view_name) total_doc_count = total_doc_count(view_name) next_exists = !@paginate_params.order_inverted? ? (offset - orig_offset + no_docs < total_doc_count) : true next_params = next_exists ? create_next(docs.last, view_keys) : false prev_exists = @paginate_params.order_inverted? ? (offset - orig_offset + no_docs < total_doc_count) : (offset - orig_offset == 0 ? false : true) prev_params = prev_exists ? create_prev(docs.first, view_keys) : false else next_exists = prev_exists = false end add_next_and_prev_meths docs, next_params, prev_params end |
#add_next_and_prev_meths(docs, next_params, prev_params) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/relaxdb/paginator.rb', line 94 def add_next_and_prev_meths docs, next_params, prev_params docs..instance_eval do define_method(:next_params) { next_params } define_method(:next_query) { next_params ? "page_params=#{::CGI::escape(next_params.to_json)}" : false } define_method(:prev_params) { prev_params } define_method(:prev_query) { prev_params ? "page_params=#{::CGI::escape(prev_params.to_json)}" : false } end end |
#add_q_next_and_prev(docs, view_name, view_keys) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/relaxdb/paginator.rb', line 16 def add_q_next_and_prev docs, view_name, view_keys if docs.empty? next_exists = prev_exists = false else next_exists = docs.size >= @paginate_params.limit next_params = next_exists ? create_next(docs.last, view_keys) : false prev_exists = !@page_params.empty? prev_params = prev_exists ? create_prev(docs.first, view_keys) : false end add_next_and_prev_meths docs, next_params, prev_params end |
#create_next(doc, view_keys) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/relaxdb/paginator.rb', line 60 def create_next(doc, view_keys) next_key = view_keys_to_vals doc, view_keys next_key = next_key.length == 1 ? next_key[0] : next_key next_key_docid = doc._id { :startkey => next_key, :startkey_docid => next_key_docid, :descending => @orig_paginate_params.descending } end |
#create_prev(doc, view_keys) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/relaxdb/paginator.rb', line 67 def create_prev(doc, view_keys) prev_key = view_keys_to_vals doc, view_keys prev_key = prev_key.length == 1 ? prev_key[0] : prev_key prev_key_docid = doc._id prev_params = { :startkey => prev_key, :startkey_docid => prev_key_docid, :descending => !@orig_paginate_params.descending } end |
#orig_offset(view_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/relaxdb/paginator.rb', line 83 def orig_offset(view_name) if @paginate_params.order_inverted? params = {:startkey => @orig_paginate_params.endkey, :descending => !@orig_paginate_params.descending} else params = {:startkey => @orig_paginate_params.startkey, :descending => @orig_paginate_params.descending} end params[:limit] = 1 RelaxDB.rf_view(view_name, params).offset end |
#total_doc_count(view_name) ⇒ Object
30 31 32 33 |
# File 'lib/relaxdb/paginator.rb', line 30 def total_doc_count(view_name) RelaxDB.view view_name, :startkey => @orig_paginate_params.startkey, :endkey => @orig_paginate_params.endkey, :descending => @orig_paginate_params.descending, :reduce => true end |
#view_keys_to_vals(doc, view_keys) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/relaxdb/paginator.rb', line 74 def view_keys_to_vals doc, view_keys view_keys.map do |k| if k.is_a?(Symbol) then doc.send(k) elsif k.is_a?(Proc) then k.call(doc) else k end end end |