Extend the dataset with a module, similar to adding a plugin with the
methods defined in DatasetMethods. This
is the recommended way to add methods to model datasets.
If an argument, it should be a module, and is used to extend the underlying
dataset. Otherwise an anonymous module is created, and if a block is
given, it is module_evaled, allowing you do define dataset methods directly
using the standard ruby def syntax. Returns the module given or the
anonymous module created.
Album.dataset_module Sequel::ColumnsIntrospection
Album.dataset_module do
def foo
:bar
end
end
Album.dataset.foo
Album.foo
Any anonymous modules created are actually instances of Sequel::Model::DatasetModule (a Module
subclass), which allows you to call the subset method on them, which
defines a dataset method that adds a filter. There are also a number of
other methods with the same names as the dataset methods, which can use to
define named dataset methods:
Album.dataset_module do
subset :released, Sequel.identifier(release_date) <= Sequel::CURRENT_DATE
order :by_release_date, :release_date
select :for_select_options, :id, :name, :release_date
end
Album.released.sql
Album.by_release_date.sql
Album.for_select_options.sql
Album.released.by_release_date.for_select_options.sql
The following methods are supported: distinct, eager, exclude,
exclude_having, grep, group, group_and_count, group_append, having, limit,
offset, order, order_append, order_prepend, select, select_all,
select_append, select_group, where, and server.
Any public methods in the dataset module will have class methods created
that call the method on the dataset, assuming that the class method is not
already defined.
def dataset_module(mod = nil)
if mod
raise Error, "can't provide both argument and block to Model.dataset_module" if block_given?
dataset_extend(mod)
mod
else
@dataset_module ||= dataset_module_class.new(self)
@dataset_module.module_eval(&Proc.new) if block_given?
dataset_extend(@dataset_module)
@dataset_module
end
end