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.new
album.a # => nil
album.b # => 2
album = Album.new(a: 1, b: 3)
album.a # => 1
album.b # => 3

You can manually set default values as well:

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

You can also provide procs to set default values:

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

Usage:

# 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

Methods

Public Class

  1. configure

Public Class methods

configure (model)

Set the default values based on the model schema

[show source]
# File lib/sequel/plugins/defaults_setter.rb, line 35
def self.configure(model)
  model.send(:set_default_values)
end