Class: DataMapper::Validations::FormatValidator

Inherits:
GenericValidator show all
Includes:
DataMapper::Validations::Format::Email, DataMapper::Validations::Format::Url
Defined in:
lib/dm-validations/validators/format_validator.rb

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Constant Summary collapse

FORMATS =

Since:

  • 0.9

{}

Constants included from DataMapper::Validations::Format::Url

DataMapper::Validations::Format::Url::Url

Constants included from DataMapper::Validations::Format::Email

DataMapper::Validations::Format::Email::EmailAddress

Instance Attribute Summary

Attributes inherited from GenericValidator

#field_name, #humanized_field_name, #if_clause, #options, #unless_clause

Instance Method Summary collapse

Methods included from DataMapper::Validations::Format::Url

included

Methods included from DataMapper::Validations::Format::Email

included

Methods inherited from GenericValidator

#==, #add_error, #evaluate_conditional_clause, #execute?, #inspect, #optional?, #set_optional_by_default

Constructor Details

#initialize(field_name, options = {}) ⇒ FormatValidator

Returns a new instance of FormatValidator.

Since:

  • 0.9



20
21
22
23
24
# File 'lib/dm-validations/validators/format_validator.rb', line 20

def initialize(field_name, options = {})
  super

  set_optional_by_default
end

Instance Method Details

#call(target) ⇒ Object

Since:

  • 0.9



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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