Class: Aux::Validations::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/aux/validations/errors.rb

Overview

Describes a collection of errors that can be initialized with an attribute name or an object to be validated

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ Errors #initialize(attribute, errors) ⇒ Errors

Returns a new instance of Errors.

Overloads:

  • #initialize(subject) ⇒ Errors

    Parameters:

    • subject (Object)
  • #initialize(attribute, errors) ⇒ Errors

    Parameters:

    • attribute (Symbol)
    • errors (Errors)


18
19
20
21
22
# File 'lib/aux/validations/errors.rb', line 18

def initialize(subject, errors = [])
  @scope = subject.is_a?(Symbol) ? nil : subject.class.name.underscore.tr('/', '.')
  @attribute = subject.is_a?(Symbol) ? subject : nil
  @errors = errors
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



11
# File 'lib/aux/validations/errors.rb', line 11

attr_reader :scope, :attribute

#scopeString? (readonly)

Returns:

  • (String, nil)


11
12
13
# File 'lib/aux/validations/errors.rb', line 11

def scope
  @scope
end

Instance Method Details

#add(attribute, type, **details) ⇒ Object #add(attribute, errors) ⇒ Object #add(attribute, errors) ⇒ Object

Overloads:

  • #add(attribute, type, **details) ⇒ Object

    Parameters:

    • attribute (Symbol)
    • type (Symbol)
    • details (Hash)

    Raises:

    • (StandardError)
  • #add(attribute, errors) ⇒ Object

    Parameters:

    Raises:

    • (StandardError)
  • #add(attribute, errors) ⇒ Object

    Parameters:

    • attribute (Symbol)
    • errors (Errors)

    Raises:

    • (StandardError)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aux/validations/errors.rb', line 37

def add(attribute, subject, **details)
  case subject
  when Symbol
    add_error(attribute, subject, **details)
  when Array
    add_nested_error(attribute, subject)
  when Errors
    add_nested_error(attribute, [subject])
  else
    raise(ArgumentError, "Unsupported argument given (#{subject.class})")
  end
end

#clearObject



50
51
52
# File 'lib/aux/validations/errors.rb', line 50

def clear
  self.errors = []
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/aux/validations/errors.rb', line 55

def empty?
  errors.empty?
end

#group_by_attributeHash<Symbol, Array>

Returns:

  • (Hash<Symbol, Array>)


65
66
67
# File 'lib/aux/validations/errors.rb', line 65

def group_by_attribute
  errors.group_by(&:attribute)
end

#map(&block) ⇒ Object

Parameters:

  • block (Proc)


60
61
62
# File 'lib/aux/validations/errors.rb', line 60

def map(&block)
  errors.map(&block)
end