Module: DataMapper::Validations::ValidatesWithin

Included in:
ClassMethods
Defined in:
lib/dm-validations/validators/within_validator.rb

Overview

class WithinValidator

Instance Method Summary collapse

Instance Method Details

#validates_within(*fields) ⇒ Object

Validates that the value of a field is within a range/set.

This validation is defined by passing a field along with a :set parameter. The :set can be a Range or any object which responds to the #include? method (an array, for example).

Examples:

Usage

require 'dm-validations'

class Review
  include DataMapper::Resource

  STATES = ['new', 'in_progress', 'published', 'archived']

  property :title, String
  property :body, String
  property :review_state, String
  property :rating, Integer

  validates_within :review_state, :set => STATES
  validates_within :rating,       :set => 1..5

  # a call to valid? will return false unless
  # the two properties conform to their sets
end


69
70
71
# File 'lib/dm-validations/validators/within_validator.rb', line 69

def validates_within(*fields)
  validators.add(WithinValidator, *fields)
end