Module: Toolchain::Validations::InstanceMethods

Defined in:
lib/toolchain/validations.rb

Instance Method Summary collapse

Instance Method Details

#errorsToolchain::Validations::ValidationErrors



78
79
80
# File 'lib/toolchain/validations.rb', line 78

def errors
  @errors ||= ValidationErrors.new
end

#valid?Boolean

Note:

If invalid (returns ‘false`), errors will have been added to the `object.errors` class.

Resets the ValidationErrors object, re-validates the current object and then determines whether or not this object is valid.

Returns:



113
114
115
116
117
# File 'lib/toolchain/validations.rb', line 113

def valid?
  errors.reset
  validate
  errors.empty?
end

#validateObject

Runs all the validation operations and will apply error messages to the ValidationErrors object when validator errors occur.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/toolchain/validations.rb', line 85

def validate
  Helpers.each_validation(self.class) do |v|
    validate = true

    if v[:if].kind_of?(Proc) && v[:if].call(self) == false
      validate = false
    end

    if v[:unless].kind_of?(Proc) && v[:unless].call(self) == true
      validate = false
    end

    if validate
      v[:validator].new(self, v[:key_path], v[:data]).validate
    end
  end

  Helpers.each_validation(self.class, :custom_validations) { |v| send(v) }
end