module Sequel::Plugins::DefaultsSetter

  1. lib/sequel/plugins/defaults_setter.rb

The defaults_setter plugin makes the column getter methods return the default values for new objects, if the values have not already been set. Example:

# column a default NULL
# column b default 2
album =
album.a # => nil
album.b # => 2
album = 1, b: 3)
album.a # => 1
album.b # => 3

You can manually set default values as well:

Album.default_values[:a] = 4 # => 4

You can also provide procs to set default values:

Album.default_values[:a] = lambda{} # =>

By default, default values returned are not cached: # => false

However, you can turn on caching of default values:

Album.plugin :defaults_setter, cache: true # => false

Note that if the cache is turned on, the cached values are stored in the values hash:

Album.plugin :defaults_setter, cache: true
album =
album.values # => {}
album.values # => {:a =>}


# Make all model subclass instances set defaults (called before loading subclasses)
Sequel::Model.plugin :defaults_setter

# Make the Album class set defaults
Album.plugin :defaults_setter


Public Class

  1. configure

Public Class methods

configure (model, opts=OPTS)

Set the default values based on the model schema. Options:


Cache default values returned in the model's values hash.

[show source]
# File lib/sequel/plugins/defaults_setter.rb, line 54
def self.configure(model, opts=OPTS)
  model.instance_exec do
    @cache_default_values = opts[:cache] if opts.has_key?(:cache)