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_inclusion_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, ValidatesInclusionOf, 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

#[](key) ⇒ Object



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

def [](key)
  instance_variable_get("@#{key}")
end

#[]=(key, value) ⇒ Object



21
22
23
# File 'lib/validatable_instance_methods.rb', line 21

def []=(key, value)
  instance_variable_set("@#{key}", value)
end

#errorsObject

call-seq: errors

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



35
36
37
# File 'lib/validatable_instance_methods.rb', line 35

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

#increment_times_validated_for(validation) ⇒ Object

:nodoc:



51
52
53
54
55
56
57
58
59
# File 'lib/validatable_instance_methods.rb', line 51

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

#initialize(attributes = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Validatable)

    the object that the method was called on



7
8
9
10
11
12
13
14
15
# File 'lib/validatable_instance_methods.rb', line 7

def initialize(attributes = nil)
  # Mass Assignment implementation
  if attributes
    attributes.each do |key, value|
      self[key] = value
    end
  end
  yield self if block_given?
end

#times_validated(key) ⇒ Object

:nodoc:



47
48
49
# File 'lib/validatable_instance_methods.rb', line 47

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)


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

def valid?
  valid_for_group?(nil)
end

#valid_for_group?(group) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/validatable_instance_methods.rb', line 39

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)


67
68
69
70
71
72
73
74
75
# File 'lib/validatable_instance_methods.rb', line 67

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