Module: Validatable

Defined in:
lib/validatable.rb,
lib/validatable/errors.rb,
lib/validatable/macros.rb,
lib/validatable_flymake.rb,
lib/validatable/requireable.rb,
lib/validatable/understandable.rb,
lib/validatable/child_validation.rb,
lib/validatable/included_validation.rb,
lib/validatable/validatable_class_methods.rb,
lib/validatable/validations/validates_each.rb,
lib/validatable/validations/validation_base.rb,
lib/validatable/validatable_instance_methods.rb,
lib/validatable/validations/validates_true_for.rb,
lib/validatable/validations/validates_format_of.rb,
lib/validatable/validations/validates_length_of.rb,
lib/validatable/validations/validates_associated.rb,
lib/validatable/validations/validates_presence_of.rb,
lib/validatable/validations/validates_exclusion_of.rb,
lib/validatable/validations/validates_inclusion_of.rb,
lib/validatable/validations/validates_acceptance_of.rb,
lib/validatable/validations/validates_confirmation_of.rb,
lib/validatable/validations/validates_numericality_of.rb

Defined Under Namespace

Modules: ClassMethods, Macros, Requireable, Understandable Classes: ChildValidation, Errors, IncludedValidation, ValidatesAcceptanceOf, ValidatesAssociated, ValidatesConfirmationOf, ValidatesEach, ValidatesExclusionOf, ValidatesFormatOf, ValidatesInclusionOf, ValidatesLengthOf, ValidatesNumericalityOf, ValidatesPresenceOf, ValidatesTrueFor, ValidationBase

Constant Summary collapse

Version =
'1.8.5'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



2
3
4
5
# File 'lib/validatable/validatable_instance_methods.rb', line 2

def self.included(klass) #:nodoc:
  klass.extend Validatable::ClassMethods
  klass.extend Validatable::Macros
end

Instance Method Details

#errorsObject

call-seq: errors

Returns the Errors object that holds all information about attribute error messages.



17
18
19
# File 'lib/validatable/validatable_instance_methods.rb', line 17

def errors
  @errors ||= Validatable::Errors.new
end

#increment_times_validated_for(validation) ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
# File 'lib/validatable/validatable_instance_methods.rb', line 33

def increment_times_validated_for(validation) #:nodoc:
  if validation.key != nil
    if times_validated_hash[validation.key].nil?
      times_validated_hash[validation.key] = 1
    else
      times_validated_hash[validation.key] += 1
    end
  end
end

#times_validated(key) ⇒ Object

:nodoc:



29
30
31
# File 'lib/validatable/validatable_instance_methods.rb', line 29

def times_validated(key) #:nodoc:
  times_validated_hash[key] || 0
end

#valid?Boolean

call-seq: valid?

Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified

Returns:

  • (Boolean)


10
11
12
# File 'lib/validatable/validatable_instance_methods.rb', line 10

def valid?
  valid_for_group?(nil)
end

#valid_for_group?(group) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/validatable/validatable_instance_methods.rb', line 21

def valid_for_group?(group) #:nodoc:
  errors.clear
  run_before_validations
  self.class.validate_children(self, group)
  self.validate_group(group)
  errors.empty?
end

#validate_only(key) ⇒ Object

call-seq: validate_only(key)

Only executes a specified validation. The argument should follow a pattern based on the key of the validation.

Examples:
  * validates_presence_of :name can be run with obj.validate_only("presence_of/name")
  * validates_presence_of :birthday, :key => "a key" can be run with obj.validate_only("presence_of/a key")

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
# File 'lib/validatable/validatable_instance_methods.rb', line 49

def validate_only(key)
  validation_name, attribute_name = key.split("/")
  validation_name = validation_name.split("_").collect{|word| word.capitalize}.join
  validation_key = "#{self.class.name}/Validatable::Validates#{validation_name}/#{attribute_name}"
  validation = self.class.all_validations.find { |validation| validation.key == validation_key }
  raise ArgumentError.new("validation with key #{validation_key} could not be found") if validation.nil?
  errors.clear
  run_validation(validation)
end