page_number 0.1.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: cbab3c028875b91910a0ca596802913cc1a50a5d
4
+ data.tar.gz: 22b82f9fca433a2369accea679b851180e1d7b8c
5
+ SHA512:
6
+ metadata.gz: 5fefd1bde3f7a784d8028be219c644a55c96d45cabbddbf388715244cdcb5ee3566ebc0e7dedaf9ecf6df01f49a423162e40a3fdda0ae6563c802905a8905850
7
+ data.tar.gz: cd86702614121abd2b7699ce882e259e703f41aad885891c8ba3cc7ac964724055cd08ab793c9c8b24470bb7b3f0ef239ffa51575ef0bdd4ed235dbadd7ea7d9
@@ -0,0 +1,64 @@
1
+ # PageNumber
2
+
3
+ [![Build Status](https://travis-ci.org/sshaw/page_number.svg?branch=master)](https://travis-ci.org/sshaw/page_number)
4
+
5
+ Utility methods for pagination page and per page that make sure you'll
6
+ always have a valid number.
7
+
8
+ Use them your controllers (recommended) or model or anywhere where you process page
9
+ info.
10
+
11
+ ## Usage
12
+
13
+ `PageNumber` provides two methods:
14
+
15
+ * `page(n)`/`__page__(n)`
16
+ * `per_page(n)`/`__per_page__(n)` (defaults to `10`)
17
+
18
+ To use, `include` or `extend` the `PageNumber` module:
19
+
20
+ ```rb
21
+ require "page_number"
22
+
23
+ class BaseController
24
+ include PageNumber
25
+ end
26
+
27
+ class SubController < BaseController
28
+ def index
29
+ User.all(:page => page(params[:page]), :per_page => per_page(params[:per_page]))
30
+ end
31
+ end
32
+
33
+ ```
34
+
35
+ To have better control over how `page` and `per_page` work you can
36
+ define one or more of the following methods:
37
+
38
+ ```rb
39
+ class BaseController
40
+ include PageNumber
41
+
42
+ # These are all optional
43
+ protected
44
+
45
+ def page_sizes
46
+ [ 25, 50, 100 ]
47
+ end
48
+
49
+ def default_per_page
50
+ 20
51
+ end
52
+
53
+ def max_page_number
54
+ # who knows!?
55
+ end
56
+ ```
57
+
58
+ ## Author
59
+
60
+ Skye Shaw [skye.shaw AT gmail.com]
61
+
62
+ ## License
63
+
64
+ Released under the MIT License: http://www.opensource.org/licenses/MIT
@@ -0,0 +1,71 @@
1
+ # === Usage
2
+ #
3
+ # require "page_number"
4
+ #
5
+ # class BaseController
6
+ # include PageNumber
7
+ #
8
+ # protected
9
+ #
10
+ # def page_sizes
11
+ # [ 25, 50, 100 ]
12
+ # end
13
+ # end
14
+ #
15
+ # class SubController < BaseController
16
+ # def action
17
+ # User.all(:page => page(params[:page]), :per_page => per_page(params[:per_page]))
18
+ # end
19
+ # end
20
+ #
21
+
22
+ module PageNumber
23
+ VERSION = "0.1.0".freeze
24
+
25
+ def page(n)
26
+ n = __int(n)
27
+ return default_page if n < 1
28
+
29
+ max_page_number && n > max_page_number ? max_page_number : n
30
+ end
31
+
32
+ alias :__page__ :page
33
+
34
+ def per_page(n)
35
+ n = __int(n)
36
+ return default_per_page if n < 1
37
+
38
+ page_sizes && !page_sizes.include?(n) ? default_per_page : n
39
+ end
40
+
41
+ alias :__per_page__ :per_page
42
+
43
+ protected
44
+
45
+ def default_per_page
46
+ 10
47
+ end
48
+
49
+ def default_page
50
+ 1
51
+ end
52
+
53
+ def max_page_number
54
+ end
55
+
56
+ def page_sizes
57
+ end
58
+
59
+ private
60
+
61
+ def __int(n)
62
+ case
63
+ when n.respond_to?(:to_i)
64
+ n.to_i
65
+ when n.respond_to?(:to_int)
66
+ n.to_int
67
+ else
68
+ -1
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,98 @@
1
+ require "minitest/autorun"
2
+ require "page_number"
3
+
4
+ class TestPageNumber < MiniTest::Test
5
+ def setup
6
+ @p = Object.new
7
+ @p.extend PageNumber
8
+ end
9
+
10
+ def test_page_invalid_arg_returns_default_page
11
+ [-1, 0, {}].each { |n| assert_equal 1, @p.page(n) }
12
+
13
+ def @p.default_page
14
+ 100
15
+ end
16
+
17
+ assert_equal 100, @p.page(-1)
18
+ end
19
+
20
+ def test_page_valid_arg_returns_arg
21
+ [1, 100, 500].each { |n| assert_equal n, @p.page(n) }
22
+ end
23
+
24
+ def test_page_converts_arg_to_fixnum
25
+ assert_equal 2, @p.page("2")
26
+
27
+ o = Object.new
28
+ def o.to_i
29
+ 2
30
+ end
31
+
32
+ assert_equal 2, @p.page(o)
33
+
34
+ o = Object.new
35
+ def o.to_int
36
+ 2
37
+ end
38
+
39
+ assert_equal 2, @p.page(o)
40
+ end
41
+
42
+ def test_page_limits_arg_to_max_page_number
43
+ # No max by default
44
+ assert_equal 1_000, @p.page(1_000)
45
+
46
+ def @p.max_page_number
47
+ 999
48
+ end
49
+
50
+ assert_equal 999, @p.page(1_000)
51
+ end
52
+
53
+ def test_per_page_invalid_arg_returns_default
54
+ [-1, 0, {}].each { |n| assert_equal 10, @p.per_page(n) }
55
+
56
+ def @p.default_per_page
57
+ 100
58
+ end
59
+
60
+ assert_equal 100, @p.per_page(-1)
61
+ end
62
+
63
+ def test_per_page_valid_arg_returns_arg
64
+ assert_equal 2, @p.per_page(2)
65
+ end
66
+
67
+ def test_per_page_converts_arg_to_fixnum
68
+ assert_equal 2, @p.per_page("2")
69
+
70
+ o = Object.new
71
+ def o.to_i
72
+ 2
73
+ end
74
+
75
+ assert_equal 2, @p.per_page(o)
76
+
77
+ o = Object.new
78
+ def o.to_int
79
+ 2
80
+ end
81
+
82
+ assert_equal 2, @p.per_page(o)
83
+ end
84
+
85
+ def test_per_page_enforces_defined_page_sizes
86
+ def @p.page_sizes
87
+ [3, 8]
88
+ end
89
+
90
+ def @p.default_per_page
91
+ 5
92
+ end
93
+
94
+ assert_equal 3, @p.per_page(3)
95
+ assert_equal 8, @p.per_page(8)
96
+ assert_equal 5, @p.per_page(100)
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-17 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: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: |2
42
+ Utility methods for pagination page and per page that make sure you'll always have a valid number.
43
+ Use them your controllers or model or anywhere where you process page info.
44
+ email: skye.shaw@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - README.md
49
+ files:
50
+ - README.md
51
+ - lib/page_number.rb
52
+ - test/test_page_number.rb
53
+ homepage: https://github.com/sshaw/page_number
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.5.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Utility methods for pagination page and per page that make sure you'll always
77
+ have a valid number.
78
+ test_files:
79
+ - test/test_page_number.rb