class DataMapper::Validations::FormatValidator
@author Guy van den Berg @since 0.9
Constants
- FORMATS
Public Class Methods
new(field_name, options = {})
click to toggle source
Calls superclass method
# File lib/dm-validations/validators/format_validator.rb, line 20 def initialize(field_name, options = {}) super set_optional_by_default end
Public Instance Methods
call(target)
click to toggle source
# File lib/dm-validations/validators/format_validator.rb, line 26 def call(target) return true if valid?(target) value = target.validation_property_value(field_name) error_message = ( @options[:message] || ValidationErrors.default_error_message( :invalid, field_name ) ) add_error( target, error_message.try_call(humanized_field_name, value), field_name ) false end
Private Instance Methods
valid?(target)
click to toggle source
# File lib/dm-validations/validators/format_validator.rb, line 47 def valid?(target) value = target.validation_property_value(field_name) return true if optional?(value) validation = @options[:as] || @options[:with] if validation.is_a?(Symbol) && !FORMATS.has_key?(validation) raise("No such predefined format '#{validation}'") end validator = if validation.is_a?(Symbol) FORMATS[validation][0] else validation end case validator when Proc then validator.call(value) when Regexp then value ? value.to_s =~ validator : false else raise(UnknownValidationFormat, "Can't determine how to validate #{target.class}##{field_name} with #{validator.inspect}") end rescue Encoding::CompatibilityError # This is to work around a bug in jruby - see formats/email.rb false end