Class: DataMapper::Validate::WithinValidator

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

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Instance Attribute Summary

Attributes inherited from GenericValidator

#if_clause, #unless_clause

Instance Method Summary collapse

Methods inherited from GenericValidator

#add_error, #execute?, #field_name

Constructor Details

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

Returns a new instance of WithinValidator.

Since:

  • 0.9



10
11
12
13
14
# File 'lib/dm-validations/within_validator.rb', line 10

def initialize(field_name, options={})
  super
  @field_name, @options = field_name, options
  @options[:set] = [] unless @options.has_key?(:set)
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/within_validator.rb', line 16

def call(target)
  includes = @options[:set].include?(target.send(field_name))
  return true if includes

  s = ''
  @options[:set].each {|item| s = s + "#{item}, "}
  s = '[' + s[0..(s.length-3)] + ']'

  error_message = @options[:message] || "%s must be one of #{s}".t(Extlib::Inflection.humanize(@field_name))
  add_error(target, error_message , @field_name)
  return false
end