simple_paginator 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 073894baaf8b6c2915339f2e24f96fe118273e81
4
+ data.tar.gz: 14e02f2da4dfe5c482e70331fdc08a72a36da897
5
+ SHA512:
6
+ metadata.gz: f9e13c3017ff127603f92c982797d6f2f99751ced7d1f315db838e881398a594e30fae07e874585d8cc512426a39c8dd5f3bbe3e85bd62924efa242dbd4d66f6
7
+ data.tar.gz: 99df5ecd25316480a42dc3ce469875624a3ad74ac985f330e004f6f354b7fe4837b074a9be8046957b6a8d35bbe18cba92453dfcc60ba2471d44ee04c9aa8cc4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_paginator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Satoshi Ebisawa
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.ja.md ADDED
@@ -0,0 +1,55 @@
1
+ # SimplePaginator
2
+
3
+ API を作るときなどに使えるページネーション用のライブラリです。
4
+
5
+ ## 特徴
6
+ - count 文を発行しません。
7
+ - 最大ページ数が設定できます。
8
+ - 1 ページあたりのレコード数を設定できます。
9
+
10
+ ## インストール方法
11
+
12
+ Gemfile に以下のように記述してください。
13
+
14
+ gem 'simple_paginator'
15
+
16
+ そして、以下を実行します。
17
+
18
+ $ bundle
19
+
20
+ ## 使い方
21
+
22
+ モデルに以下のように SimplePaginator を include してください。
23
+
24
+ ```
25
+ class Post < ActiveRecord::Base
26
+ include SimplePaginator
27
+ end
28
+ ```
29
+
30
+ `Post.paged(page)` メソッドでページ分けされたレコードを取得します。
31
+
32
+ デフォルトでは 1 ページあたり 25 件ずつ、最大 10 ページまで取得できるようになっています。また、次のページにレコードがある場合は規定より 1 件多くレコードが返ります。
33
+
34
+ ```
35
+ Post.paged(1) #=> 最大 26 件のオブジェクトが返ります。
36
+ Post.paged(11) #=> 最大ページ以降は常に 0 件のレコードが返ります。
37
+ ```
38
+
39
+ 1 ページあたりのレコード数や最大ページ数を変えたい場合は `set_per_page`, `set_max_page` メソッドを使います。
40
+
41
+ ```
42
+ class Post < ActiveRecord::Base
43
+ include SimplePaginator
44
+ set_per_page 10
45
+ set_max_page 5
46
+ end
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # SimplePaginator
2
+
3
+ This is a useful pagination library especially building APIs.
4
+
5
+ ## Feature
6
+ - Does not issue `count`.
7
+ - You can set max page.
8
+ - You can set number of records per page.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'simple_paginator'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+ Include SimplePaginator into your models like below.
23
+
24
+ ```
25
+ class Post < ActiveRecord::Base
26
+ include SimplePaginator
27
+ end
28
+ ```
29
+
30
+ You can get paginated records using `Post.paged(page)` method.
31
+
32
+ Default is 25 records / page, maximum page number is 10.
33
+ You will get 26 (per_page + 1) records when you can get next page.
34
+
35
+ ```
36
+ Post.paged(1) #=> returns 26 records at most.
37
+ Post.paged(11) #=> returns 0 records if page is more than max_page.
38
+ ```
39
+
40
+ You can use `set_per_page`, `set_max_page` to change default behavior.
41
+
42
+ ```
43
+ class Post < ActiveRecord::Base
44
+ include SimplePaginator
45
+ set_per_page 10
46
+ set_max_page 5
47
+ end
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module SimplePaginator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "simple_paginator/version"
2
+ require 'active_support/core_ext/class'
3
+ require 'active_support/concern'
4
+ require 'active_record'
5
+
6
+ module SimplePaginator
7
+ extend ActiveSupport::Concern
8
+
9
+ DEFAULT_PER_PAGE = 25
10
+ DEFAULT_MAX_PAGE = 10
11
+
12
+ included do
13
+ cattr_accessor :per_page, :max_page
14
+ self.per_page = DEFAULT_PER_PAGE
15
+ self.max_page = DEFAULT_MAX_PAGE
16
+ scope :paged, ->(page) {
17
+ num = page_number(page)
18
+ if num > max_page
19
+ none
20
+ elsif num == max_page
21
+ limit(per_page).offset((num-1)*per_page)
22
+ else
23
+ limit(per_page+1).offset((num-1)*per_page)
24
+ end
25
+ }
26
+ end
27
+
28
+ module ClassMethods
29
+ def page_number(raw_page)
30
+ [raw_page.to_i, 1].max
31
+ end
32
+ def set_per_page(per_page)
33
+ self.per_page = per_page
34
+ end
35
+ def set_max_page(max_page)
36
+ self.max_page = max_page
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_paginator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_paginator"
8
+ spec.version = SimplePaginator::VERSION
9
+ spec.authors = ["Satoshi Ebisawa"]
10
+ spec.email = ["e.satoshi@gmail.com"]
11
+ spec.description = %q{Useful paginator library for building APIs.}
12
+ spec.summary = %q{Useful paginator library for building APIs.}
13
+ spec.homepage = "https://github.com/satococoa/simple_paginator"
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 "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'sqlite3'
25
+ spec.add_dependency 'activesupport'
26
+ spec.add_dependency 'activerecord'
27
+ end
@@ -0,0 +1,132 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe SimplePaginator do
4
+ context 'Post includes SimplePaginator' do
5
+
6
+ before do
7
+ class Post < ActiveRecord::Base
8
+ include SimplePaginator
9
+ end
10
+ ActiveRecord::Schema.define do
11
+ self.verbose = false
12
+ create_table :posts, :force => true do |t|
13
+ t.string :text
14
+ end
15
+ end
16
+ end
17
+
18
+ describe 'class methods' do
19
+ specify { expect(Post).to be_respond_to(:paged) }
20
+ specify { expect(Post).to be_respond_to(:set_per_page) }
21
+ specify { expect(Post).to be_respond_to(:set_max_page) }
22
+ end
23
+
24
+ describe '.page_number' do
25
+ subject(:page_number) { Post.page_number(raw_page) }
26
+ context 'raw_page is string' do
27
+ let(:raw_page) { 'foo' }
28
+ specify { expect(subject).to eq(1) }
29
+ end
30
+ context 'raw_page is blank string' do
31
+ let(:raw_page) { '' }
32
+ specify { expect(subject).to eq(1) }
33
+ end
34
+ context 'raw_page is 0' do
35
+ let(:raw_page) { 0 }
36
+ specify { expect(subject).to eq(1) }
37
+ end
38
+ context 'raw_page is negative' do
39
+ let(:raw_page) { -1 }
40
+ specify { expect(subject).to eq(1) }
41
+ end
42
+ context 'raw_page is a string to represent number' do
43
+ let(:raw_page) { '10' }
44
+ specify { expect(subject).to eq(10) }
45
+ end
46
+ context 'raw_page is a string to represent negative number' do
47
+ let(:raw_page) { '-1' }
48
+ specify { expect(subject).to eq(1) }
49
+ end
50
+ context 'raw_page is nil' do
51
+ let(:raw_page) { nil }
52
+ specify { expect(subject).to eq(1) }
53
+ end
54
+ end
55
+
56
+ describe '.paged' do
57
+ context 'per_page, max_page are default' do
58
+ context 'page = 1' do
59
+ let(:page) { 1 }
60
+ it 'send :limit, :offset method' do
61
+ Post.should_receive(:limit).with(26).once { Post }
62
+ Post.should_receive(:offset).with(0).once
63
+ Post.paged(page)
64
+ end
65
+ end
66
+ context 'page = 2' do
67
+ let(:page) { 2 }
68
+ it 'send :limit, :offset method' do
69
+ Post.should_receive(:limit).with(26).once { Post }
70
+ Post.should_receive(:offset).with(25).once
71
+ Post.paged(page)
72
+ end
73
+ end
74
+ context 'page = 10' do
75
+ let(:page) { 10 }
76
+ it 'send :limit, :offset method' do
77
+ Post.should_receive(:limit).with(25).once { Post }
78
+ Post.should_receive(:offset).with(25 * (page-1)).once
79
+ Post.paged(page)
80
+ end
81
+ end
82
+ context 'page = 11' do
83
+ let(:page) { 11 }
84
+ it 'send :none method' do
85
+ Post.should_receive(:none).once
86
+ Post.paged(page)
87
+ end
88
+ end
89
+ end
90
+
91
+ context 'change per_page, max_page' do
92
+ before do
93
+ Post.class_eval {
94
+ set_per_page 3
95
+ set_max_page 2
96
+ }
97
+ end
98
+
99
+ after do
100
+ Post.class_eval {
101
+ set_per_page SimplePaginator::DEFAULT_PER_PAGE
102
+ set_max_page SimplePaginator::DEFAULT_MAX_PAGE
103
+ }
104
+ end
105
+
106
+ context 'page = 1' do
107
+ let(:page) { 1 }
108
+ it 'send :limit, :offset method' do
109
+ Post.should_receive(:limit).with(4).once { Post }
110
+ Post.should_receive(:offset).with(0).once
111
+ Post.paged(page)
112
+ end
113
+ end
114
+ context 'page = 2' do
115
+ let(:page) { 2 }
116
+ it 'send :limit, :offset method' do
117
+ Post.should_receive(:limit).with(3).once { Post }
118
+ Post.should_receive(:offset).with(3).once
119
+ Post.paged(page)
120
+ end
121
+ end
122
+ context 'page = 3' do
123
+ let(:page) { 3 }
124
+ it 'send :limit, :offset method' do
125
+ Post.should_receive(:none).once
126
+ Post.paged(page)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,32 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ lib = File.expand_path('../../lib', __FILE__)
8
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
9
+
10
+ require 'bundler/setup'
11
+ Bundler.require
12
+
13
+ require 'simple_paginator'
14
+
15
+ RSpec.configure do |config|
16
+ config.treat_symbols_as_metadata_keys_with_true_values = true
17
+ config.run_all_when_everything_filtered = true
18
+ config.filter_run :focus
19
+
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ config.order = 'random'
25
+
26
+ config.before do
27
+ ActiveRecord::Base.establish_connection(
28
+ :adapter => 'sqlite3',
29
+ :database => ':memory:'
30
+ )
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_paginator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Satoshi Ebisawa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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: sqlite3
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: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Useful paginator library for building APIs.
98
+ email:
99
+ - e.satoshi@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.ja.md
109
+ - README.md
110
+ - Rakefile
111
+ - lib/simple_paginator.rb
112
+ - lib/simple_paginator/version.rb
113
+ - simple_paginator.gemspec
114
+ - spec/simple_paginator_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/satococoa/simple_paginator
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Useful paginator library for building APIs.
140
+ test_files:
141
+ - spec/simple_paginator_spec.rb
142
+ - spec/spec_helper.rb
143
+ has_rdoc: