Skip to content

Instantly share code, notes, and snippets.

@bcm
Created August 14, 2012 21:30
Show Gist options
  • Save bcm/3353209 to your computer and use it in GitHub Desktop.
Save bcm/3353209 to your computer and use it in GitHub Desktop.
Balanced pagination metadata
diff --git a/lib/balanced/pager.rb b/lib/balanced/pager.rb
index 2338d0e..9150592 100644
--- a/lib/balanced/pager.rb
+++ b/lib/balanced/pager.rb
@@ -34,17 +34,29 @@ module Balanced
load! unless @page
@page[:limit]
end
+ alias limit_value limit
def offset
load! unless @page
@page[:offset]
end
+ alias offset_value offset
def items
load! unless @page
@page[:items]
end
+ def current_page
+ (offset / limit) + 1
+ end
+
+ def num_pages
+ num = total / limit
+ num += 1 if total % limit > 0
+ num
+ end
+
# @return [Array] Iterates through the current page of records.
# @yield [record]
def each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment