module Sequel::Plugins::BooleanSubsets

  1. lib/sequel/plugins/boolean_subsets.rb

The boolean_subsets plugin allows for the automatic creation of subsets for for boolean columns, which can DRY up model classes that define such subsets manually. By default, subsets are created for all columns of type :boolean, with the subset name being the same as column name, and the conditions being column IS TRUE (assuming the database supports that syntax).

You can provide a block to the plugin, which will be called with column name symbol, and should return an array of arguments to pass to dataset_module.where. Using this, you can change the method name and arguments for each column. This block is executed in the context of the model class.

Usage:

# Add boolean subset methods for all columns of type :boolean
# in all model subclasses (called before loading subclasses)
Sequel::Model.plugin :boolean_subsets

# Add subsets for all boolean columns in the Album class
Album.plugin(:boolean_subsets)

# Remove is_ from the front of the column name when creating the subset
# method name, and use (column = 'Y') as the filter conditions
Sequel::Model.plugin :boolean_subsets do |column|
  [column.to_s.sub(/\Ais_/, ''), {column=>'Y'}]
end

Methods

Public Class

  1. configure

Public Class methods

configure(model, &block)

Create boolean subset methods for each boolean column.

[show source]
   # File lib/sequel/plugins/boolean_subsets.rb
32 def self.configure(model, &block)
33   model.instance_exec do
34     if block
35       define_singleton_method(:boolean_subset_args, &block)
36       singleton_class.send(:private, :boolean_subset_args)
37     end
38     create_boolean_subsets if @dataset
39   end
40 end