Class: DataMapper::Validations::AcceptanceValidator

Inherits:
GenericValidator show all
Defined in:
lib/dm-validations/validators/acceptance_validator.rb

Overview

Author:

  • Martin Kihlgren

Since:

  • 0.9

Instance Attribute Summary

Attributes inherited from GenericValidator

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

Instance Method Summary collapse

Methods inherited from GenericValidator

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

Constructor Details

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

Returns a new instance of AcceptanceValidator.

Since:

  • 0.9



7
8
9
10
11
12
13
14
# File 'lib/dm-validations/validators/acceptance_validator.rb', line 7

def initialize(field_name, options = {})
  super

  @options[:allow_nil] = true unless @options.key?(:allow_nil)

  @options[:accept] ||= [ '1', 1, 'true', true, 't' ]
  @options[:accept] = Array(@options[:accept])
end

Instance Method Details

#call(target) ⇒ Object

Since:

  • 0.9



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dm-validations/validators/acceptance_validator.rb', line 16

def call(target)
  return true if valid?(target)

  error_message = (
    @options[:message] || ValidationErrors.default_error_message(
      :accepted, field_name
    )
  )
  add_error(target, error_message, field_name)

  false
end