page_clip 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 855d3260600eacab9c0cdc86cdb2e4902a2f2716
4
+ data.tar.gz: c836d76d3213776f3d068b51816483dd25311fcf
5
+ SHA512:
6
+ metadata.gz: d5736b8475dda63cb9cf11b6e4bcaff09c3a5067ecd66109f194f0e3ca73175859b4cf44dff1385022d833b346a1de4debd7a6ec06371e564868c12bcba8ba16
7
+ data.tar.gz: 84d3cd93b851075720705ce74bb71e724ff0c081b2ade82971149ea078383de90df5cd1b8a454b44a706aec495c99e989e0eeaec1329611900343dbe4904b50a
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ coverage/
@@ -0,0 +1,14 @@
1
+ cs/AbcSize:
2
+ Max: 30
3
+
4
+ Metrics/LineLength:
5
+ Max: 100
6
+
7
+ Metrics/MethodLength:
8
+ Max: 15
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Style/RaiseArgs:
14
+ EnforcedStyle: compact
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright © 2015 Mert Guldur
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.
@@ -0,0 +1,32 @@
1
+ # page_clip
2
+
3
+ [![Build Status](https://travis-ci.org/mertguldur/page_clip.svg?branch=master)](https://travis-ci.org/mertguldur/page_clip)
4
+ [![Coverage Status](https://coveralls.io/repos/mertguldur/page_clip/badge.svg?branch=master&service=github)](https://coveralls.io/github/mertguldur/page_clip?branch=master)
5
+
6
+ Page clip calculates the pages to display in a paginator based on item count, items per page, current page and number of pages to display.
7
+
8
+ ## Installation
9
+
10
+ ```ruby
11
+ gem 'page_clip'
12
+ ```
13
+
14
+ ## Examples
15
+
16
+ ```ruby
17
+ pages, last_page = PageClip.show \
18
+ item_count: 100,
19
+ per_page: 10,
20
+ current_page: 1,
21
+ pages_to_show_count: 5
22
+ pages # => [1, 2, 3, 4, 5]
23
+ last_page # => 10
24
+
25
+ pages, last_page = PageClip.show \
26
+ item_count: 100,
27
+ per_page: 20,
28
+ current_page: 3,
29
+ pages_to_show_count: 3
30
+ pages # => [2, 3, 4]
31
+ last_page # => 5
32
+ ```
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RuboCop::RakeTask.new
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:rubocop, :spec]
@@ -0,0 +1,73 @@
1
+ require 'set'
2
+
3
+ class PageClip
4
+ def initialize(item_count, per_page, current_page, pages_to_show_count)
5
+ verify_arguments(item_count, per_page, current_page, pages_to_show_count)
6
+
7
+ @current_page = current_page
8
+ @page_count = (item_count.to_f / per_page.to_f).ceil
9
+ @pages = (1..@page_count).to_set
10
+ @side_pages_to_show_count = (pages_to_show_count - 1) / 2
11
+ end
12
+
13
+ def self.show(item_count:, per_page:, current_page:, pages_to_show_count:)
14
+ new(item_count, per_page, current_page, pages_to_show_count).calculate
15
+ end
16
+
17
+ def calculate
18
+ [pages_to_show, @page_count]
19
+ end
20
+
21
+ private
22
+
23
+ def pages_to_show
24
+ return [] if @page_count.zero?
25
+
26
+ verify_current_page
27
+
28
+ pages = [@current_page]
29
+
30
+ pages_to_add, left_missed_page_count = side_pages_to_add(:left)
31
+ pages += pages_to_add
32
+
33
+ if left_missed_page_count > 0
34
+ pages += side_pages_to_add(:right, left_missed_page_count).first
35
+ return pages.sort
36
+ end
37
+
38
+ pages_to_add, right_missed_page_count = side_pages_to_add(:right)
39
+ pages += pages_to_add
40
+
41
+ if right_missed_page_count > 0
42
+ pages += side_pages_to_add(:left, right_missed_page_count).first
43
+ end
44
+
45
+ pages.uniq.sort
46
+ end
47
+
48
+ def side_pages_to_add(direction, extra_page_count = 0)
49
+ pages = []
50
+ page = @current_page
51
+ pages_to_add_count = @side_pages_to_show_count + extra_page_count
52
+
53
+ pages_to_add_count.times do
54
+ page = direction == :left ? page - 1 : page + 1
55
+ pages << page if @pages.include?(page)
56
+ end
57
+
58
+ missed_page_count = pages_to_add_count - pages.size
59
+ [pages, missed_page_count]
60
+ end
61
+
62
+ def verify_arguments(item_count, per_page, current_page, pages_to_show_count)
63
+ fail "Item count can't be negative!" if item_count < 0
64
+ fail 'Per page must be positive!' if per_page <= 0
65
+ fail 'Current page must be positive!' if current_page <= 0
66
+ fail 'Pages to show count must be positive!' if pages_to_show_count <= 0
67
+ fail 'Pages to show count must be an odd number!' if pages_to_show_count.even?
68
+ end
69
+
70
+ def verify_current_page
71
+ fail "Current page can't be greater than the last page!" if @current_page > @page_count
72
+ end
73
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'page_clip'
3
+ s.version = '1.0.0'
4
+ s.date = '2015-08-18'
5
+ s.summary = 'Pagination calculator'
6
+ s.description = 'Calculates pages to display in a paginator'
7
+ s.authors = ['Mert Guldur']
8
+ s.email = 'mertguldur@gmail.com'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.license = 'MIT'
11
+
12
+ s.add_development_dependency 'rake'
13
+ s.add_development_dependency 'rspec'
14
+ s.add_development_dependency 'simplecov'
15
+ s.add_development_dependency 'coveralls'
16
+ s.add_development_dependency 'rubocop'
17
+ end
@@ -0,0 +1,279 @@
1
+ require 'spec_helper'
2
+
3
+ describe PageClip do
4
+ describe '.show' do
5
+ context 'item count' do
6
+ context '0 items, 10 per page, page 1, 5 pages to show' do
7
+ it 'returns pages: [1], last_page: 1' do
8
+ pages, last_page = described_class.show \
9
+ item_count: 0,
10
+ per_page: 10,
11
+ current_page: 1,
12
+ pages_to_show_count: 5
13
+ expect(pages).to eq([])
14
+ expect(last_page).to eq(0)
15
+ end
16
+ end
17
+
18
+ context '10 items, 10 per page, page 1, 5 pages to show' do
19
+ it 'returns pages: [1], last_page: 1' do
20
+ pages, last_page = described_class.show \
21
+ item_count: 10,
22
+ per_page: 10,
23
+ current_page: 1,
24
+ pages_to_show_count: 5
25
+ expect(pages).to eq([1])
26
+ expect(last_page).to eq(1)
27
+ end
28
+ end
29
+
30
+ context '20 items, 10 per page, page 1, 5 pages to show' do
31
+ it 'returns pages: [1, 2], last_page: 2' do
32
+ pages, last_page = described_class.show \
33
+ item_count: 20,
34
+ per_page: 10,
35
+ current_page: 1,
36
+ pages_to_show_count: 5
37
+ expect(pages).to eq([1, 2])
38
+ expect(last_page).to eq(2)
39
+ end
40
+ end
41
+
42
+ context '50 items, 10 per page, page 1, 5 pages to show' do
43
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 5' do
44
+ pages, last_page = described_class.show \
45
+ item_count: 50,
46
+ per_page: 10,
47
+ current_page: 1,
48
+ pages_to_show_count: 5
49
+ expect(pages).to eq([1, 2, 3, 4, 5])
50
+ expect(last_page).to eq(5)
51
+ end
52
+ end
53
+
54
+ context '60 items, 10 per page, page 1, 5 pages to show' do
55
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 6' do
56
+ pages, last_page = described_class.show \
57
+ item_count: 60,
58
+ per_page: 10,
59
+ current_page: 1,
60
+ pages_to_show_count: 5
61
+ expect(pages).to eq([1, 2, 3, 4, 5])
62
+ expect(last_page).to eq(6)
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'per page' do
68
+ context '50 items, 10 per page, page 1, 5 pages to show' do
69
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 5' do
70
+ pages, last_page = described_class.show \
71
+ item_count: 50,
72
+ per_page: 10,
73
+ current_page: 1,
74
+ pages_to_show_count: 5
75
+ expect(pages).to eq([1, 2, 3, 4, 5])
76
+ expect(last_page).to eq(5)
77
+ end
78
+ end
79
+
80
+ context '50 items, 25 per page, page 1, 5 pages to show' do
81
+ it 'returns pages: [1, 2], last_page: 2' do
82
+ pages, last_page = described_class.show \
83
+ item_count: 50,
84
+ per_page: 25,
85
+ current_page: 1,
86
+ pages_to_show_count: 5
87
+ expect(pages).to eq([1, 2])
88
+ expect(last_page).to eq(2)
89
+ end
90
+ end
91
+
92
+ context '50 items, 50 per page, page 1, 5 pages to show' do
93
+ it 'returns pages: [1], last_page: 1' do
94
+ pages, last_page = described_class.show \
95
+ item_count: 50,
96
+ per_page: 50,
97
+ current_page: 1,
98
+ pages_to_show_count: 5
99
+ expect(pages).to eq([1])
100
+ expect(last_page).to eq(1)
101
+ end
102
+ end
103
+
104
+ context '50 items, 60 per page, page 1, 5 pages to show' do
105
+ it 'returns pages: [1], last_page: 1' do
106
+ pages, last_page = described_class.show \
107
+ item_count: 50,
108
+ per_page: 60,
109
+ current_page: 1,
110
+ pages_to_show_count: 5
111
+ expect(pages).to eq([1])
112
+ expect(last_page).to eq(1)
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'current page' do
118
+ context '60 items, 10 per page, page 1, 5 pages to show' do
119
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 6' do
120
+ pages, last_page = described_class.show \
121
+ item_count: 60,
122
+ per_page: 10,
123
+ current_page: 1,
124
+ pages_to_show_count: 5
125
+ expect(pages).to eq([1, 2, 3, 4, 5])
126
+ expect(last_page).to eq(6)
127
+ end
128
+ end
129
+
130
+ context '60 items, 10 per page, page 3, 5 pages to show' do
131
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 6' do
132
+ pages, last_page = described_class.show \
133
+ item_count: 60,
134
+ per_page: 10,
135
+ current_page: 3,
136
+ pages_to_show_count: 5
137
+ expect(pages).to eq([1, 2, 3, 4, 5])
138
+ expect(last_page).to eq(6)
139
+ end
140
+ end
141
+
142
+ context '60 items, 10 per page, page 4, 5 pages to show' do
143
+ it 'returns pages: [2, 3, 4, 5, 6], last_page: 6' do
144
+ pages, last_page = described_class.show \
145
+ item_count: 60,
146
+ per_page: 10,
147
+ current_page: 4,
148
+ pages_to_show_count: 5
149
+ expect(pages).to eq([2, 3, 4, 5, 6])
150
+ expect(last_page).to eq(6)
151
+ end
152
+ end
153
+
154
+ context '60 items, 10 per page, page 6, 5 pages to show' do
155
+ it 'returns pages: [2, 3, 4, 5, 6], last_page: 6' do
156
+ pages, last_page = described_class.show \
157
+ item_count: 60,
158
+ per_page: 10,
159
+ current_page: 6,
160
+ pages_to_show_count: 5
161
+ expect(pages).to eq([2, 3, 4, 5, 6])
162
+ expect(last_page).to eq(6)
163
+ end
164
+ end
165
+ end
166
+
167
+ context 'pages to show count' do
168
+ context '50 items, 10 per page, page 1, 5 pages to show' do
169
+ it 'returns pages: [1, 2, 3, 4, 5], last_page: 5' do
170
+ pages, last_page = described_class.show \
171
+ item_count: 50,
172
+ per_page: 10,
173
+ current_page: 1,
174
+ pages_to_show_count: 5
175
+ expect(pages).to eq([1, 2, 3, 4, 5])
176
+ expect(last_page).to eq(5)
177
+ end
178
+ end
179
+
180
+ context '50 items, 10 per page, page 1, 3 pages to show' do
181
+ it 'returns pages: [1, 2, 3], last_page: 5' do
182
+ pages, last_page = described_class.show \
183
+ item_count: 50,
184
+ per_page: 10,
185
+ current_page: 1,
186
+ pages_to_show_count: 3
187
+ expect(pages).to eq([1, 2, 3])
188
+ expect(last_page).to eq(5)
189
+ end
190
+ end
191
+
192
+ context '50 items, 10 per page, page 1, 1 page to show' do
193
+ it 'returns pages: [1], last_page: 5' do
194
+ pages, last_page = described_class.show \
195
+ item_count: 50,
196
+ per_page: 10,
197
+ current_page: 1,
198
+ pages_to_show_count: 1
199
+ expect(pages).to eq([1])
200
+ expect(last_page).to eq(5)
201
+ end
202
+ end
203
+ end
204
+
205
+ context 'invalid arguments' do
206
+ context 'negative item count' do
207
+ it 'throws an exception' do
208
+ expect do
209
+ described_class.show \
210
+ item_count: -1,
211
+ per_page: 10,
212
+ current_page: 1,
213
+ pages_to_show_count: 5
214
+ end.to raise_error("Item count can't be negative!")
215
+ end
216
+ end
217
+
218
+ context 'zero per page' do
219
+ it 'throws an exception' do
220
+ expect do
221
+ described_class.show \
222
+ item_count: 10,
223
+ per_page: 0,
224
+ current_page: 1,
225
+ pages_to_show_count: 5
226
+ end.to raise_error('Per page must be positive!')
227
+ end
228
+ end
229
+
230
+ context 'zero current page' do
231
+ it 'throws an exception' do
232
+ expect do
233
+ described_class.show \
234
+ item_count: 10,
235
+ per_page: 10,
236
+ current_page: 0,
237
+ pages_to_show_count: 5
238
+ end.to raise_error('Current page must be positive!')
239
+ end
240
+ end
241
+
242
+ context 'zero pages to show count' do
243
+ it 'throws an exception' do
244
+ expect do
245
+ described_class.show \
246
+ item_count: 10,
247
+ per_page: 10,
248
+ current_page: 1,
249
+ pages_to_show_count: 0
250
+ end.to raise_error('Pages to show count must be positive!')
251
+ end
252
+ end
253
+
254
+ context 'even pages to show count' do
255
+ it 'throws an exception' do
256
+ expect do
257
+ described_class.show \
258
+ item_count: 10,
259
+ per_page: 10,
260
+ current_page: 1,
261
+ pages_to_show_count: 2
262
+ end.to raise_error('Pages to show count must be an odd number!')
263
+ end
264
+ end
265
+
266
+ context 'current page is greater than the last page' do
267
+ it 'throws an exception' do
268
+ expect do
269
+ described_class.show \
270
+ item_count: 10,
271
+ per_page: 10,
272
+ current_page: 2,
273
+ pages_to_show_count: 5
274
+ end.to raise_error("Current page can't be greater than the last page!")
275
+ end
276
+ end
277
+ end
278
+ end
279
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ Coveralls::SimpleCov::Formatter,
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ ]
8
+
9
+ SimpleCov.start
10
+
11
+ require 'page_clip'
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_clip
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mert Guldur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Calculates pages to display in a paginator
84
+ email: mertguldur@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".rubocop.yml"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/page_clip.rb
97
+ - page_clip.gemspec
98
+ - spec/page_clip_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage:
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.6
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Pagination calculator
124
+ test_files: []