Class: DataMapper::Validations::LengthValidator

### Home Classes Methods

### In Files

- lib/dm-validations/validators/length_validator.rb

### Parent

DataMapper::Validations::GenericValidator

### Methods

- ::new
- #call

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::InvalidContextError
- DataMapper::Validate
- DataMapper::ValidationError
- DataMapper::Validations
- DataMapper::Validations::AbsenceValidator
- DataMapper::Validations::AcceptanceValidator
- DataMapper::Validations::AutoValidations
- DataMapper::Validations::AutoValidations::ModelExtension
- DataMapper::Validations::ClassMethods
- DataMapper::Validations::ConfirmationValidator
- DataMapper::Validations::Context
- DataMapper::Validations::ContextualValidators
- DataMapper::Validations::Format
- DataMapper::Validations::Format::Email
- DataMapper::Validations::Format::Url
- DataMapper::Validations::FormatValidator
- DataMapper::Validations::GenericValidator
- DataMapper::Validations::LengthValidator
- DataMapper::Validations::MethodValidator
- DataMapper::Validations::NumericalityValidator
- DataMapper::Validations::OrderedHash
- DataMapper::Validations::PresenceValidator
- DataMapper::Validations::PrimitiveTypeValidator
- DataMapper::Validations::UniquenessValidator
- DataMapper::Validations::UnknownValidationFormat
- DataMapper::Validations::ValidatesAbsence
- DataMapper::Validations::ValidatesAcceptance
- DataMapper::Validations::ValidatesConfirmation
- DataMapper::Validations::ValidatesFormat
- DataMapper::Validations::ValidatesLength
- DataMapper::Validations::ValidatesNumericality
- DataMapper::Validations::ValidatesPresence
- DataMapper::Validations::ValidatesPrimitiveType
- DataMapper::Validations::ValidatesUniqueness
- DataMapper::Validations::ValidatesWithBlock
- DataMapper::Validations::ValidatesWithMethod
- DataMapper::Validations::ValidatesWithin
- DataMapper::Validations::ValidationErrors
- DataMapper::Validations::WithinValidator
- Object

No matching classes.

# DataMapper::Validations::LengthValidator

### Public Class Methods

new(field_name, options) click to toggle source

Initialize a length validator

@param [Symbol] field_name

    the name of the field to validate

@param [Hash] options

    the validator options

@api semipublic

    # File lib/dm-validations/validators/length_validator.rb, line 14
    def initialize(field_name, options)
      super

      @equal = options[:is]      || options[:equals]
      @range = options[:within]  || options[:in]
      @min   = options[:minimum] || options[:min]
      @max   = options[:maximum] || options[:max]

      if @min && @max
        @range ||= @min..@max
      end
    end

### Public Instance Methods

call(target) click to toggle source

Test the resource field for validity

@example when the resource field is valid

    validator.call(valid_resource)  # => true

@example when the resource field is not valid

    validator.call(invalid_resource)  # => false

@param [Resource] target

    the Resource to test

@return [Boolean]

    true if the field is valid, false if not

@api semipublic

    # File lib/dm-validations/validators/length_validator.rb, line 43
    def call(target)
      value = target.validation_property_value(field_name)
      return true if optional?(value)

      return true unless error_message = error_message_for(value)

      add_error(target, error_message, field_name)
      false
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.