/usr/lib/ruby/vendor_ruby/sequel/plugins/defaults_setter.rb is in ruby-sequel 3.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | module Sequel
module Plugins
# DefaultsSetter is a simple plugin that sets non-nil/NULL default values upon
# initialize:
#
# # column a default NULL
# # column b default 2
# album = Album.new.values # {:b => 2}
# album = Album.new(:a=>1, :b=>3).values # {:a => 1, :b => 3}
#
# 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
module DefaultsSetter
# Set the default values based on the model schema
def self.configure(model)
model.send(:set_default_values)
end
module ClassMethods
# The default values to set in initialize 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.
attr_reader :default_values
# Set the default values when loading the dataset.
def set_dataset(*)
x = super
set_default_values
x
end
private
def set_default_values
h = {}
@db_schema.each{|k, v| h[k] = v[:ruby_default] if v[:ruby_default]} if @db_schema
@default_values = h
end
end
module InstanceMethods
private
# Set default values if they are not already set by the hash provided to initialize.
def initialize_set(h)
super
model.default_values.each{|k,v| self[k] = (v.respond_to?(:call) ? v.call : v) unless values.has_key?(k)}
end
end
end
end
end
|