Class: DataMapper::Validate::CustomValidator
- GenericValidator
- DataMapper::Validate::CustomValidator
Constructor Summary
public
initialize(field_name, options = {}, &b)
[View source]
12 13 14 15 16
# File 'dm-more/dm-validations/lib/dm-validations/custom_validator.rb', line 12 def initialize(field_name, options = {}, &b) #super(field_name, options) #@field_name, @options = field_name, options #@options[:allow_nil] = false unless @options.has_key?(:allow_nil) end
Public Visibility
Public Instance Method Summary
| #call(target) |
|---|
Public Instance Method Details
call
public
call(target)
[View source]
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
# File 'dm-more/dm-validations/lib/dm-validations/custom_validator.rb', line 18 def call(target) #field_value = target.instance_variable_get("@#{@field_name}") #return true if @options[:allow_nil] && field_value.nil? #validation = (@options[:as] || @options[:with]) #error_message = nil # Figure out what to use as the actual validator. # If a symbol is passed to :as, look up # the canned validation in FORMATS. # #validator = if validation.is_a? Symbol # if FORMATS[validation].is_a? Array # error_message = FORMATS[validation][1] # FORMATS[validation][0] # else # FORMATS[validation] || validation # end #else # validation #end #valid = case validator #when Proc then validator.call(field_value) #when Regexp then validator =~ field_value #else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}" #end #unless valid # field = Inflector.humanize(@field_name) # value = field_value # # error_message = @options[:message] || error_message || '%s is invalid'.t(field) # error_message = error_message.call(field, value) if Proc === error_message # # add_error(target, error_message , @field_name) #end #return valid end