module Sequel::Plugins::AutoValidations::ClassMethods

Attributes

auto_validate_explicit_not_null_columns[R]

The columns with automatic not_null validations for columns present in the values.

auto_validate_max_length_columns[R]

The columns or sets of columns with automatic max_length validations, as an array of pairs, with the first entry being the column name and second entry being the maximum length.

auto_validate_not_null_columns[R]

The columns with automatic not_null validations

auto_validate_options[R]

Inherited options

auto_validate_unique_columns[R]

The columns or sets of columns with automatic unique validations

Public Instance Methods

auto_validate_presence?() click to toggle source

Whether to use a presence validation for not null columns

    # File lib/sequel/plugins/auto_validations.rb
121 def auto_validate_presence?
122   @auto_validate_presence
123 end
auto_validate_types?() click to toggle source

Whether to automatically validate schema types for all columns

    # File lib/sequel/plugins/auto_validations.rb
126 def auto_validate_types?
127   @auto_validate_types
128 end
freeze() click to toggle source

Freeze auto_validation settings when freezing model class.

Calls superclass method
    # File lib/sequel/plugins/auto_validations.rb
131 def freeze
132   @auto_validate_not_null_columns.freeze
133   @auto_validate_explicit_not_null_columns.freeze
134   @auto_validate_max_length_columns.freeze
135   @auto_validate_unique_columns.freeze
136 
137   super
138 end
skip_auto_validations(type) click to toggle source

Skip automatic validations for the given validation type (:not_null, :types, :unique). If :all is given as the type, skip all auto validations.

    # File lib/sequel/plugins/auto_validations.rb
142 def skip_auto_validations(type)
143   case type
144   when :all
145     [:not_null, :types, :unique, :max_length].each{|v| skip_auto_validations(v)}
146   when :not_null
147     auto_validate_not_null_columns.clear
148     auto_validate_explicit_not_null_columns.clear
149   when :types
150     @auto_validate_types = false
151   else
152     public_send("auto_validate_#{type}_columns").clear
153   end
154 end

Private Instance Methods

setup_auto_validations() click to toggle source

Parse the database schema and indexes and record the columns to automatically validate.

    # File lib/sequel/plugins/auto_validations.rb
159 def setup_auto_validations
160   not_null_cols, explicit_not_null_cols = db_schema.select{|col, sch| sch[:allow_null] == false}.partition{|col, sch| sch[:default].nil?}.map{|cs| cs.map{|col, sch| col}}
161   @auto_validate_not_null_columns = not_null_cols - Array(primary_key)
162   explicit_not_null_cols += Array(primary_key)
163   @auto_validate_explicit_not_null_columns = explicit_not_null_cols.uniq
164   @auto_validate_max_length_columns = db_schema.select{|col, sch| sch[:type] == :string && sch[:max_length].is_a?(Integer)}.map{|col, sch| [col, sch[:max_length]]}
165   table = dataset.first_source_table
166   @auto_validate_unique_columns = if db.supports_index_parsing? && [Symbol, SQL::QualifiedIdentifier, SQL::Identifier, String].any?{|c| table.is_a?(c)}
167     db.indexes(table).select{|name, idx| idx[:unique] == true}.map{|name, idx| idx[:columns].length == 1 ? idx[:columns].first : idx[:columns]}
168   else
169     []
170   end
171 end