Methods

Class Index [+]

Quicksearch

DataMapper::Validations::WithinValidator

@author Guy van den Berg @since 0.9

Public Class Methods

new(field_name, options={}) click to toggle source
    # File lib/dm-validations/validators/within_validator.rb, line 7
 7:       def initialize(field_name, options={})
 8:         super
 9: 
10:         @options[:set] = [] unless @options.has_key?(:set)
11:       end

Public Instance Methods

call(target) click to toggle source
    # File lib/dm-validations/validators/within_validator.rb, line 13
13:       def call(target)
14:         value = target.validation_property_value(field_name)
15:         return true if optional?(value)
16:         return true if @options[:set].include?(value)
17: 
18:         n = 1.0/0
19:         set = @options[:set]
20:         msg = @options[:message]
21: 
22:         if set.is_a?(Range)
23:           if set.first != -n && set.last != n
24:             error_message = msg || ValidationErrors.default_error_message(:value_between, field_name, set.first, set.last)
25:           elsif set.first == -n
26:             error_message = msg || ValidationErrors.default_error_message(:less_than_or_equal_to, field_name, set.last)
27:           elsif set.last == n
28:             error_message = msg || ValidationErrors.default_error_message(:greater_than_or_equal_to, field_name, set.first)
29:           end
30:         else
31:           error_message = msg || ValidationErrors.default_error_message(:inclusion, field_name, set.to_a.join(', '))
32:         end
33: 
34:         add_error(target, error_message, field_name)
35: 
36:         false
37:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.