cursor_paginator 0.1.0
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 +7 -0
- data/.github/workflows/build.yml +25 -0
- data/.github/workflows/create_release.yml +34 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.rubocop.yml +50 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +85 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rspec +3 -0
- data/bin/setup +8 -0
- data/cursor_paginator.gemspec +35 -0
- data/lib/cursor_paginator.rb +20 -0
- data/lib/cursor_paginator/options_parser.rb +41 -0
- data/lib/cursor_paginator/pagination_result.rb +62 -0
- data/lib/cursor_paginator/paginator.rb +13 -0
- data/lib/cursor_paginator/paginator/active_record.rb +25 -0
- data/lib/cursor_paginator/paginator/array.rb +37 -0
- data/lib/cursor_paginator/paginator/base.rb +38 -0
- data/lib/cursor_paginator/paginator/dynamoid.rb +15 -0
- data/lib/cursor_paginator/version.rb +3 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cd17cf8b896b670b9774e243a20515a7d2eca976bd52d7315d3f9173f2ce88f6
|
4
|
+
data.tar.gz: 57600555eab7a1ce51a220b2bc30498911776a845963957a38161a493bb940e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c3b6b036cbb90297eecd44c374324d1ab09f8253f371a6335100f336f01330e8bbe3fad132727f287689d43ab7fc503afc17b3d25cb772a1e85010090448f84
|
7
|
+
data.tar.gz: 3f9c0a1ca85e3769a160eb04b50d3e134653eea166944ceadffa6d867681e80e854c41863c45cb6950181a7334c9459b37f911f579f3681afe3e142a6a40440e
|
@@ -0,0 +1,25 @@
|
|
1
|
+
name: Build
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v2
|
12
|
+
|
13
|
+
- name: Set up Ruby
|
14
|
+
uses: actions/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: 2.7.x
|
17
|
+
|
18
|
+
- name: Install Dependencies
|
19
|
+
run: |
|
20
|
+
gem install bundler
|
21
|
+
bundle install --jobs 4 --retry 3
|
22
|
+
|
23
|
+
- name: Run Specs
|
24
|
+
run: |
|
25
|
+
bundle exec rake
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Create Release
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches:
|
5
|
+
- master
|
6
|
+
jobs:
|
7
|
+
release:
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
steps:
|
10
|
+
- uses: actions/checkout@v2
|
11
|
+
|
12
|
+
- name: Set up Ruby
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
|
17
|
+
- name: Install Dependencies
|
18
|
+
run: |
|
19
|
+
gem install bundler
|
20
|
+
bundle install --jobs 4 --retry 3
|
21
|
+
- name: Set up Git
|
22
|
+
run: |
|
23
|
+
git config --global user.email "dev@bongloy.com"
|
24
|
+
git config --global user.name "Bongloy"
|
25
|
+
|
26
|
+
- name: Bump version and publish release
|
27
|
+
env:
|
28
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
29
|
+
GEM_HOST_API_KEY: ${{ secrets.GEM_HOST_API_KEY }}
|
30
|
+
run: |
|
31
|
+
mkdir -p ~/.gem
|
32
|
+
echo -e "---\r\n:rubygems_api_key: $GEM_HOST_API_KEY" > ~/.gem/credentials
|
33
|
+
chmod 0600 ~/.gem/credentials
|
34
|
+
bundle exec rake release
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
Style/FrozenStringLiteralComment:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Style/StringLiterals:
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Layout/LineLength:
|
12
|
+
Max: 100
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/ClassAndModuleChildren:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/BlockLength:
|
21
|
+
Enabled: true
|
22
|
+
Exclude:
|
23
|
+
- spec/**/*.rb
|
24
|
+
|
25
|
+
Style/BlockDelimiters:
|
26
|
+
Enabled: true
|
27
|
+
Exclude:
|
28
|
+
- spec/**/*.rb
|
29
|
+
|
30
|
+
Style/NumericLiterals:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
DescribedClass:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
MultipleExpectations:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
ExampleLength:
|
40
|
+
Max: 10
|
41
|
+
|
42
|
+
RSpec/DescribeClass:
|
43
|
+
Enabled: true
|
44
|
+
Exclude:
|
45
|
+
- spec/system/**/*.rb
|
46
|
+
- spec/requests/**/*.rb
|
47
|
+
- spec/views/**/*.rb
|
48
|
+
|
49
|
+
RSpec/ExampleLength:
|
50
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at dev@bongloy.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cursor_paginator (0.1.0)
|
5
|
+
activesupport (>= 3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.3)
|
11
|
+
activesupport (= 6.0.3)
|
12
|
+
activerecord (6.0.3)
|
13
|
+
activemodel (= 6.0.3)
|
14
|
+
activesupport (= 6.0.3)
|
15
|
+
activesupport (6.0.3)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
21
|
+
ast (2.4.0)
|
22
|
+
coderay (1.1.2)
|
23
|
+
concurrent-ruby (1.1.6)
|
24
|
+
diff-lcs (1.3)
|
25
|
+
i18n (1.8.2)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
method_source (1.0.0)
|
28
|
+
minitest (5.14.1)
|
29
|
+
parallel (1.19.1)
|
30
|
+
parser (2.7.1.2)
|
31
|
+
ast (~> 2.4.0)
|
32
|
+
pry (0.13.1)
|
33
|
+
coderay (~> 1.1)
|
34
|
+
method_source (~> 1.0)
|
35
|
+
rainbow (3.0.0)
|
36
|
+
rake (12.3.3)
|
37
|
+
rexml (3.2.4)
|
38
|
+
rspec (3.9.0)
|
39
|
+
rspec-core (~> 3.9.0)
|
40
|
+
rspec-expectations (~> 3.9.0)
|
41
|
+
rspec-mocks (~> 3.9.0)
|
42
|
+
rspec-core (3.9.2)
|
43
|
+
rspec-support (~> 3.9.3)
|
44
|
+
rspec-expectations (3.9.2)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.9.0)
|
47
|
+
rspec-mocks (3.9.1)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.9.0)
|
50
|
+
rspec-support (3.9.3)
|
51
|
+
rubocop (0.83.0)
|
52
|
+
parallel (~> 1.10)
|
53
|
+
parser (>= 2.7.0.1)
|
54
|
+
rainbow (>= 2.2.2, < 4.0)
|
55
|
+
rexml
|
56
|
+
ruby-progressbar (~> 1.7)
|
57
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
58
|
+
rubocop-performance (1.5.2)
|
59
|
+
rubocop (>= 0.71.0)
|
60
|
+
rubocop-rspec (1.39.0)
|
61
|
+
rubocop (>= 0.68.1)
|
62
|
+
ruby-progressbar (1.10.1)
|
63
|
+
sqlite3 (1.4.2)
|
64
|
+
thread_safe (0.3.6)
|
65
|
+
tzinfo (1.2.7)
|
66
|
+
thread_safe (~> 0.1)
|
67
|
+
unicode-display_width (1.7.0)
|
68
|
+
zeitwerk (2.3.0)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
ruby
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
activerecord
|
75
|
+
cursor_paginator!
|
76
|
+
pry
|
77
|
+
rake (~> 12.0)
|
78
|
+
rspec
|
79
|
+
rubocop
|
80
|
+
rubocop-performance
|
81
|
+
rubocop-rspec
|
82
|
+
sqlite3
|
83
|
+
|
84
|
+
BUNDLED WITH
|
85
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 David Wilkie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# CursorPaginator
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cursor_paginator`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cursor_paginator'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cursor_paginator
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bongloy/cursor_paginator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/bongloy/cursor_paginator/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the CursorPaginator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bongloy/cursor_paginator/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cursor_paginator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rspec
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative "lib/cursor_paginator/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "cursor_paginator"
|
5
|
+
spec.version = CursorPaginator::VERSION
|
6
|
+
spec.authors = ["David Wilkie", "Samnang Chhun"]
|
7
|
+
spec.email = ["dwilkie@bongloy.com", "samnang@bongloy.com"]
|
8
|
+
|
9
|
+
spec.summary = "Cursor pagination"
|
10
|
+
spec.description = "Cursor pagination for ruby"
|
11
|
+
spec.homepage = "https://github.com/bongloy/cursor_paginator"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "activesupport", ">= 3"
|
27
|
+
|
28
|
+
spec.add_development_dependency "activerecord"
|
29
|
+
spec.add_development_dependency "sqlite3"
|
30
|
+
spec.add_development_dependency "pry"
|
31
|
+
spec.add_development_dependency "rspec"
|
32
|
+
spec.add_development_dependency "rubocop"
|
33
|
+
spec.add_development_dependency "rubocop-performance"
|
34
|
+
spec.add_development_dependency "rubocop-rspec"
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
class << self
|
3
|
+
attr_accessor :paginator
|
4
|
+
|
5
|
+
def paginate(original_scope, **options)
|
6
|
+
if original_scope.is_a?(Array)
|
7
|
+
Paginator::Array.new(**options).paginate(original_scope)
|
8
|
+
else
|
9
|
+
paginator.new(**options).paginate(original_scope)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require "active_support/core_ext"
|
16
|
+
|
17
|
+
require_relative "cursor_paginator/version"
|
18
|
+
require_relative "cursor_paginator/paginator"
|
19
|
+
require_relative "cursor_paginator/options_parser"
|
20
|
+
require_relative "cursor_paginator/pagination_result"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
MIN_ITEMS = 1
|
3
|
+
MAX_ITEMS = 100
|
4
|
+
DEFAULT_LIMIT = 10
|
5
|
+
|
6
|
+
class OptionsParser
|
7
|
+
attr_reader :options
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@options = options.deep_dup
|
11
|
+
@options[:size] = normalize_page_size
|
12
|
+
end
|
13
|
+
|
14
|
+
def filter_required?
|
15
|
+
cursor.present?
|
16
|
+
end
|
17
|
+
|
18
|
+
def cursor
|
19
|
+
options[:before].presence || options[:after].presence
|
20
|
+
end
|
21
|
+
|
22
|
+
def cursor_direction
|
23
|
+
(after_direction? ? "after" : "before").inquiry
|
24
|
+
end
|
25
|
+
|
26
|
+
def page_size
|
27
|
+
@options[:size]
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def after_direction?
|
33
|
+
options[:before].blank?
|
34
|
+
end
|
35
|
+
|
36
|
+
def normalize_page_size
|
37
|
+
result = options.fetch(:size, DEFAULT_LIMIT).to_i
|
38
|
+
[[result, MIN_ITEMS].max, MAX_ITEMS].min
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
class PaginationResult
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :records_scope, :paginator
|
6
|
+
|
7
|
+
delegate :each, :size, to: :records
|
8
|
+
|
9
|
+
def initialize(records_scope, paginator)
|
10
|
+
@records_scope = records_scope
|
11
|
+
@paginator = paginator
|
12
|
+
end
|
13
|
+
|
14
|
+
def prev_cursor_params
|
15
|
+
{ before: prev_cursor }
|
16
|
+
end
|
17
|
+
|
18
|
+
def next_cursor_params
|
19
|
+
{ after: next_cursor }
|
20
|
+
end
|
21
|
+
|
22
|
+
def last_page?
|
23
|
+
additional_record.blank?
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def prev_cursor
|
29
|
+
return if paginator.cursor_direction.before? && last_page?
|
30
|
+
return if records.empty?
|
31
|
+
|
32
|
+
fetch_cursor(records.first)
|
33
|
+
end
|
34
|
+
|
35
|
+
def next_cursor
|
36
|
+
return if paginator.cursor_direction.after? && last_page?
|
37
|
+
return if records.empty?
|
38
|
+
|
39
|
+
fetch_cursor(records.last)
|
40
|
+
end
|
41
|
+
|
42
|
+
def fetch_cursor(record)
|
43
|
+
record.public_send(paginator.paginator_options.fetch(:primary_key))
|
44
|
+
end
|
45
|
+
|
46
|
+
def records
|
47
|
+
@records ||= load_records
|
48
|
+
end
|
49
|
+
|
50
|
+
def additional_record
|
51
|
+
load_records if @records.nil?
|
52
|
+
|
53
|
+
@additional_record
|
54
|
+
end
|
55
|
+
|
56
|
+
def load_records
|
57
|
+
records = records_scope.take(paginator.page_size + 1)
|
58
|
+
@additional_record = records.pop if records.size > paginator.page_size
|
59
|
+
paginator.cursor_direction.after? ? records : records.reverse
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
module Paginator
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require_relative "paginator/base"
|
7
|
+
require_relative "paginator/array"
|
8
|
+
|
9
|
+
if defined?(::ActiveRecord)
|
10
|
+
require_relative "paginator/active_record"
|
11
|
+
|
12
|
+
CursorPaginator.paginator = CursorPaginator::Paginator::ActiveRecord
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
module Paginator
|
3
|
+
class ActiveRecord < Base
|
4
|
+
def paginate(scope)
|
5
|
+
records = scope.reorder(paginator_options.fetch(:order_key) => order_direction)
|
6
|
+
records = filter_by_cursor(records) if options_parser.filter_required?
|
7
|
+
|
8
|
+
result = PaginationResult.new(records, self)
|
9
|
+
result
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def filter_by_cursor(collection)
|
15
|
+
arel_table = collection.arel_table
|
16
|
+
order_column = arel_table[paginator_options.fetch(:order_key)]
|
17
|
+
primary_column = arel_table[paginator_options.fetch(:primary_key)]
|
18
|
+
cursor_select = arel_table.project(order_column).where(primary_column.eq(options_parser.cursor))
|
19
|
+
where_clause = order_column.public_send(query_operator, cursor_select)
|
20
|
+
|
21
|
+
collection.where(where_clause)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
module Paginator
|
3
|
+
class Array < Base
|
4
|
+
def paginate(scope)
|
5
|
+
records = scope.sort_by(&paginator_options.fetch(:order_key))
|
6
|
+
records = filter_by_cursor(records) if options_parser.filter_required?
|
7
|
+
records = records.reverse if order_direction == :desc
|
8
|
+
|
9
|
+
PaginationResult.new(records, self)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def filter_by_cursor(collection)
|
15
|
+
cursor_index = find_cursor_index(collection)
|
16
|
+
return [] if cursor_index.blank?
|
17
|
+
|
18
|
+
left, right = split_collection(collection, cursor_index)
|
19
|
+
|
20
|
+
query_operator == :lt ? left : right
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_cursor_index(collection)
|
24
|
+
collection.index do |item|
|
25
|
+
item.public_send(paginator_options.fetch(:primary_key)) == options_parser.cursor
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def split_collection(collection, cursor_index)
|
30
|
+
left, right = collection.partition.with_index { |_, i| i <= cursor_index }
|
31
|
+
left.pop
|
32
|
+
|
33
|
+
[left, right]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
module Paginator
|
3
|
+
class Base
|
4
|
+
SORT_DIRECTIONS = %i[asc desc].freeze
|
5
|
+
|
6
|
+
attr_reader :options_parser, :paginator_options
|
7
|
+
delegate :cursor_direction, :page_size, to: :options_parser
|
8
|
+
|
9
|
+
def initialize(page_options: {}, paginator_options: {})
|
10
|
+
@options_parser = OptionsParser.new(page_options)
|
11
|
+
@paginator_options = paginator_options.reverse_merge(
|
12
|
+
order_key: :id,
|
13
|
+
primary_key: :id,
|
14
|
+
sort_direction: :desc
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def query_operator
|
21
|
+
return cursor_direction.after? ? :gt : :lt if sort_direction == :asc
|
22
|
+
return cursor_direction.after? ? :lt : :gt if sort_direction == :desc
|
23
|
+
end
|
24
|
+
|
25
|
+
def order_direction
|
26
|
+
cursor_direction.after? ? sort_direction : opposite_sort_direction
|
27
|
+
end
|
28
|
+
|
29
|
+
def sort_direction
|
30
|
+
paginator_options.fetch(:sort_direction)
|
31
|
+
end
|
32
|
+
|
33
|
+
def opposite_sort_direction
|
34
|
+
SORT_DIRECTIONS.find { |i| i != sort_direction }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CursorPaginator
|
2
|
+
module Paginator
|
3
|
+
class Dynamoid < Base
|
4
|
+
def paginate(scope)
|
5
|
+
if options_parser.filter_required?
|
6
|
+
cursor = scope.source.where(paginator_options.fetch(:primary_key) => options_parser.cursor).first
|
7
|
+
return PaginationResult.new([], self) if cursor.blank?
|
8
|
+
end
|
9
|
+
|
10
|
+
records = scope.record_limit(page_size + 1).start(cursor)
|
11
|
+
PaginationResult.new(records, self)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cursor_paginator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Wilkie
|
8
|
+
- Samnang Chhun
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activerecord
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: sqlite3
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rubocop
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rubocop-performance
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: rubocop-rspec
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Cursor pagination for ruby
|
127
|
+
email:
|
128
|
+
- dwilkie@bongloy.com
|
129
|
+
- samnang@bongloy.com
|
130
|
+
executables: []
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- ".github/workflows/build.yml"
|
135
|
+
- ".github/workflows/create_release.yml"
|
136
|
+
- ".gitignore"
|
137
|
+
- ".rspec"
|
138
|
+
- ".rubocop.yml"
|
139
|
+
- CODE_OF_CONDUCT.md
|
140
|
+
- Gemfile
|
141
|
+
- Gemfile.lock
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
- Rakefile
|
145
|
+
- bin/console
|
146
|
+
- bin/rspec
|
147
|
+
- bin/setup
|
148
|
+
- cursor_paginator.gemspec
|
149
|
+
- lib/cursor_paginator.rb
|
150
|
+
- lib/cursor_paginator/options_parser.rb
|
151
|
+
- lib/cursor_paginator/pagination_result.rb
|
152
|
+
- lib/cursor_paginator/paginator.rb
|
153
|
+
- lib/cursor_paginator/paginator/active_record.rb
|
154
|
+
- lib/cursor_paginator/paginator/array.rb
|
155
|
+
- lib/cursor_paginator/paginator/base.rb
|
156
|
+
- lib/cursor_paginator/paginator/dynamoid.rb
|
157
|
+
- lib/cursor_paginator/version.rb
|
158
|
+
homepage: https://github.com/bongloy/cursor_paginator
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata:
|
162
|
+
homepage_uri: https://github.com/bongloy/cursor_paginator
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
require_paths:
|
166
|
+
- lib
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: 2.3.0
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - ">="
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
requirements: []
|
178
|
+
rubygems_version: 3.1.2
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: Cursor pagination
|
182
|
+
test_files: []
|