File: any_not_empty.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen-string-literal: true
#
# The any_not_empty extension changes the behavior of Dataset#any?
# if called without a block.  By default, this method uses the
# standard Enumerable behavior of enumerating results and seeing
# if any result is not false or nil.  With this extension, it
# just checks whether the dataset is empty.  This approach can
# be much faster if the dataset is currently large.
#
#   DB[:table].any?
#   # SELECT * FROM table
#
#   DB[:table].extension(:any_not_empty).any?
#   # SELECT 1 as one FROM table LIMIT 1
#   
# You can load this extension into specific datasets:
#
#   ds = DB[:table]
#   ds = ds.extension(:any_not_empty)
#
# Or you can load it into all of a database's datasets, which
# is probably the desired behavior if you are using this extension:
#
#   DB.extension(:any_not_empty)
#
# Note that this can result in any? returning a different result if
# the dataset has a row_proc that can return false or nil.
#
# Related module: Sequel::AnyNotEmpty

#
module Sequel
  module AnyNotEmpty
    # If a block is not given, return whether the dataset is not empty.
    def any?
      if defined?(yield)
        super
      else
        !empty?
      end
    end
  end

  Dataset.register_extension(:any_not_empty, AnyNotEmpty)
end