Class Index [+]

Quicksearch

Sequel::Plugins::ValidationHelpers::InstanceMethods

Public Instance Methods

validates_exact_length(exact, atts, opts={}) click to toggle source

Check that the attribute values are the given exact length.

    # File lib/sequel/plugins/validation_helpers.rb, line 64
64:         def validates_exact_length(exact, atts, opts={})
65:           validatable_attributes_for_type(:exact_length, atts, opts){|a,v,m| validation_error_message(m, exact) unless v && v.length == exact}
66:         end
validates_format(with, atts, opts={}) click to toggle source

Check the string representation of the attribute value(s) against the regular expression with.

    # File lib/sequel/plugins/validation_helpers.rb, line 69
69:         def validates_format(with, atts, opts={})
70:           validatable_attributes_for_type(:format, atts, opts){|a,v,m| validation_error_message(m, with) unless v.to_s =~ with}
71:         end
validates_includes(set, atts, opts={}) click to toggle source

Check attribute value(s) is included in the given set.

    # File lib/sequel/plugins/validation_helpers.rb, line 74
74:         def validates_includes(set, atts, opts={})
75:           validatable_attributes_for_type(:includes, atts, opts){|a,v,m| validation_error_message(m, set) unless set.send(set.respond_to?(:cover?) ? :cover? : :include?, v)}
76:         end
validates_integer(atts, opts={}) click to toggle source

Check attribute value(s) string representation is a valid integer.

    # File lib/sequel/plugins/validation_helpers.rb, line 79
79:         def validates_integer(atts, opts={})
80:           validatable_attributes_for_type(:integer, atts, opts) do |a,v,m|
81:             begin
82:               Kernel.Integer(v.to_s)
83:               nil
84:             rescue
85:               validation_error_message(m)
86:             end
87:           end
88:         end
validates_length_range(range, atts, opts={}) click to toggle source

Check that the attribute values length is in the specified range.

    # File lib/sequel/plugins/validation_helpers.rb, line 91
91:         def validates_length_range(range, atts, opts={})
92:           validatable_attributes_for_type(:length_range, atts, opts){|a,v,m| validation_error_message(m, range) unless v && range.send(range.respond_to?(:cover?) ? :cover? : :include?, v.length)}
93:         end
validates_max_length(max, atts, opts={}) click to toggle source

Check that the attribute values are not longer than the given max length.

Accepts a :nil_message option that is the error message to use when the value is nil instead of being too long.

     # File lib/sequel/plugins/validation_helpers.rb, line 99
 99:         def validates_max_length(max, atts, opts={})
100:           validatable_attributes_for_type(:max_length, atts, opts){|a,v,m| v ? validation_error_message(m, max) : validation_error_message(opts[:nil_message] || DEFAULT_OPTIONS[:max_length][:nil_message]) unless v && v.length <= max}
101:         end
validates_min_length(min, atts, opts={}) click to toggle source

Check that the attribute values are not shorter than the given min length.

     # File lib/sequel/plugins/validation_helpers.rb, line 104
104:         def validates_min_length(min, atts, opts={})
105:           validatable_attributes_for_type(:min_length, atts, opts){|a,v,m| validation_error_message(m, min) unless v && v.length >= min}
106:         end
validates_not_string(atts, opts={}) click to toggle source

Check that the attribute value(s) is not a string. This is generally useful in conjunction with raise_on_typecast_failure = false, where you are passing in string values for non-string attributes (such as numbers and dates). If typecasting fails (invalid number or date), the value of the attribute will be a string in an invalid format, and if typecasting succeeds, the value will not be a string.

     # File lib/sequel/plugins/validation_helpers.rb, line 114
114:         def validates_not_string(atts, opts={})
115:           validatable_attributes_for_type(:not_string, atts, opts){|a,v,m| validation_error_message(m, (db_schema[a]||{})[:type]) if v.is_a?(String)}
116:         end
validates_numeric(atts, opts={}) click to toggle source

Check attribute value(s) string representation is a valid float.

     # File lib/sequel/plugins/validation_helpers.rb, line 119
119:         def validates_numeric(atts, opts={})
120:           validatable_attributes_for_type(:numeric, atts, opts) do |a,v,m|
121:             begin
122:               Kernel.Float(v.to_s)
123:               nil
124:             rescue
125:               validation_error_message(m)
126:             end
127:           end
128:         end
validates_presence(atts, opts={}) click to toggle source

Check attribute value(s) is not considered blank by the database, but allow false values.

     # File lib/sequel/plugins/validation_helpers.rb, line 137
137:         def validates_presence(atts, opts={})
138:           validatable_attributes_for_type(:presence, atts, opts){|a,v,m| validation_error_message(m) if model.db.send(:blank_object?, v) && v != false}
139:         end
validates_type(klass, atts, opts={}) click to toggle source

Check if value is an instance of a class

     # File lib/sequel/plugins/validation_helpers.rb, line 131
131:         def validates_type(klass, atts, opts={})
132:           klass = klass.to_s.constantize if klass.is_a?(String) || klass.is_a?(Symbol)
133:           validatable_attributes_for_type(:type, atts, opts){|a,v,m| validation_error_message(m, klass) if v && !v.is_a?(klass)}
134:         end
validates_unique(*atts) click to toggle source

Checks that there are no duplicate values in the database for the given attributes. Pass an array of fields instead of multiple fields to specify that the combination of fields must be unique, instead of that each field should have a unique value.

This means that the code:

  validates_unique([:column1, :column2])

validates the grouping of column1 and column2 while

  validates_unique(:column1, :column2)

validates them separately.

You can pass a block, which is yielded the dataset in which the columns must be unique. So if you are doing a soft delete of records, in which the name must be unique, but only for active records:

  validates_unique(:name){|ds| ds.filter(:active)}

You should also add a unique index in the database, as this suffers from a fairly obvious race condition.

This validation does not respect the :allow_* options that the other validations accept, since it can deal with a grouping of multiple attributes.

Possible Options:

  • :message - The message to use (default: ‘is already taken’)

  • :only_if_modified - Only check the uniqueness if the object is new or one of the columns has been modified.

     # File lib/sequel/plugins/validation_helpers.rb, line 168
168:         def validates_unique(*atts)
169:           opts = default_validation_helpers_options(:unique)
170:           if atts.last.is_a?(Hash)
171:             opts = opts.merge(atts.pop)
172:           end
173:           message = validation_error_message(opts[:message])
174:           atts.each do |a|
175:             arr = Array(a)
176:             next if opts[:only_if_modified] && !new? && !arr.any?{|x| changed_columns.include?(x)}
177:             ds = model.filter(arr.map{|x| [x, send(x)]})
178:             ds = yield(ds) if block_given?
179:             ds = ds.exclude(pk_hash) unless new?
180:             errors.add(a, message) unless ds.count == 0
181:           end
182:         end

Private Instance Methods

default_validation_helpers_options(type) click to toggle source

The default options hash for the given type of validation. Can be overridden on a per-model basis for different per model defaults. The hash return must include a :message option that is either a proc or string.

     # File lib/sequel/plugins/validation_helpers.rb, line 190
190:         def default_validation_helpers_options(type)
191:           DEFAULT_OPTIONS[type]
192:         end
validatable_attributes(atts, opts) click to toggle source

Skip validating any attribute that matches one of the allow_* options. Otherwise, yield the attribute, value, and passed option :message to the block. If the block returns anything except nil or false, add it as an error message for that attributes.

     # File lib/sequel/plugins/validation_helpers.rb, line 198
198:         def validatable_attributes(atts, opts)
199:           am, an, ab, m = opts.values_at(:allow_missing, :allow_nil, :allow_blank, :message)
200:           Array(atts).each do |a|
201:             next if am && !values.has_key?(a)
202:             v = send(a)
203:             next if an && v.nil?
204:             next if ab && v.respond_to?(:blank?) && v.blank?
205:             if message = yield(a, v, m)
206:               errors.add(a, message)
207:             end
208:           end
209:         end
validatable_attributes_for_type(type, atts, opts, &block) click to toggle source

Merge the given options with the default options for the given type and call validatable_attributes with the merged options.

     # File lib/sequel/plugins/validation_helpers.rb, line 213
213:         def validatable_attributes_for_type(type, atts, opts, &block)
214:           validatable_attributes(atts, default_validation_helpers_options(type).merge(opts), &block)
215:         end
validation_error_message(message, *args) click to toggle source

The validation error message to use, as a string. If message is a Proc, call it with the args. Otherwise, assume it is a string and return it.

     # File lib/sequel/plugins/validation_helpers.rb, line 220
220:         def validation_error_message(message, *args)
221:           message.is_a?(Proc) ? message.call(*args) : message
222:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.