Module: Mixture::Extensions::Validatable::InstanceMethods

Defined in:
lib/mixture/extensions/validatable.rb

Overview

The instance methods.

Instance Method Summary collapse

Instance Method Details

#errorsHash{Attribute => Array<ValidationError>}

Returns a hash, mapping attributes to the errors that they have.

Returns:


52
53
54
# File 'lib/mixture/extensions/validatable.rb', line 52

def errors
  @errors ||= Hash.new { |h, k| h[k] = [] }
end

#invalid?Boolean

Opposite of valid.

Returns:

  • (Boolean)

See Also:


44
45
46
# File 'lib/mixture/extensions/validatable.rb', line 44

def invalid?
  !valid?
end

#valid?Boolean

Validates the attributes on the record. This will fill up #errors with errors, if there are any.

Returns:

  • (Boolean)

31
32
33
34
35
36
37
38
# File 'lib/mixture/extensions/validatable.rb', line 31

def valid?
  @errors = Hash.new { |h, k| h[k] = [] }
  self.class.attributes.each do |name, attribute|
    next unless attribute.options[:validate]
    Validate.validate(self, attribute, attribute(name))
  end
  !@errors.values.any?(&:any?)
end