simply_paginate 0.0.1 → 0.0.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.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SimplyPaginate
2
2
 
3
- TODO: Write a gem description
3
+ Simply paginate will do just that, give me a collection and you will be able to use a pagination logic (no extra html boilerplate or dependecies).
4
4
 
5
5
  ## Installation
6
6
 
@@ -28,7 +28,7 @@ collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
28
28
 
29
29
  paginator = SimplyPaginate::Paginator.new(collection)
30
30
 
31
- # create pages with max of 5 elements
31
+ # create pages with max of 3 elements
32
32
  paginator.paginate 3
33
33
 
34
34
  # retrieve a certain page
@@ -38,8 +38,8 @@ paginator[0]
38
38
  paginator[0].next
39
39
  paginator[0].previous
40
40
 
41
- #you might want the first element on the 2nd page
42
- paginator[0].next.next.next.previous[0]
41
+ #you might want the first element on the 3rd page
42
+ paginator[0].next.next.next.previous.elements[0]
43
43
 
44
44
  #or maybe all of them
45
45
  paginator[0].next.next.next.previous.elements
@@ -47,10 +47,13 @@ paginator[0].next.next.next.previous.elements
47
47
 
48
48
  ## Currently working on:
49
49
 
50
- * Moving from a copy algorithm to a more performant pagination using offsets.
51
50
  * Adding support for common ORMs like: ActiveRecord, Sequel and DataMapper.
52
51
  * Testing...
53
52
 
53
+ ## Release 0.0.2 features:
54
+ * Dropping the each_slice in order to use a more memory efficient index algorithm for paging.
55
+ * Adding more tests to the page object.
56
+
54
57
  ## Contributing
55
58
 
56
59
  1. Fork it
@@ -1,39 +1,37 @@
1
1
  module SimplyPaginate
2
2
  class Page
3
3
  attr_reader :next_page, :previous_page
4
-
5
- def initialize(collection, previous_page = nil, next_page = nil)
6
- @collection = collection
4
+
5
+ def initialize(first, last, paginator, previous_page = nil, next_page = nil)
6
+ @first = first
7
+ @last = last
8
+ @paginator = paginator
7
9
  @next_page = next_page
8
10
  @previous_page = previous_page
9
11
  end
10
-
11
- def [](pos)
12
- @collection[pos]
13
- end
14
-
12
+
15
13
  def current
16
14
  self
17
15
  end
18
-
16
+
19
17
  def next
20
18
  @next_page
21
19
  end
22
-
20
+
23
21
  def previous
24
22
  @previous_page
25
23
  end
26
-
24
+
27
25
  def next=(v)
28
26
  @next_page = v
29
27
  end
30
-
28
+
31
29
  def previous=(v)
32
30
  @previous_page = v
33
31
  end
34
-
32
+
35
33
  def elements
36
- @collection
34
+ @paginator.collection[@first..@last]
37
35
  end
38
36
  end
39
- end
37
+ end
@@ -1,28 +1,33 @@
1
1
  module SimplyPaginate
2
2
  class Paginator
3
- attr_reader :pages
4
-
3
+ attr_reader :pages, :collection
4
+
5
5
  def initialize(collection)
6
6
  @collection = collection
7
7
  @pages = []
8
8
  end
9
-
9
+
10
10
  def paginate(limit)
11
11
  @pages = []
12
-
13
- @collection.each_slice(limit) do |slice|
14
- page = Page.new(slice, @pages[@pages.count - 1])
15
-
12
+ first = last = 0
13
+ count = limit - 1
14
+ final_index = @collection.count - 1
15
+
16
+ (@collection.count.to_f / limit.to_f).ceil.times do
17
+ last = (last <= final_index) ? first + count : final_index
18
+
19
+ page = Page.new(first, last, self, @pages[@pages.count - 1])
16
20
  @pages[@pages.count - 1].next = page unless @pages[@pages.count - 1].nil?
17
-
21
+
18
22
  @pages << page
23
+ first = last + 1
19
24
  end
20
-
25
+
21
26
  self
22
27
  end
23
-
28
+
24
29
  def [](pos)
25
30
  @pages[pos]
26
31
  end
27
32
  end
28
- end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module SimplyPaginate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/page_spec.rb CHANGED
@@ -1,5 +1,58 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SimplyPaginate::Page do
4
+ before do
5
+ @paginator = SimplyPaginate::Paginator.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).paginate 5
6
+ @first_page = SimplyPaginate::Page.new(0, 4, @paginator)
7
+ @last_page = SimplyPaginate::Page.new(5, 9, @paginator, @first_page)
8
+ @first_page.next = @last_page
4
9
 
5
- end
10
+ class SimplyPaginate::Page
11
+ def paginator
12
+ @paginator
13
+ end
14
+ end
15
+ end
16
+
17
+ it "must always have an instance of paginator" do
18
+ @first_page.paginator.wont_be_nil
19
+ @last_page.paginator.wont_be_nil
20
+
21
+ @first_page.next.paginator.wont_be_nil
22
+ @first_page.next.previous.paginator.wont_be_nil
23
+ end
24
+
25
+ it "must be able to get the elements" do
26
+ @first_page.elements.must_equal [1, 2, 3, 4, 5]
27
+ @last_page.elements.must_equal [6, 7, 8, 9, 10]
28
+
29
+ @first_page.next.elements.must_equal [6, 7, 8, 9, 10]
30
+ @last_page.previous.elements.must_equal [1, 2, 3, 4, 5]
31
+ end
32
+
33
+ describe "when having next and previous pages" do
34
+ before do
35
+ @paginator.paginate 2
36
+ end
37
+
38
+ it "must be able to go back and foward" do
39
+ @paginator[1].next.must_equal @paginator[2]
40
+ @paginator[1].previous.must_equal @paginator[0]
41
+ end
42
+ end
43
+
44
+ describe "when having previous and no next" do
45
+ it "must be able to go back " do
46
+ @last_page.next.must_be_nil
47
+ @last_page.previous.must_equal @first_page
48
+ end
49
+ end
50
+
51
+ describe "when having next and no previous" do
52
+ it "must be able to go back " do
53
+ @first_page.next.must_equal @last_page
54
+ @first_page.previous.must_be_nil
55
+ end
56
+ end
57
+
58
+ end
@@ -4,54 +4,54 @@ describe SimplyPaginate::Paginator do
4
4
  before do
5
5
  @paginator = SimplyPaginate::Paginator.new [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6
6
  end
7
-
7
+
8
8
  describe 'before paginating' do
9
9
  it 'must be able to paginate' do
10
10
  @paginator.paginate(3).pages.count.must_equal 4
11
11
  end
12
-
12
+
13
13
  it 'must not be able to retrieve a certain page' do
14
14
  @paginator[0].must_be_nil
15
15
  end
16
-
16
+
17
17
  it 'must not be able to retrieve all pages' do
18
18
  @paginator.pages.must_equal []
19
19
  end
20
20
  end
21
-
21
+
22
22
  describe 'after paginating' do
23
23
  before do
24
24
  @paginator.paginate(5)
25
-
26
- @first_page = SimplyPaginate::Page.new([1, 2, 3, 4, 5], nil)
27
- @last_page = SimplyPaginate::Page.new([6, 7, 8, 9, 10], @first_page, nil)
25
+
26
+ @first_page = SimplyPaginate::Page.new(0, 4, @paginator)
27
+ @last_page = SimplyPaginate::Page.new(5, 9, @paginator, @first_page, nil)
28
28
  @first_page.next = @last_page
29
-
29
+
30
30
  @pages = [@first_page, @last_page]
31
31
  end
32
-
32
+
33
33
  it 'must be able to paginate' do
34
34
  @paginator.paginate(3).pages.count.must_equal 4
35
35
  end
36
-
36
+
37
37
  it 'must be able to retrieve a certain page' do
38
38
  @paginator[0].elements.must_equal @first_page.elements
39
39
  @paginator[0].previous.must_be_nil
40
40
  @paginator[0].next.wont_be_nil
41
41
  end
42
-
42
+
43
43
  it 'must be able to retrieve all pages' do
44
- @paginator.pages.count 2
45
-
44
+ @paginator.pages.count 2
45
+
46
46
  #test first element
47
47
  @paginator[0].elements.must_equal @first_page.elements
48
48
  @paginator[0].previous.must_be_nil
49
49
  @paginator[0].next.wont_be_nil
50
-
50
+
51
51
  #test second element
52
52
  @paginator[1].elements.must_equal @last_page.elements
53
53
  @paginator[1].previous.wont_be_nil
54
54
  @paginator[1].next.must_be_nil
55
55
  end
56
56
  end
57
- end
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simply_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-22 00:00:00.000000000 Z
12
+ date: 2013-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler