simply_paginate 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - jruby-head
10
+ - 1.8.7
11
+ - ree
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # SimplyPaginate
2
+ [![Build Status](https://travis-ci.org/guiman/simply_paginate.png)](https://travis-ci.org/guiman/simply_paginate)
2
3
 
3
4
  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
5
 
@@ -29,7 +30,7 @@ collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
29
30
  paginator = SimplyPaginate::Paginator.new(collection)
30
31
 
31
32
  # create pages with max of 3 elements
32
- paginator.paginate 3
33
+ SimplyPaginate::Paginator.per_page 3
33
34
 
34
35
  # retrieve a certain page
35
36
  paginator[0]
@@ -45,11 +46,29 @@ paginator[0].next.next.next.previous.elements[0]
45
46
  paginator[0].next.next.next.previous.elements
46
47
  ```
47
48
 
49
+ There is also the possibility to use a DataMapper::Collection as the collection to be paginated (right out of the box, not extensions needed):
50
+
51
+ ```ruby
52
+ # I will guess you have a model called Post
53
+ # By default Paginator.per_page is 10
54
+
55
+ paginator = SimplyPaginate::Paginator.new(Post.all)
56
+
57
+ paginator[0].elements # this will return a DataMapper::Collection with the first 10 elements
58
+ ```
59
+
48
60
  ## Currently working on:
49
61
 
50
62
  * Adding support for common ORMs like: ActiveRecord, Sequel and DataMapper.
51
63
  * Testing...
52
64
 
65
+ ## Alternative branch
66
+ This branch contains a different implementation on the paging algorithm. This one creates pages on the fly and calculating offsets,
67
+ so when you call ```previous``` or ```next``` or ```[]```page object representing is created on that moment, thus saving memory.
68
+
69
+ Another modification is that the ```per_page``` value has moved up to the class, and there is no need on calling ```paginate``` anymore,
70
+ also the ```pages``` has been dropped.
71
+
53
72
  ## Release 0.0.2 features:
54
73
  * Dropping the each_slice in order to use a more memory efficient index algorithm for paging.
55
74
  * Adding more tests to the page object.
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
3
 
4
+ task :default => [:spec]
5
+
4
6
  Rake::TestTask.new :spec do |test|
5
7
  test.libs = ['lib', 'spec']
6
8
  test.verbose = true
@@ -1,3 +1,4 @@
1
1
  require "simply_paginate/version"
2
2
  require "simply_paginate/paginator"
3
3
  require "simply_paginate/page"
4
+ require "simply_paginate/not_in_range_error"
@@ -0,0 +1,3 @@
1
+ module SimplyPaginate
2
+ class NotInRangeError < StandardError; end
3
+ end
@@ -1,33 +1,29 @@
1
1
  module SimplyPaginate
2
2
  class Page
3
- attr_reader :next_page, :previous_page
3
+ attr_reader :first, :last
4
4
 
5
5
  def initialize(first, last, paginator, previous_page = nil, next_page = nil)
6
6
  @first = first
7
7
  @last = last
8
8
  @paginator = paginator
9
- @next_page = next_page
10
- @previous_page = previous_page
11
- end
12
-
13
- def current
14
- self
15
9
  end
16
10
 
17
11
  def next
18
- @next_page
12
+ first = @last + 1
13
+ last = first + Paginator.per_page - 1
14
+
15
+ (first >= @paginator.collection.count) ? nil : @paginator.current = Page.new(first, last, @paginator)
19
16
  end
20
17
 
21
18
  def previous
22
- @previous_page
23
- end
19
+ first = @first - Paginator.per_page
20
+ last = @first - 1
24
21
 
25
- def next=(v)
26
- @next_page = v
22
+ (first < 0) ? nil : @paginator.current = Page.new(first, last, @paginator)
27
23
  end
28
24
 
29
- def previous=(v)
30
- @previous_page = v
25
+ def current?
26
+ !@paginator.current.nil? && @paginator.current.first == @first && @paginator.current.last == @last
31
27
  end
32
28
 
33
29
  def elements
@@ -1,33 +1,34 @@
1
1
  module SimplyPaginate
2
2
  class Paginator
3
- attr_reader :pages, :collection
3
+ attr_reader :collection
4
+ attr_accessor :current
4
5
 
5
- def initialize(collection)
6
- @collection = collection
7
- @pages = []
8
- end
6
+ def self.per_page=(amount_per_page)
7
+ raise NotInRangeError.new("Amount per page should be greater than 0") unless amount_per_page > 0
9
8
 
10
- def paginate(limit)
11
- @pages = []
12
- first = last = 0
13
- count = limit - 1
14
- final_index = @collection.count - 1
9
+ @@per_page = amount_per_page
10
+ end
15
11
 
16
- (@collection.count.to_f / limit.to_f).ceil.times do
17
- last = (last <= final_index) ? first + count : final_index
12
+ def self.per_page
13
+ @@per_page
14
+ end
18
15
 
19
- page = Page.new(first, last, self, @pages[@pages.count - 1])
20
- @pages[@pages.count - 1].next = page unless @pages[@pages.count - 1].nil?
16
+ def initialize(collection)
17
+ @collection = collection
18
+ @current = nil
19
+ end
21
20
 
22
- @pages << page
23
- first = last + 1
24
- end
21
+ def [](pos)
22
+ first = pos * @@per_page
23
+ last = first + @@per_page - 1
25
24
 
26
- self
25
+ ((first < 0) || (first >= self.collection.count)) ? nil : Page.new(first, last, self)
27
26
  end
28
27
 
29
- def [](pos)
30
- @pages[pos]
28
+ def total_pages
29
+ (@collection.count.to_f / @@per_page.to_f).ceil
31
30
  end
31
+
32
+ Paginator.per_page = 10
32
33
  end
33
34
  end
@@ -1,3 +1,3 @@
1
1
  module SimplyPaginate
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["alvarola@gmail.com"]
11
11
  spec.description = %q{This gem is a simple paginator for Enumerable collections}
12
12
  spec.summary = %q{You enumerate it we will simply paginate it}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/guiman/simply_paginate"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
data/spec/page_spec.rb CHANGED
@@ -2,10 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe SimplyPaginate::Page do
4
4
  before do
5
- @paginator = SimplyPaginate::Paginator.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).paginate 5
5
+ SimplyPaginate::Paginator.per_page = 5
6
+
7
+ @paginator = SimplyPaginate::Paginator.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
6
8
  @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
9
+ @last_page = SimplyPaginate::Page.new(5, 9, @paginator)
9
10
 
10
11
  class SimplyPaginate::Page
11
12
  def paginator
@@ -32,27 +33,42 @@ describe SimplyPaginate::Page do
32
33
 
33
34
  describe "when having next and previous pages" do
34
35
  before do
35
- @paginator.paginate 2
36
+ SimplyPaginate::Paginator.per_page = 2
36
37
  end
37
38
 
38
39
  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]
40
+ @paginator[1].next.elements.must_equal @paginator[2].elements
41
+ @paginator[1].previous.elements.must_equal @paginator[0].elements
41
42
  end
42
43
  end
43
44
 
44
45
  describe "when having previous and no next" do
45
46
  it "must be able to go back " do
46
47
  @last_page.next.must_be_nil
47
- @last_page.previous.must_equal @first_page
48
+ @last_page.previous.elements.must_equal @first_page.elements
48
49
  end
49
50
  end
50
51
 
51
52
  describe "when having next and no previous" do
52
53
  it "must be able to go back " do
53
- @first_page.next.must_equal @last_page
54
+ @first_page.next.elements.must_equal @last_page.elements
54
55
  @first_page.previous.must_be_nil
55
56
  end
56
57
  end
57
58
 
59
+ describe "when moving to previous or next" do
60
+ it "must be setted as current when landed after next" do
61
+ @paginator[0].next
62
+ @paginator[0].current?.must_equal false
63
+ @paginator[1].current?.must_equal true
64
+ end
65
+
66
+ it "must be setted as current when landed after previous" do
67
+ @paginator[1].previous
68
+ @paginator[1].current?.must_equal false
69
+ @paginator[0].current?.must_equal true
70
+ end
71
+
72
+ end
73
+
58
74
  end
@@ -1,37 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
+
3
4
  describe SimplyPaginate::Paginator do
4
5
  before do
6
+ SimplyPaginate::Paginator.per_page = 3
5
7
  @paginator = SimplyPaginate::Paginator.new [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6
8
  end
7
9
 
8
10
  describe 'before paginating' do
9
11
  it 'must be able to paginate' do
10
- @paginator.paginate(3).pages.count.must_equal 4
11
- end
12
-
13
- it 'must not be able to retrieve a certain page' do
14
- @paginator[0].must_be_nil
15
- end
16
-
17
- it 'must not be able to retrieve all pages' do
18
- @paginator.pages.must_equal []
12
+ @paginator.total_pages.must_equal 4
19
13
  end
20
14
  end
21
15
 
22
16
  describe 'after paginating' do
23
17
  before do
24
- @paginator.paginate(5)
18
+ SimplyPaginate::Paginator.per_page = 5
25
19
 
26
20
  @first_page = SimplyPaginate::Page.new(0, 4, @paginator)
27
- @last_page = SimplyPaginate::Page.new(5, 9, @paginator, @first_page, nil)
28
- @first_page.next = @last_page
21
+ @last_page = SimplyPaginate::Page.new(5, 9, @paginator)
29
22
 
30
23
  @pages = [@first_page, @last_page]
31
24
  end
32
25
 
33
26
  it 'must be able to paginate' do
34
- @paginator.paginate(3).pages.count.must_equal 4
27
+ @paginator.total_pages.must_equal 2
35
28
  end
36
29
 
37
30
  it 'must be able to retrieve a certain page' do
@@ -41,7 +34,7 @@ describe SimplyPaginate::Paginator do
41
34
  end
42
35
 
43
36
  it 'must be able to retrieve all pages' do
44
- @paginator.pages.count 2
37
+ @paginator.total_pages.must_equal 2
45
38
 
46
39
  #test first element
47
40
  @paginator[0].elements.must_equal @first_page.elements
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.2
4
+ version: 0.0.3
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-23 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -67,11 +67,13 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
+ - .travis.yml
70
71
  - Gemfile
71
72
  - LICENSE.txt
72
73
  - README.md
73
74
  - Rakefile
74
75
  - lib/simply_paginate.rb
76
+ - lib/simply_paginate/not_in_range_error.rb
75
77
  - lib/simply_paginate/page.rb
76
78
  - lib/simply_paginate/paginator.rb
77
79
  - lib/simply_paginate/version.rb
@@ -79,7 +81,7 @@ files:
79
81
  - spec/page_spec.rb
80
82
  - spec/paginator_spec.rb
81
83
  - spec/spec_helper.rb
82
- homepage: ''
84
+ homepage: https://github.com/guiman/simply_paginate
83
85
  licenses:
84
86
  - MIT
85
87
  post_install_message: