module Sequel::Plugins::BooleanReaders

  1. lib/sequel/plugins/boolean_readers.rb

The BooleanReaders plugin allows for the creation of attribute? methods for boolean columns, which provides a nicer API. By default, the accessors are created for all columns of type :boolean. However, you can provide a block to the plugin to change the criteria used to determine if a column is boolean. The block is yielded with the column symbol for each column in the models dataset.

Usage:

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

# Add boolean readers for all tinyint columns in the Album class
Album.plugin(:boolean_readers){|c| db_schema[c][:db_type] =~ /\Atinyint/}

# Add a boolean reader for a specific columns in the Artist class
Artist.plugin(:boolean_readers){|c| [:column1, :column2, :column3].include?(c)}

Methods

Public Class

  1. configure

Constants

DEFAULT_BOOLEAN_ATTRIBUTE_PROC = lambda{|c| s = db_schema[c] and s[:type] == :boolean}  

Default proc for determining if given column is a boolean, which just checks that the :type is boolean.

Public Class methods

configure (model, &block)

Add the boolean_attribute? class method to the model, and create attribute? boolean reader methods for the class's columns if the class has a dataset.

[show source]
# File lib/sequel/plugins/boolean_readers.rb, line 29
def self.configure(model, &block)
  model.instance_eval do
    (class << self; self; end).send(:define_method, :boolean_attribute?, &(block || DEFAULT_BOOLEAN_ATTRIBUTE_PROC))
    send(:create_boolean_readers) if @dataset
  end
end