taylorbarstow-simple_pagination 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 1
@@ -7,7 +7,7 @@ module SimplePagination
7
7
  ##
8
8
  # The zero-based offset of the first record in this page
9
9
  def offset
10
- (@number-1) * @collection.page_size + 1
10
+ (@number-1) * @collection.page_size
11
11
  end
12
12
 
13
13
  ##
@@ -3,8 +3,9 @@ module SimplePagination
3
3
  ##
4
4
  # Create a new PageCollection object. The following options are available.
5
5
  # [:current_offset] The zero-based offset of the first record in the current page of results
6
- # [:current_page] The one-based offset of the current page of results
7
- # [:total] The total number of results across all pages
6
+ # [:current_page] The one-based offset of the current page of results (alternative to :current_offset)
7
+ # [:total_records] The total number of results across all pages
8
+ # [:total_pages] The total number of pages (alternative to :total_records)
8
9
  # [:page_size] The page size
9
10
  #
10
11
  # You can choose between :current_offset and :current_page based on your
@@ -17,7 +18,7 @@ module SimplePagination
17
18
  def initialize(options={})
18
19
  check_options(options)
19
20
  @pages, @page_size = {}, options[:page_size]
20
- @length = options[:total_pages] || options[:total_records] / @page_size
21
+ @length = options[:total_pages] || (options[:total_records] / @page_size.to_f).ceil
21
22
  @current_page = options[:current_page] || (options[:current_offset]/@page_size)+1
22
23
  end
23
24
 
@@ -58,7 +59,7 @@ module SimplePagination
58
59
  def check_options(options)
59
60
  raise ArgumentError, "requires :current_offset or :current_page" unless options[:current_offset] || options[:current_page]
60
61
  raise ArgumentError, "requires :page_size" unless options[:page_size]
61
- raise ArgumentError, "requires :total" unless options[:total_pages] or options[:total_records]
62
+ raise ArgumentError, "requires :total_pages or :total_records" unless options[:total_pages] or options[:total_records]
62
63
  end
63
64
 
64
65
  def out_of_bounds?(i)
@@ -25,26 +25,40 @@ class PageCollectionTest < Test::Unit::TestCase
25
25
  end
26
26
  end
27
27
 
28
- context "accessors" do
29
- setup do
30
- @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
31
- end
32
-
28
+ context "page_size" do
33
29
  should "provide page_size accessor" do
30
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
34
31
  assert_equal 10, @collection.page_size
35
32
  end
36
-
37
- should "provide length accessor" do
38
- assert_equal 10, @collection.length
33
+ end
34
+
35
+ context "length" do
36
+ context "when given total_pages" do
37
+ should "be equal to total_pages" do
38
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
39
+ assert_equal 10, @collection.length
40
+ end
39
41
  end
40
42
 
41
- should "calculate length when given :total_records" do
42
- c = PageCollection.new(:total_records => 113, :page_size => 3, :current_offset => 0)
43
- assert_equal 37, c.length
43
+ context "when given total_records" do
44
+ should "be zero when total_records is zero" do
45
+ @collection = PageCollection.new(:total_records => 0, :page_size => 10, :current_offset => 0)
46
+ assert_equal 0, @collection.length
47
+ end
48
+
49
+ should "be one if total_records is less than page_size results" do
50
+ @collection = PageCollection.new(:total_records => 5, :page_size => 10, :current_offset => 0)
51
+ assert_equal 1, @collection.length
52
+ end
53
+
54
+ should "be ceil(total_records / page_size)" do
55
+ @collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0)
56
+ assert_equal 3, @collection.length
57
+ end
44
58
  end
45
59
  end
46
60
 
47
- context "accessing a specific page" do
61
+ context "accessing a numbered page" do
48
62
  setup do
49
63
  @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page=>1)
50
64
  end
@@ -69,31 +83,50 @@ class PageCollectionTest < Test::Unit::TestCase
69
83
  end
70
84
  end
71
85
 
72
- context "accessing named pages" do
73
- setup do
74
- @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page=>1)
86
+ context "current page" do
87
+ context "when given current_page" do
88
+ should "provide page with index == current_page" do
89
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 3)
90
+ @collection.expects(:[]).with(3)
91
+ @collection.current
92
+ end
75
93
  end
76
94
 
77
- should "access current page" do
78
- (1..3).each do |i|
79
- c = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page=>i)
80
- c.expects(:[]).with(i)
81
- c.current
95
+ context "when given current_offset" do
96
+ should "provide page 1 if current_offset is zero" do
97
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
98
+ @collection.expects(:[]).with(1)
99
+ @collection.current
100
+ end
101
+
102
+ should "provide page with index == floor(current_offset / page_size) + 1" do
103
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 21)
104
+ @collection.expects(:[]).with(3)
105
+ @collection.current
82
106
  end
83
107
  end
84
-
85
- should "access first page" do
108
+ end
109
+
110
+ context "first page" do
111
+ should "provide page 1" do
112
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5)
86
113
  @collection.expects(:[]).with(1)
87
114
  @collection.first
88
115
  end
116
+ end
117
+
118
+ context "last page" do
119
+ # this is not an exhaustive test but I shouldn't need to make it so since
120
+ # the tests for length above do this...
89
121
 
90
- should "access last page" do
122
+ should "provide page with index == length" do
123
+ @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5)
91
124
  @collection.expects(:[]).with(10)
92
125
  @collection.last
93
126
 
94
- c = PageCollection.new(:total_records => 113, :page_size => 3, :current_page => 1)
95
- c.expects(:[]).with(37)
96
- c.last
127
+ @collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0)
128
+ @collection.expects(:[]).with(3)
129
+ @collection.last
97
130
  end
98
131
  end
99
132
  end
@@ -19,7 +19,8 @@ class PageTest < Test::Unit::TestCase
19
19
  end
20
20
 
21
21
  should "provide accessor for first record offset" do
22
- assert_equal 41, @page.offset
22
+ assert_equal 40, @page.offset
23
+ assert_equal 0, @collection[1].offset
23
24
  end
24
25
  end
25
26
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taylorbarstow-simple_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Barstow