sequel_count_comparisons 1.0.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
+ SHA256:
3
+ metadata.gz: b0dda31dca4ef83af94b35f825f9625af8dcb097804e73b5f616c3a2af3c99f2
4
+ data.tar.gz: 8120c91e4dce2d96d210372f15d559cae1945c1f0e09ed3c0f57b2f4dc0790ae
5
+ SHA512:
6
+ metadata.gz: 05db3f4b5803e51c9e21453898178a57da956cb36b516870ad78c4f7cbba014d4c80dadc95a79bc5781fb8184de0ab605faa6d65f7a5f09d273f4a72f2701bd7
7
+ data.tar.gz: 25ea2ae4e012e05c5e9e1abf3993b41a9900c1f50c9b97d63d0f23bfc00a2e2369b1279b91cc6a1f4715798f4904c903c2562044bf0e4e3b990fd34b6926b6f3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 (2025-03-10)
2
+
3
+ * Initial public release
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Sequel::CountComparisons
2
+
3
+ This gem adds five methods to `Sequel::Dataset`:
4
+
5
+ * `#count_equals?`
6
+ * `#count_greater_than?`
7
+ * `#count_less_than?`
8
+ * `#count_at_most?`
9
+ * `#count_at_least?`
10
+
11
+ These methods allow you to efficiently compare a dataset's row count to a given number.
12
+
13
+ Why use this gem?
14
+
15
+ * It generates efficient SQL queries to minimize database overhead, using
16
+ `LIMIT`
17
+ and `OFFSET` clauses to count only the rows needed. This is especially useful
18
+ for large datasets.
19
+ * It provides a readable API for expressing dataset count comparisons, which
20
+ makes code easier to understand and less error-prone.
21
+
22
+ ## Installation
23
+
24
+ Install the gem and add to the application's Gemfile by executing:
25
+
26
+ $ bundle add sequel_count_comparisons
27
+
28
+ If Bundler is not being used to manage dependencies, install the gem by executing:
29
+
30
+ $ gem install sequel_count_comparisons
31
+
32
+ ## Usage
33
+
34
+ You can load this extension into specific datasets:
35
+
36
+ ```ruby
37
+ ds = DB[:table].extension(:count_comparisons)
38
+ ``````
39
+
40
+ Or you can load it into all of a database’s datasets:
41
+
42
+ ```ruby
43
+ DB.extension(:count_comparisons)
44
+
45
+ DB[:table].count_equals?(1)
46
+ # SELECT EXISTS (SELECT 1 FROM table OFFSET 0) AND NOT EXISTS (SELECT 1 FROM table OFFSET 1)
47
+
48
+ DB[:table].count_greater_than?(0)
49
+ # SELECT 1 FROM table LIMIT 1
50
+
51
+ DB[:table].count_greater_than?(1)
52
+ # SELECT 1 FROM table LIMIT 1 OFFSET 1
53
+
54
+ DB[:table].count_less_than?(1)
55
+ # SELECT 1 FROM table LIMIT 1
56
+
57
+ DB[:table].count_less_than?(5)
58
+ # SELECT 1 FROM table LIMIT 1 OFFSET 4
59
+
60
+ DB[:table].count_at_least?(3)
61
+ # SELECT 1 FROM table LIMIT 1 OFFSET 2
62
+
63
+ DB[:table].count_at_most?(100)
64
+ # SELECT 1 FROM table LIMIT 1 OFFSET 100
65
+
66
+ ```
67
+
68
+ ## Caveats
69
+
70
+ The gem is tested on PostgreSQL. It may or may not work for other databases.
71
+
72
+ ## License
73
+
74
+ This library is released under the MIT License.
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eriklovmo/sequel_count_comparisons.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: %i[spec]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sequel
4
+ module CountComparisons
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sequel'
4
+ require_relative '../count_comparisons/version'
5
+
6
+ # The count_comparisons extension adds the #count_greater_than?, #count_less_than?, #count_at_most?,
7
+ # #count_at_least?, and #count_equals? methods to Dataset. These methods can be used to efficiently
8
+ # compare a dataset's row count to a given number.
9
+
10
+ # You can load this extension into specific datasets:
11
+ #
12
+ # ds = DB[:table].extension(:count_comparisons)
13
+ #
14
+ # Or load it into all of a database's datasets:
15
+ #
16
+ # DB.extension(:count_comparisons)
17
+
18
+ module Sequel
19
+ module Extensions
20
+ module CountComparisons
21
+ LITERAL_1 = Sequel::SQL::AliasedExpression.new(1, :one)
22
+ private_constant :LITERAL_1
23
+
24
+ # Returns true if more than *number_of_rows* records exist in the dataset, false otherwise
25
+ #
26
+ # Equivalent to a "greater than" (>) comparison
27
+ #
28
+ # @param number_of_rows [Integer] The number to compare against
29
+ # @return [Boolean] Whether the dataset contains more rows than *number_of_rows*
30
+ # @raise [ArgumentError] If `number_of_rows` is not an integer
31
+ def count_greater_than?(number_of_rows)
32
+ unless number_of_rows.is_a?(Integer)
33
+ raise ArgumentError,
34
+ "`number_of_rows` must be an Integer, got #{number_of_rows.inspect}"
35
+ end
36
+
37
+ if number_of_rows.negative?
38
+ true
39
+ elsif number_of_rows.zero?
40
+ !empty?
41
+ else
42
+ ds = @opts[:sql] ? from_self : self
43
+ !ds.offset(number_of_rows).empty?
44
+ end
45
+ end
46
+
47
+ # Returns true if fewer than *number_of_rows* records exist in the dataset, false otherwise
48
+ #
49
+ # Equivalent to a "less than" (<) comparison
50
+ #
51
+ # @param number_of_rows [Integer] The number to compare against
52
+ # @return [Boolean] Whether the dataset contains fewer rows than *number_of_rows*
53
+ # @raise [ArgumentError] If `number_of_rows` is not an integer
54
+ def count_less_than?(number_of_rows)
55
+ unless number_of_rows.is_a?(Integer)
56
+ raise ArgumentError,
57
+ "`number_of_rows` must be an Integer, got #{number_of_rows.inspect}"
58
+ end
59
+
60
+ !count_greater_than?(number_of_rows - 1)
61
+ end
62
+
63
+ # Returns true if at least *number_of_rows* records exist in the dataset, false otherwise
64
+ #
65
+ # Equivalent to a "greater than or equal to" (>=) comparison
66
+ #
67
+ # @param number_of_rows [Integer] The number to compare against
68
+ # @return [Boolean] Whether the dataset contains at least *number_of_rows*
69
+ # @raise [ArgumentError] If `number_of_rows` is not an integer
70
+ def count_at_least?(number_of_rows)
71
+ unless number_of_rows.is_a?(Integer)
72
+ raise ArgumentError,
73
+ "`number_of_rows` must be an Integer, got #{number_of_rows.inspect}"
74
+ end
75
+
76
+ count_greater_than?(number_of_rows - 1)
77
+ end
78
+
79
+ # Returns true if at most *number_of_rows* records exist in the dataset, false otherwise
80
+ #
81
+ # Equivalent to a "less than or equal to" (<=) comparison
82
+ #
83
+ # @param number_of_rows [Integer] The number to compare against
84
+ # @return [Boolean] Whether the dataset contains at most *number_of_rows*
85
+ # @raise [ArgumentError] If `number_of_rows` is not an integer
86
+ def count_at_most?(number_of_rows)
87
+ !count_greater_than?(number_of_rows)
88
+ end
89
+
90
+ # Returns true if exactly *number_of_rows* records exist in the dataset, false otherwise
91
+ #
92
+ # Equivalent to an "equal to" (==) comparison
93
+ #
94
+ # @param number_of_rows [Integer] The number to compare against
95
+ # @return [Boolean] Whether the dataset contains exactly *number_of_rows*
96
+ # @raise [ArgumentError] If `number_of_rows` is not an integer
97
+ def count_equals?(number_of_rows)
98
+ unless number_of_rows.is_a?(Integer)
99
+ raise ArgumentError,
100
+ "`number_of_rows` must be an Integer, got #{number_of_rows.inspect}"
101
+ end
102
+
103
+ if number_of_rows.negative?
104
+ false
105
+ elsif number_of_rows.zero?
106
+ empty?
107
+ else
108
+ ds = @opts[:sql] ? from_self : self
109
+ ds = ds.unordered.select(LITERAL_1)
110
+ @db.get(
111
+ ds.offset(number_of_rows - 1).exists &
112
+ ~ds.offset(number_of_rows).exists
113
+ )
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ Sequel::Dataset.register_extension(:count_comparisons, Sequel::Extensions::CountComparisons)
120
+ end
data/license.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Erik Lövmo
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.
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_count_comparisons
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - eriklovmo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-10 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: standard
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
+ description:
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - CHANGELOG
63
+ - README.md
64
+ - Rakefile
65
+ - lib/sequel/count_comparisons/version.rb
66
+ - lib/sequel/extensions/count_comparisons.rb
67
+ - license.txt
68
+ homepage: https://github.com/eriklovmo/sequel_count_comparisons
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ homepage_uri: https://github.com/eriklovmo/sequel_count_comparisons
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 3.0.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.5.16
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: 'Adds #count_equals?, #count_greater_than?, #count_less_than?, #count_at_least?
92
+ and #count_at_most? to Sequel::Dataset'
93
+ test_files: []