sequel-keyset_pagination 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e196af0224272c18188558ec8985ad9fd6b0cd2
4
+ data.tar.gz: df8465b47dcae2d5c1142af8a8bba914117aa95d
5
+ SHA512:
6
+ metadata.gz: 6cbfcba7713d84c43b2cdbaaa090c0b832600d3a47e9b0d37caccd6d5a3697438f08b563478ac7fa68e9086ca35bdb5fd81e7b78a599e53685d81129991b1c7c
7
+ data.tar.gz: 8237d47d36a49070d34454a61738f4eaa17f621594bd068e4ccc109309496f38968a70723235117fb2484d0be7b845d86dfa957fb7d44a021db717edbb0e05c4
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sequel"
4
+
5
+ module Sequel
6
+ module KeysetPagination
7
+ module Utils
8
+ def self.qualify_order(expression)
9
+ case expression
10
+ when Sequel::SQL::OrderedExpression
11
+ expression
12
+ else
13
+ Sequel::SQL::OrderedExpression.new(expression, false)
14
+ end
15
+ end
16
+
17
+ def self.cursor_conditions(columns, cursor2, reverse: false)
18
+ zipped = columns.zip(cursor2)
19
+ desc = reverse ? :> : :<
20
+ asc = reverse ? :< : :>
21
+
22
+ # Reduce the dimensions
23
+ segments = zipped.each_with_index.reverse_each.reduce([]) do |acc, ((column, cursor), idx)|
24
+ # We always start of with the leaf
25
+ segment = [Sequel[column.expression].send(column.descending ? desc : asc, cursor)]
26
+
27
+ # Scope the leaf to its higher level dimensions
28
+ zipped.slice(0, idx).each do |(col, cur)|
29
+ segment << Sequel[col.expression].send(:=~, cur)
30
+ end
31
+
32
+ acc << Sequel.&(*segment)
33
+ end
34
+ Sequel.|(*segments)
35
+ end
36
+ end
37
+
38
+ def seek(before: nil, after: nil)
39
+ raise ArgumentError, "`before` or `after` is required" unless before || after
40
+
41
+ if opts[:order].nil?
42
+ raise StandardError, "cannot call #seek on a dataset with no order"
43
+ end
44
+
45
+ cursor_size = opts[:order].count
46
+
47
+ if before
48
+ before = [before] unless before.is_a? Array
49
+ raise StandardError, "The `before` cursor has the wrong number of values. Expected #{cursor_size}, received #{before.count}." unless before.count == cursor_size
50
+ end
51
+
52
+ if after
53
+ after = [after] unless after.is_a? Array
54
+ raise StandardError, "The `after` cursor has the wrong number of values. Expected #{cursor_size}, received #{after.count}." unless after.count == cursor_size
55
+ end
56
+
57
+ columns = opts[:order].map { |o| Utils.qualify_order(o) }
58
+ conditions = []
59
+
60
+ if after
61
+ conditions << Utils.cursor_conditions(columns, after)
62
+ end
63
+
64
+ if before
65
+ conditions << Utils.cursor_conditions(columns, before, reverse: true)
66
+ end
67
+
68
+ where(Sequel.&(*conditions))
69
+ end
70
+ end
71
+
72
+ Dataset.register_extension(:keyset_pagination, KeysetPagination)
73
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-keyset_pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sarah Henkens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.12'
27
+ description: Adds support to Sequel for easy cursor based pagination on datasets
28
+ email:
29
+ - sarah.a.henkens@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/sequel/extensions/keyset_pagination.rb
35
+ homepage: https://github.com/sarahhenkens/sequel-keyset_pagination
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.5.2.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Keyset Pagination Extension for Sequel
59
+ test_files: []