paginate-simple 0.1.0 → 0.1.1

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,6 +1,77 @@
1
1
  = paginate-simple
2
+ The idea for building this gem came up when i was looking for a way to paginate my posts
3
+ in a simple sinatra blog that i was building. I needed some kind of pagination that was
4
+ not coupled with any of the existing ORM's.
5
+ The abstraction of a paginator should be simple.
2
6
 
3
- Description goes here.
7
+ It should be able to paginate some collection of data by providing a
8
+ 1. total number of items
9
+ 2. the page
10
+ 3. and number of items per page
11
+
12
+ It should be able to answer on the following
13
+ 1. How many items should i skip (offset)
14
+ 2. How many items should i get (per_page)
15
+ 3. Is there a next page and if there is give me the next page.
16
+ 4. Is there a previous page and if there is give me the previous page.
17
+ 5. It should be able to give you the first page and the last page.
18
+ 6. It should be able to provide you a way to iterate over the existing pages.
19
+
20
+ In the previous version of the gem i have created a PaginateSimple module to work as a
21
+ singleton but in this version since it should be a pagination logic i have changed that
22
+ behavior in order to provide a way to extend and change the functionality of the module
23
+ and its usage.
24
+
25
+ Usage:
26
+ In order to create a singleton pagination object we need to create a class Paginator that
27
+ will extend this module and change some of its built in logic like this:
28
+ class Paginator
29
+ extend PaginateSimple
30
+
31
+ def self.current_page(page)
32
+ super page.to_i # allow strings to be passed like "1" as well
33
+ end
34
+ end
35
+
36
+ If we need to have more than one Paginator we will include the module instead and
37
+ then instantiate the object as needed.
38
+ class Paginator
39
+ include PaginateSimple
40
+
41
+ def current_page(page)
42
+ super page.to_i # allow strings to be passed like "1" as well
43
+ end
44
+ end
45
+
46
+ @paginator = Paginator.new
47
+ @paginator.config :total => 100 , :page => 1, :per_page => 10
48
+
49
+ @another_paginator = Paginator.new
50
+ @another_paginator.config :total => 100 , :page => 1, :per_page => 3
51
+
52
+ By default no helper for the paginator exists. But to write one is quite simple.
53
+ Here is the example of a paginator helper that i use in my blog.
54
+
55
+ require 'sinatra/base'
56
+
57
+ module Sinatra
58
+ module PaginationHelper
59
+ def paginate(resource)
60
+ data = []
61
+ data << '<div class="pagination">'
62
+ data << "<a href =\"/#{resource}/?page=#{Paginator.previous_page}\">Prev</a>" if Paginator.has_previous_page?
63
+ data << ' | ' if Paginator.has_previous_page? and Paginator.has_next_page?
64
+ data << "<a href =\"/#{resource}/?page=#{Paginator.next_page}\">Next</a>" if Paginator.has_next_page?
65
+ data << '</div>'
66
+ data.join(' ')
67
+ end
68
+ end
69
+
70
+ helpers PaginationHelper
71
+ end
72
+
73
+ and i use it like this in my haml file
74
+ = paginate 'posts'
4
75
 
5
76
  == Contributing to paginate-simple
6
77
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,5 +1,4 @@
1
1
  module PaginateSimple
2
- class << self
3
2
  attr_accessor :current_page, :total, :per_page
4
3
 
5
4
  def config(args = {})
@@ -54,6 +53,5 @@ module PaginateSimple
54
53
  raise ArgumentError if total_results < 0
55
54
  @total = total_results
56
55
  end
57
-
58
- end
56
+
59
57
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{paginate-simple}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["filip"]
@@ -1,80 +1,84 @@
1
1
  require './helper'
2
2
 
3
+ class Paginator
4
+ extend PaginateSimple
5
+ end
6
+
3
7
  describe PaginateSimple do
4
8
  before do
5
- PaginateSimple.config :total => 100 , :page => 1, :per_page => 10
9
+ Paginator.config :total => 100 , :page => 1, :per_page => 10
6
10
  end
7
11
 
8
12
  it "should be configured properly" do
9
- PaginateSimple.total.should eql(100)
10
- PaginateSimple.current_page.should eql(1)
11
- PaginateSimple.per_page.should eql(10)
13
+ Paginator.total.should eql(100)
14
+ Paginator.current_page.should eql(1)
15
+ Paginator.per_page.should eql(10)
12
16
  end
13
17
 
14
18
  it "should provide the correct number of pages" do
15
- PaginateSimple.num_of_pages.should eql(10)
19
+ Paginator.num_of_pages.should eql(10)
16
20
  end
17
21
 
18
22
  it "should answer to has_next_page? correctly" do
19
- PaginateSimple.should have_next_page
20
- PaginateSimple.current_page = 10
21
- PaginateSimple.should_not have_next_page
23
+ Paginator.should have_next_page
24
+ Paginator.current_page = 10
25
+ Paginator.should_not have_next_page
22
26
  end
23
27
 
24
28
  it "should answer to has_previous_page? correctly" do
25
- PaginateSimple.should_not have_previous_page
26
- PaginateSimple.current_page = 2
27
- PaginateSimple.should have_next_page
29
+ Paginator.should_not have_previous_page
30
+ Paginator.current_page = 2
31
+ Paginator.should have_next_page
28
32
  end
29
33
 
30
34
  it "should return pages as iterator" do
31
- PaginateSimple.pages.should respond_to(:each)
32
- PaginateSimple.current_page = 1
33
- PaginateSimple.total = 0
34
- PaginateSimple.pages.should respond_to(:each)
35
+ Paginator.pages.should respond_to(:each)
36
+ Paginator.current_page = 1
37
+ Paginator.total = 0
38
+ Paginator.pages.should respond_to(:each)
35
39
  end
36
40
 
37
41
  it "should return correct num of pages" do
38
- PaginateSimple.per_page = 3
39
- PaginateSimple.total = 10
40
- PaginateSimple.should have(4).pages
42
+ Paginator.per_page = 3
43
+ Paginator.total = 10
44
+ Paginator.should have(4).pages
41
45
 
42
- PaginateSimple.current_page = 1
43
- PaginateSimple.total = 0
44
- PaginateSimple.should have(0).pages
46
+ Paginator.current_page = 1
47
+ Paginator.total = 0
48
+ Paginator.should have(0).pages
45
49
  end
46
50
 
47
51
  it "should provide offset" do
48
- PaginateSimple.current_page = 2
49
- PaginateSimple.offset.should eql(10)
52
+ Paginator.current_page = 2
53
+ Paginator.offset.should eql(10)
50
54
  end
51
55
 
52
56
  it "should return next page or nil otherwise" do
53
- PaginateSimple.current_page = 4
54
- PaginateSimple.should have_next_page
55
- PaginateSimple.next_page.should eql(5)
57
+ Paginator.current_page = 4
58
+ Paginator.should have_next_page
59
+ Paginator.next_page.should eql(5)
56
60
 
57
- PaginateSimple.current_page = 10
58
- PaginateSimple.should_not have_next_page
59
- PaginateSimple.next_page.should be_nil
61
+ Paginator.current_page = 10
62
+ Paginator.should_not have_next_page
63
+ Paginator.next_page.should be_nil
60
64
  end
61
65
 
62
66
  it "should return previous page or nil otherwise" do
63
- PaginateSimple.current_page = 2
64
- PaginateSimple.should have_previous_page
65
- PaginateSimple.previous_page.should eql(1)
67
+ Paginator.current_page = 2
68
+ Paginator.should have_previous_page
69
+ Paginator.previous_page.should eql(1)
66
70
 
67
- PaginateSimple.current_page = 1
68
- PaginateSimple.should_not have_previous_page
69
- PaginateSimple.previous_page.should be_nil
71
+ Paginator.current_page = 1
72
+ Paginator.should_not have_previous_page
73
+ Paginator.previous_page.should be_nil
70
74
  end
71
75
 
72
76
  it "should raise ArgumentError on negative current_page" do
73
- lambda { PaginateSimple.current_page = -1 }.should raise_error(ArgumentError)
77
+ lambda { Paginator.current_page = -1 }.should raise_error(ArgumentError)
74
78
  end
75
79
 
76
80
  it "should raise ArgumentError on negative total" do
77
- lambda { PaginateSimple.total = -1 }.should raise_error(ArgumentError)
81
+ lambda { Paginator.total = -1 }.should raise_error(ArgumentError)
78
82
  end
79
83
 
80
84
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: paginate-simple
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - filip
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- hash: -1273603786278675498
106
+ hash: -3688072682752233478
107
107
  segments:
108
108
  - 0
109
109
  version: "0"