Module: Validatable

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

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



2
3
4
5
# File 'lib/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_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_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_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_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_instance_methods.rb', line 21

def valid_for_group?(group) #:nodoc:
  run_before_validations
  errors.clear
  self.class.validate_children(self, group)
  self.validate(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_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