page_number 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbab3c028875b91910a0ca596802913cc1a50a5d
4
- data.tar.gz: 22b82f9fca433a2369accea679b851180e1d7b8c
3
+ metadata.gz: 712172dd03a791a91e3194afd2c19eb460706fb9
4
+ data.tar.gz: f699670034fe3b8b841a0fb81eb879d212f6d233
5
5
  SHA512:
6
- metadata.gz: 5fefd1bde3f7a784d8028be219c644a55c96d45cabbddbf388715244cdcb5ee3566ebc0e7dedaf9ecf6df01f49a423162e40a3fdda0ae6563c802905a8905850
7
- data.tar.gz: cd86702614121abd2b7699ce882e259e703f41aad885891c8ba3cc7ac964724055cd08ab793c9c8b24470bb7b3f0ef239ffa51575ef0bdd4ed235dbadd7ea7d9
6
+ metadata.gz: 62d30c68bea957aca09b7923dbd257e0d90685a04dd750622d0406fd60b512a67a473508339394913a5597f71f5d92fcdbd8c5795ce4d914102802c8804ed02a
7
+ data.tar.gz: cded76c0227c21f63f66295d1bbfe9c3fb371dfe39877f26fc613a36c0a0d06001a8c29c6ee5cd026ff4cf7c9339ee6e3710cdcf6943fd0e0d2c5fdd1801f65b
data/README.md CHANGED
@@ -10,9 +10,10 @@ info.
10
10
 
11
11
  ## Usage
12
12
 
13
- `PageNumber` provides two methods:
13
+ `PageNumber` provides 3 methods:
14
14
 
15
- * `page(n)`/`__page__(n)`
15
+ * `page(n)`/`__page__(n)` (defaults to `1`)
16
+ * `page!(n)`/`__page__!(n)` (raises `PageNumber::PageInvalid` instead of returning default)
16
17
  * `per_page(n)`/`__per_page__(n)` (defaults to `10`)
17
18
 
18
19
  To use, `include` or `extend` the `PageNumber` module:
@@ -50,6 +51,10 @@ class BaseController
50
51
  20
51
52
  end
52
53
 
54
+ def default_page
55
+ 2
56
+ end
57
+
53
58
  def max_page_number
54
59
  # who knows!?
55
60
  end
@@ -1,26 +1,45 @@
1
1
  # === Usage
2
2
  #
3
- # require "page_number"
3
+ # require "page_number"
4
4
  #
5
- # class BaseController
6
- # include PageNumber
5
+ # class BaseController
6
+ # include PageNumber
7
7
  #
8
- # protected
8
+ # protected
9
9
  #
10
- # def page_sizes
11
- # [ 25, 50, 100 ]
12
- # end
13
- # end
10
+ # def page_sizes
11
+ # [ 25, 50, 100 ]
12
+ # end
13
+ # end
14
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
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
20
  #
21
21
 
22
22
  module PageNumber
23
- VERSION = "0.1.0".freeze
23
+ VERSION = "0.2.0".freeze
24
+
25
+ class PageInvalid < StandardError
26
+ attr_reader :page
27
+
28
+ def initialize(message, page)
29
+ super message
30
+ @page = page
31
+ end
32
+ end
33
+
34
+ def page!(page)
35
+ n = __int(page)
36
+ raise PageInvalid.new("page must be > 0", page) if n < 1
37
+ raise PageInvalid.new("page must be <= #{max_page_number}", page) if max_page_number && n > max_page_number
38
+
39
+ n
40
+ end
41
+
42
+ alias :__page__! :page!
24
43
 
25
44
  def page(n)
26
45
  n = __int(n)
@@ -39,6 +39,34 @@ class TestPageNumber < MiniTest::Test
39
39
  assert_equal 2, @p.page(o)
40
40
  end
41
41
 
42
+ def test_page_exception_invalid_arg_raises_exception
43
+ [-1, 0, {}].each do |n|
44
+ ex = assert_raises PageNumber::PageInvalid do
45
+ @p.page!(n)
46
+ end
47
+
48
+ assert_equal n, ex.page
49
+ end
50
+
51
+ def @p.max_page_number
52
+ 10
53
+ end
54
+
55
+ ex = assert_raises PageNumber::PageInvalid do
56
+ @p.page!(11)
57
+ end
58
+
59
+ assert_equal 11, ex.page
60
+ end
61
+
62
+ def test_page_exception_valid_arg_does_not_raise_exception
63
+ [1, 100, 500].each { |n| assert_equal n, @p.page!(n) }
64
+ end
65
+
66
+ def test_page_exception_converts_arg_to_fixnum
67
+ assert_equal 2, @p.page("2")
68
+ end
69
+
42
70
  def test_page_limits_arg_to_max_page_number
43
71
  # No max by default
44
72
  assert_equal 1_000, @p.page(1_000)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: page_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skye Shaw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-17 00:00:00.000000000 Z
11
+ date: 2019-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.5.1
73
+ rubygems_version: 2.6.14
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: Utility methods for pagination page and per page that make sure you'll always