Class: DataMapper::Validate::FormatValidator

  • GenericValidator
    • DataMapper::Validate::FormatValidator

Constants

FORMATS
{}

Constructor Summary

public initialize(field_name, options = {}, &b)
[View source]


18
19
20
21
22
# File 'dm-more/dm-validations/lib/dm-validations/format_validator.rb', line 18

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 Class Methods Included from DataMapper::Validate::Format::Email

included

Public Instance Method Summary

#call(target)

Public Instance Method Details

call

public call(target)
[View source]


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/format_validator.rb', line 24

def call(target)
  value = target.validation_property_value(@field_name)
  return true if @options[:allow_nil] && value.nil?

  validation = (@options[:as] || @options[:with])

  raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
  validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation

  field = Extlib::Inflection.humanize(@field_name)
  error_message = @options[:message] || '%s has an invalid format'.t(field)

  valid = case validator




  when Proc   then validator.call(value)
  when Regexp then validator =~ value
  else raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{@field_name} with #{validator.inspect}"
  end

  unless valid




    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