module Sequel::Plugins::DefaultsSetter::ClassMethods

Attributes

default_values[R]

The default values to use for this model. A hash with column symbol keys and default values. If the default values respond to call, it will be called to get the value, otherwise the value will be used directly. You can manually modify this hash to set specific default values, by default the ones will be parsed from the database.

Public Instance Methods

cache_default_values?() click to toggle source

Whether default values should be cached in the values hash after being retrieved.

# File lib/sequel/plugins/defaults_setter.rb, line 75
def cache_default_values?
  @cache_default_values
end
freeze() click to toggle source

Freeze default values when freezing model class

Calls superclass method
# File lib/sequel/plugins/defaults_setter.rb, line 80
def freeze
  @default_values.freeze
  super
end

Private Instance Methods

convert_default_value(v) click to toggle source

Handle the CURRENT_DATE and CURRENT_TIMESTAMP values specially by returning an appropriate Date or Time/DateTime value.

# File lib/sequel/plugins/defaults_setter.rb, line 104
def convert_default_value(v)
  case v
  when Sequel::CURRENT_DATE
    lambda{Date.today}
  when Sequel::CURRENT_TIMESTAMP
    lambda{dataset.current_datetime}
  when Hash, Array
    v = Marshal.dump(v).freeze
    lambda{Marshal.load(v)}
  when Delegator
    # DelegateClass returns an anonymous case, which cannot be marshalled, so marshal the
    # underlying object and create a new instance of the class with the unmarshalled object.
    klass = v.class
    case o = v.__getobj__
    when Hash, Array
      v = Marshal.dump(o).freeze
      lambda{klass.new(Marshal.load(v))}
    else
      v
    end
  else
    v
  end
end
set_default_values() click to toggle source

Parse the cached database schema for this model and set the default values appropriately.

# File lib/sequel/plugins/defaults_setter.rb, line 88
def set_default_values
  h = {}
  if @db_schema
    @db_schema.each do |k, v|
      if v[:callable_default]
        h[k] = v[:callable_default]
      elsif !v[:ruby_default].nil?
        h[k] = convert_default_value(v[:ruby_default])
      end
    end
  end
  @default_values = h.merge!(@default_values || OPTS)
end