Module: DataMapper::Validations

Includes:
Context
Defined in:
lib/dm-validations.rb,
lib/dm-validations/context.rb,
lib/dm-validations/version.rb,
lib/dm-validations/formats/url.rb,
lib/dm-validations/auto_validate.rb,
lib/dm-validations/formats/email.rb,
lib/dm-validations/validation_errors.rb,
lib/dm-validations/support/ordered_hash.rb,
lib/dm-validations/contextual_validators.rb,
lib/dm-validations/validators/block_validator.rb,
lib/dm-validations/validators/format_validator.rb,
lib/dm-validations/validators/length_validator.rb,
lib/dm-validations/validators/method_validator.rb,
lib/dm-validations/validators/within_validator.rb,
lib/dm-validations/validators/generic_validator.rb,
lib/dm-validations/validators/numeric_validator.rb,
lib/dm-validations/validators/primitive_validator.rb,
lib/dm-validations/validators/acceptance_validator.rb,
lib/dm-validations/validators/uniqueness_validator.rb,
lib/dm-validations/validators/absent_field_validator.rb,
lib/dm-validations/validators/confirmation_validator.rb,
lib/dm-validations/validators/required_field_validator.rb

Defined Under Namespace

Modules: AutoValidations, ClassMethods, Context, Format, ValidatesAbsence, ValidatesAcceptance, ValidatesConfirmation, ValidatesFormat, ValidatesLength, ValidatesNumericality, ValidatesPresence, ValidatesPrimitiveType, ValidatesUniqueness, ValidatesWithBlock, ValidatesWithMethod, ValidatesWithin Classes: AbsenceValidator, AcceptanceValidator, ConfirmationValidator, ContextualValidators, FormatValidator, GenericValidator, LengthValidator, MethodValidator, NumericalityValidator, OrderedHash, PresenceValidator, PrimitiveTypeValidator, UniquenessValidator, UnknownValidationFormat, ValidationErrors, WithinValidator

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context

any?, current, #default_validation_context, in_context, stack

Class Method Details

.included(model) ⇒ Object



30
31
32
# File 'lib/dm-validations.rb', line 30

def self.included(model)
  model.extend ClassMethods
end

Instance Method Details

#errorsObject

Return the ValidationErrors



61
62
63
# File 'lib/dm-validations.rb', line 61

def errors
  @errors ||= ValidationErrors.new(self)
end

#save(context = default_validation_context) ⇒ Object

Ensures the object is valid for the context provided, and otherwise throws :halt and returns false.



38
39
40
41
# File 'lib/dm-validations.rb', line 38

def save(context = default_validation_context)
  model.validators.assert_valid(context)
  Validations::Context.in_context(context) { super() }
end

#save_selfObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
# File 'lib/dm-validations.rb', line 50

def save_self(*)
  if Validations::Context.any? && !valid?(model.validators.current_context)
    false
  else
    super
  end
end

#update(attributes = {}, context = default_validation_context) ⇒ Object



44
45
46
47
# File 'lib/dm-validations.rb', line 44

def update(attributes = {}, context = default_validation_context)
  model.validators.assert_valid(context)
  Validations::Context.in_context(context) { super(attributes) }
end

#valid?(context = :default) ⇒ Boolean

Check if a resource is valid in a given context

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/dm-validations.rb', line 84

def valid?(context = :default)
  model = respond_to?(:model) ? self.model : self.class
  model.validators.execute(context, self)
end

#valid_for_default?Boolean

Alias for valid?(:default)

TODO: deprecate

Returns:

  • (Boolean)


77
78
79
# File 'lib/dm-validations.rb', line 77

def valid_for_default?
  valid?(:default)
end

#validatable?Boolean

Mark this resource as validatable. When we validate associations of a resource we can check if they respond to validatable? before trying to recursively validate them

Returns:

  • (Boolean)


70
71
72
# File 'lib/dm-validations.rb', line 70

def validatable?
  true
end

#validation_property_value(name) ⇒ Object



90
91
92
# File 'lib/dm-validations.rb', line 90

def validation_property_value(name)
  __send__(name) if respond_to?(name, true)
end