simply_paginate 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +8 -0
- data/lib/simply_paginate.rb +3 -0
- data/lib/simply_paginate/page.rb +39 -0
- data/lib/simply_paginate/paginator.rb +28 -0
- data/lib/simply_paginate/version.rb +3 -0
- data/simply_paginate.gemspec +24 -0
- data/spec/page_spec.rb +5 -0
- data/spec/paginator_spec.rb +57 -0
- data/spec/spec_helper.rb +3 -0
- metadata +111 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Álvaro Lara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# SimplyPaginate
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simply_paginate'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simply_paginate
|
18
|
+
|
19
|
+
## Motivation
|
20
|
+
When dealing with pagination most known gems come bundled with lots of extra functionality I usually don't need (like html boilerplate), this is my attempt to create just the pagination logic, as simple as that.
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'simply_paginate'
|
26
|
+
|
27
|
+
collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
28
|
+
|
29
|
+
paginator = SimplyPaginate::Paginator.new(collection)
|
30
|
+
|
31
|
+
# create pages with max of 5 elements
|
32
|
+
paginator.paginate 3
|
33
|
+
|
34
|
+
# retrieve a certain page
|
35
|
+
paginator[0]
|
36
|
+
|
37
|
+
#move through the pages freely
|
38
|
+
paginator[0].next
|
39
|
+
paginator[0].previous
|
40
|
+
|
41
|
+
#you might want the first element on the 2nd page
|
42
|
+
paginator[0].next.next.next.previous[0]
|
43
|
+
|
44
|
+
#or maybe all of them
|
45
|
+
paginator[0].next.next.next.previous.elements
|
46
|
+
```
|
47
|
+
|
48
|
+
## Currently working on:
|
49
|
+
|
50
|
+
* Moving from a copy algorithm to a more performant pagination using offsets.
|
51
|
+
* Adding support for common ORMs like: ActiveRecord, Sequel and DataMapper.
|
52
|
+
* Testing...
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module SimplyPaginate
|
2
|
+
class Page
|
3
|
+
attr_reader :next_page, :previous_page
|
4
|
+
|
5
|
+
def initialize(collection, previous_page = nil, next_page = nil)
|
6
|
+
@collection = collection
|
7
|
+
@next_page = next_page
|
8
|
+
@previous_page = previous_page
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](pos)
|
12
|
+
@collection[pos]
|
13
|
+
end
|
14
|
+
|
15
|
+
def current
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def next
|
20
|
+
@next_page
|
21
|
+
end
|
22
|
+
|
23
|
+
def previous
|
24
|
+
@previous_page
|
25
|
+
end
|
26
|
+
|
27
|
+
def next=(v)
|
28
|
+
@next_page = v
|
29
|
+
end
|
30
|
+
|
31
|
+
def previous=(v)
|
32
|
+
@previous_page = v
|
33
|
+
end
|
34
|
+
|
35
|
+
def elements
|
36
|
+
@collection
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SimplyPaginate
|
2
|
+
class Paginator
|
3
|
+
attr_reader :pages
|
4
|
+
|
5
|
+
def initialize(collection)
|
6
|
+
@collection = collection
|
7
|
+
@pages = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def paginate(limit)
|
11
|
+
@pages = []
|
12
|
+
|
13
|
+
@collection.each_slice(limit) do |slice|
|
14
|
+
page = Page.new(slice, @pages[@pages.count - 1])
|
15
|
+
|
16
|
+
@pages[@pages.count - 1].next = page unless @pages[@pages.count - 1].nil?
|
17
|
+
|
18
|
+
@pages << page
|
19
|
+
end
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def [](pos)
|
25
|
+
@pages[pos]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simply_paginate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simply_paginate"
|
8
|
+
spec.version = SimplyPaginate::VERSION
|
9
|
+
spec.authors = ["Álvaro F. Lara"]
|
10
|
+
spec.email = ["alvarola@gmail.com"]
|
11
|
+
spec.description = %q{This gem is a simple paginator for Enumerable collections}
|
12
|
+
spec.summary = %q{You enumerate it we will simply paginate it}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "minitest"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
data/spec/page_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimplyPaginate::Paginator do
|
4
|
+
before do
|
5
|
+
@paginator = SimplyPaginate::Paginator.new [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'before paginating' do
|
9
|
+
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 []
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'after paginating' do
|
23
|
+
before do
|
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)
|
28
|
+
@first_page.next = @last_page
|
29
|
+
|
30
|
+
@pages = [@first_page, @last_page]
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'must be able to paginate' do
|
34
|
+
@paginator.paginate(3).pages.count.must_equal 4
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'must be able to retrieve a certain page' do
|
38
|
+
@paginator[0].elements.must_equal @first_page.elements
|
39
|
+
@paginator[0].previous.must_be_nil
|
40
|
+
@paginator[0].next.wont_be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'must be able to retrieve all pages' do
|
44
|
+
@paginator.pages.count 2
|
45
|
+
|
46
|
+
#test first element
|
47
|
+
@paginator[0].elements.must_equal @first_page.elements
|
48
|
+
@paginator[0].previous.must_be_nil
|
49
|
+
@paginator[0].next.wont_be_nil
|
50
|
+
|
51
|
+
#test second element
|
52
|
+
@paginator[1].elements.must_equal @last_page.elements
|
53
|
+
@paginator[1].previous.wont_be_nil
|
54
|
+
@paginator[1].next.must_be_nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simply_paginate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Álvaro F. Lara
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: This gem is a simple paginator for Enumerable collections
|
63
|
+
email:
|
64
|
+
- alvarola@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/simply_paginate.rb
|
75
|
+
- lib/simply_paginate/page.rb
|
76
|
+
- lib/simply_paginate/paginator.rb
|
77
|
+
- lib/simply_paginate/version.rb
|
78
|
+
- simply_paginate.gemspec
|
79
|
+
- spec/page_spec.rb
|
80
|
+
- spec/paginator_spec.rb
|
81
|
+
- spec/spec_helper.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.23
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: You enumerate it we will simply paginate it
|
107
|
+
test_files:
|
108
|
+
- spec/page_spec.rb
|
109
|
+
- spec/paginator_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
has_rdoc:
|