activerecord-cursor 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2535f2c0d21228e6cfba576bc8ee249fb1e5fbe
4
+ data.tar.gz: df2ba240dcc29b2b5b9113a3fd74d89b7c6f080a
5
+ SHA512:
6
+ metadata.gz: 4346da805065a8595341f7d5a993e533ade462b4a72584c886db54ef36f4a33f64d970a3240bf8a7a134d5a08a4ee7297ad6155ee9a14d44c09d2edc487a60f3
7
+ data.tar.gz: 4a8cb139ef303251fe7d198a815e957a90153954b6e21c9966123e055c5d1e6bf208171c5ce27581291c815275b505625f79124b400b52d10814e298bd498596
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ Style/Documentation:
4
+ Enabled: false
5
+ Style/FileName:
6
+ Enabled: false
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'spec/**/*'
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,26 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-12-31 19:03:18 +0900 using RuboCop version 0.61.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 3
10
+ Metrics/AbcSize:
11
+ Max: 18
12
+
13
+ # Offense count: 1
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 7
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: CountComments, ExcludedMethods.
19
+ Metrics/MethodLength:
20
+ Max: 11
21
+
22
+ # Offense count: 7
23
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
24
+ # URISchemes: http, https
25
+ Metrics/LineLength:
26
+ Max: 116
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.0
4
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in activerecord-cursor.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # ActiveRecord::Cursor
2
+
3
+ cursor pagination for ActiveRecord
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-cursor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-cursor
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Post.all
25
+ => [
26
+ #<Post id: 1, published: true, score: 0, created_at: "2018-12-30 19:38:27", updated_at: "2018-12-30 19:38:27">,
27
+ #<Post id: 2, published: false, score: 1, created_at: "2018-12-30 19:38:28", updated_at: "2018-12-30 19:38:28">,
28
+ #<Post id: 3, published: true, score: 1, created_at: "2018-12-30 19:38:29", updated_at: "2018-12-30 19:38:29">,
29
+ #<Post id: 4, published: false, score: 2, created_at: "2018-12-30 19:38:30", updated_at: "2018-12-30 19:38:30">
30
+ ]
31
+ Post.cursor(key: :created_at)
32
+ => [#<Post id: 1, published: false, created_at: "2018-12-30 19:38:27", updated_at: "2018-12-30 19:38:27">]
33
+ Post.where(published: true).cursor(key: :created_at, reverse: true)
34
+ => [#<Post id: 3, published: true, score: 1, created_at: "2018-12-30 19:38:29", updated_at: "2018-12-30 19:38:29">]
35
+ Post.where(published: true).cursor(key: :created_at, reverse: true, start: Post.next_cursor) # Get next page
36
+ => [#<Post id: 1, published: true, score: 0, created_at: "2018-12-30 19:38:27", updated_at: "2018-12-30 19:38:27">]
37
+ Post.where(published: true).cursor(key: :created_at, reverse: true, stop: Post.prev_cursor) # Get previous page
38
+ => [#<Post id: 3, published: true, score: 1, created_at: "2018-12-30 19:38:29", updated_at: "2018-12-30 19:38:29">]
39
+ ```
40
+
41
+ ### Options
42
+
43
+ - `key`: Cursor key. Defult: 'id'
44
+ - `reverse`: Set it to true, if your set are ordered descendingly (DESC). Default: false
45
+ - `size`: Page size. Default: 1
46
+ - `start`: Cursor value
47
+ - `stop`: Cursor value
48
+
49
+ ## Development
50
+
51
+ 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.
52
+
53
+ 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).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tsuwatch/activerecord-cursor.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'activerecord/cursor/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activerecord-cursor'
7
+ spec.version = ActiveRecord::Cursor::VERSION
8
+ spec.authors = ['Tomohiro Suwa']
9
+ spec.email = ['neoen.gsn@gmail.com']
10
+
11
+ spec.summary = 'pagination using cursors for ActiveRecord'
12
+ spec.description = 'pagination using cursors for ActiveRecord'
13
+ spec.homepage = 'https://github.com/tsuwatch/activerecord-curosr'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activerecord'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.15'
25
+ spec.add_development_dependency 'coveralls'
26
+ spec.add_development_dependency 'database_cleaner'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'rubocop'
30
+ spec.add_development_dependency 'sqlite3'
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'activerecord/cursor'
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/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+ require 'activerecord/cursor/version'
3
+
4
+ module ActiveRecord
5
+ module Cursor
6
+ class InvalidCursor < StandardError; end
7
+ end
8
+ end
9
+
10
+ ActiveSupport.on_load(:active_record) do
11
+ require 'activerecord/cursor/extension'
12
+ ActiveRecord::Base.send(:include, ActiveRecord::Cursor::Extension)
13
+ end
@@ -0,0 +1,22 @@
1
+ require 'activerecord/cursor/model_extension'
2
+
3
+ module ActiveRecord
4
+ module Cursor
5
+ module Extension
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def inherited(kls)
10
+ super
11
+ kls.public_send(:include, ActiveRecord::Cursor::ModelExtension) if kls.superclass == ActiveRecord::Base
12
+ end
13
+ end
14
+
15
+ included do
16
+ descendants.each do |kls|
17
+ kls.public_send(:include, ActiveRecord::Cursor::ModelExtension) if kls.superclass == ApplicationRecord
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,118 @@
1
+ require 'activerecord/cursor/params'
2
+
3
+ module ActiveRecord
4
+ module Cursor
5
+ module ModelExtension
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def cursor(options = {})
10
+ @options = default_options.merge!(options).symbolize_keys!
11
+ @options[:direction] =
12
+ if @options.key?(:start) || @options.key?(:stop)
13
+ @options.key?(:start) ? :start : :stop
14
+ end
15
+ @cursor = Params.decode(@options[@options[:direction]]).value
16
+ @records = on_cursor.in_order.limit(@options[:size] + 1)
17
+ set_cursor
18
+ @records
19
+ rescue ActiveRecord::StatementInvalid
20
+ raise Cursor::InvalidCursor
21
+ end
22
+
23
+ def next_cursor
24
+ @next
25
+ end
26
+
27
+ def prev_cursor
28
+ @prev
29
+ end
30
+
31
+ def on_cursor
32
+ if @cursor.nil?
33
+ where(nil)
34
+ else
35
+ where(
36
+ "(#{column} = ? AND #{table_name}.id #{sign_of_inequality} ?) OR (#{column} #{sign_of_inequality} ?)",
37
+ @cursor[:key],
38
+ @cursor[:id],
39
+ @cursor[:key]
40
+ )
41
+ end
42
+ end
43
+
44
+ def in_order
45
+ reorder("#{column} #{by}", "#{table_name}.id #{by}")
46
+ end
47
+
48
+ private
49
+
50
+ def default_options
51
+ { key: 'id', reverse: false, size: 1 }
52
+ end
53
+
54
+ def column
55
+ "#{table_name}.#{@options[:key]}"
56
+ end
57
+
58
+ def sign_of_inequality
59
+ case @options[:reverse]
60
+ when true
61
+ @options[:direction] == :start ? '<' : '>'
62
+ when false
63
+ @options[:direction] == :start ? '>' : '<'
64
+ end
65
+ end
66
+
67
+ def by
68
+ direction = @options[:direction]
69
+ case @options[:reverse]
70
+ when true
71
+ direction == :start || direction.nil? ? 'desc' : 'asc'
72
+ when false
73
+ direction == :start || direction.nil? ? 'asc' : 'desc'
74
+ end
75
+ end
76
+
77
+ def set_cursor
78
+ @next = nil
79
+ @prev = nil
80
+ if @options[:direction] == :start
81
+ set_cursor_on_start
82
+ elsif @options[:direction] == :stop
83
+ set_cursor_on_stop
84
+ elsif @records.size == @options[:size] + 1
85
+ @records = @records.limit(@options[:size])
86
+ @next = generate_cursor(@records[@records.size - 1])
87
+ end
88
+ end
89
+
90
+ def set_cursor_on_start
91
+ record = @records[0]
92
+ @prev = generate_cursor(record) if record
93
+ size = @records.size
94
+ @records = @records.limit(@options[:size])
95
+ return unless size == @options[:size] + 1
96
+
97
+ @next = generate_cursor(@records[@records.size - 1])
98
+ end
99
+
100
+ def set_cursor_on_stop
101
+ record = @records[0]
102
+ @next = generate_cursor(record) if record
103
+ size = @records.size
104
+ @records = @records.limit(@options[:size]).sort do |a, b|
105
+ b.public_send(@options[:key]) <=> a.public_send(@options[:key])
106
+ end
107
+ return unless size == @options[:size] + 1
108
+
109
+ @prev = generate_cursor(record)
110
+ end
111
+
112
+ def generate_cursor(record)
113
+ Params.new(id: record.id, key: record.public_send(@options[:key])).encoded
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,32 @@
1
+ module ActiveRecord
2
+ module Cursor
3
+ class Params
4
+ attr_reader :cursor
5
+
6
+ def initialize(cursor)
7
+ @cursor = cursor
8
+ end
9
+
10
+ def self.decode(cursor)
11
+ if cursor.nil?
12
+ new nil
13
+ else
14
+ new YAML.safe_load(
15
+ Base64.urlsafe_decode64(cursor),
16
+ [Symbol, Time, ActiveSupport::TimeZone, ActiveSupport::TimeWithZone]
17
+ ).with_indifferent_access
18
+ end
19
+ rescue Psych::SyntaxError
20
+ raise InvalidCursor
21
+ end
22
+
23
+ def encoded
24
+ Base64.urlsafe_encode64 cursor.to_yaml
25
+ end
26
+
27
+ def value
28
+ @cursor
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Cursor
3
+ VERSION = '0.2.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-cursor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomohiro Suwa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
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: database_cleaner
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: pagination using cursors for ActiveRecord
126
+ email:
127
+ - neoen.gsn@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".rubocop_todo.yml"
136
+ - ".travis.yml"
137
+ - Gemfile
138
+ - README.md
139
+ - Rakefile
140
+ - activerecord-cursor.gemspec
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/activerecord-cursor.rb
144
+ - lib/activerecord/cursor/extension.rb
145
+ - lib/activerecord/cursor/model_extension.rb
146
+ - lib/activerecord/cursor/params.rb
147
+ - lib/activerecord/cursor/version.rb
148
+ homepage: https://github.com/tsuwatch/activerecord-curosr
149
+ licenses: []
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.4.5.1
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: pagination using cursors for ActiveRecord
171
+ test_files: []