Sha256: 18871396206c23277e58c4ad07d065cfe414953200057833caea2082436c1a90

Contents?: true

Size: 1.72 KB

Versions: 98

Compression:

Stored size: 1.72 KB

Contents

# frozen-string-literal: true

module Sequel
  module Plugins
    # The inverted_subsets plugin adds another method for each defined
    # subset, which inverts the condition supplied. By default, inverted
    # subset method names are prefixed with not_.
    #
    # You can change the prefix, or indeed entirely customise the inverted names,
    # by passing a block to the plugin configuration:
    #
    #   # Use an exclude_ prefix for inverted subsets instead of not_
    #   Album.plugin(:inverted_subsets){|name| "exclude_#{name}"}
    #
    # Usage:
    #
    #   # Add inverted subsets in the Album class
    #   Album.plugin :inverted_subsets
    #
    #   # This will now create two methods, published and not_published
    #   Album.dataset_module do
    #     where :published, published: true
    #   end
    #
    #   Album.published.sql
    #   # SELECT * FROM albums WHERE (published IS TRUE)
    #
    #   Album.not_published.sql
    #   # SELECT * FROM albums WHERE (published IS NOT TRUE)
    #
    module InvertedSubsets
      def self.apply(model, &block)
        model.instance_exec do
          @dataset_module_class = Class.new(@dataset_module_class) do
            include DatasetModuleMethods
            if block
              define_method(:inverted_subset_name, &block)
              private :inverted_subset_name
            end
          end
        end
      end

      module DatasetModuleMethods
        # Define a not_ prefixed subset which inverts the subset condition.
        def where(name, *args, &block)
          super
          exclude(inverted_subset_name(name), *args, &block)
        end

        private

        def inverted_subset_name(name)
          "not_#{name}"
        end
      end
    end
  end
end

Version data entries

98 entries across 84 versions & 2 rubygems

Version Path
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/sequel-5.53.0/lib/sequel/plugins/inverted_subsets.rb
sequel-5.53.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.52.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.51.0 lib/sequel/plugins/inverted_subsets.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/sequel-5.50.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/plugins/inverted_subsets.rb
sequel-5.50.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.49.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.48.0 lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/sequel-5.47.0/lib/sequel/plugins/inverted_subsets.rb
sequel-5.47.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.46.0 lib/sequel/plugins/inverted_subsets.rb
sequel-5.45.0 lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/sequel-5.39.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/sequel-5.43.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/sequel-5.38.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/sequel-5.41.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/sequel-5.41.0/lib/sequel/plugins/inverted_subsets.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/plugins/inverted_subsets.rb