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
76 def cache_default_values?
77   @cache_default_values
78 end
freeze() click to toggle source

Freeze default values when freezing model class

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