Module: DataMapper::Validate::ValidatesLength

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

Overview

class LengthValidator

Instance Method Summary collapse

Instance Method Details

#validates_length(*fields) ⇒ Object

Validates that the length of the attribute is equal to, less than, greater than or within a certain range (depending upon the options you specify).

Examples:

Usage

require 'dm-validations'

class Page
  include DataMapper::Resource

  property high, Integer
  property low, Integer
  property just_right, Integer

  validates_length :high, :min => 100000000000
  validates_length :low, :equals => 0
  validates_length :just_right, :within => 1..10

  # a call to valid? will return false unless:
  # high is greater than or equal to 100000000000
  # low is equal to 0
  # just_right is between 1 and 10 (inclusive of both 1 and 10)

Parameters:

  • :allow_nil<Boolean> (Hash)

    a customizable set of options

  • :minimum (Hash)

    a customizable set of options

  • :min (Hash)

    a customizable set of options

  • :maximum (Hash)

    a customizable set of options

  • :max (Hash)

    a customizable set of options

  • :equals (Hash)

    a customizable set of options

  • :is (Hash)

    a customizable set of options

  • :in<Range> (Hash)

    a customizable set of options

  • :within<Range> (Hash)

    a customizable set of options


106
107
108
109
# File 'lib/dm-validations/length_validator.rb', line 106

def validates_length(*fields)
  opts = opts_from_validator_args(fields)
  add_validator_to_context(opts, fields, DataMapper::Validate::LengthValidator)
end