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
82 def cache_default_values?
83   @cache_default_values
84 end
freeze() click to toggle source

Freeze default values when freezing model class

Calls superclass method
   # File lib/sequel/plugins/defaults_setter.rb
87 def freeze
88   @default_values.freeze
89   super
90 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
111 def convert_default_value(v)
112   case v
113   when Sequel::CURRENT_DATE
114     lambda{Date.today}
115   when Sequel::CURRENT_TIMESTAMP
116     lambda{dataset.current_datetime}
117   when Hash, Array
118     v = Marshal.dump(v).freeze
119     lambda{Marshal.load(v)}
120   when Delegator
121     # DelegateClass returns an anonymous case, which cannot be marshalled, so marshal the
122     # underlying object and create a new instance of the class with the unmarshalled object.
123     klass = v.class
124     case o = v.__getobj__
125     when Hash, Array
126       v = Marshal.dump(o).freeze
127       lambda{klass.new(Marshal.load(v))}
128     else
129       v
130     end
131   else
132     v
133   end
134 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
 95 def set_default_values
 96   h = {}
 97   if @db_schema
 98     @db_schema.each do |k, v|
 99       if v[:callable_default]
100         h[k] = v[:callable_default]
101       elsif !v[:ruby_default].nil?
102         h[k] = convert_default_value(v[:ruby_default])
103       end
104     end
105   end
106   @default_values = h.merge!(@default_values || OPTS)
107 end