Class: Lotus::Validations::Errors

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

Overview

A set of errors for a validator

This is the result of calling ‘#valid?` on a validator.

See Also:

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initializeErrors

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the errors

Since:

  • 0.1.0



17
18
19
# File 'lib/lotus/validations/errors.rb', line 17

def initialize
  @errors = Hash.new
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass Also known as: eql?

Check if the current set of errors equals to the one who belongs to ‘other`.

Parameters:

  • other (Object)

    the other term of comparison

Returns:

  • (TrueClass, FalseClass)

    the result of comparison

Since:

  • 0.1.0



120
121
122
123
# File 'lib/lotus/validations/errors.rb', line 120

def ==(other)
  other.is_a?(self.class) &&
    other.errors == errors
end

#add(attribute, *errors) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add an error to the set

Parameters:

  • attribute (Symbol)

    the name of the attribute

  • errors (Array)

    a collection of errors

See Also:

Since:

  • 0.1.0



96
97
98
99
100
101
# File 'lib/lotus/validations/errors.rb', line 96

def add(attribute, *errors)
  if errors.any?
    @errors[attribute] ||= []
    @errors[attribute].push(*errors)
  end
end

#any?TrueClass, FalseClass

Check if the set has any entry

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.2.0



39
40
41
# File 'lib/lotus/validations/errors.rb', line 39

def any?
  @errors.any?
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears the internal state of the errors

Since:

  • 0.1.0



58
59
60
# File 'lib/lotus/validations/errors.rb', line 58

def clear
  @errors.clear
end

#countFixnum Also known as: size

Returns how many validations have failed

Returns:

  • (Fixnum)

    the count

Since:

  • 0.1.0



48
49
50
# File 'lib/lotus/validations/errors.rb', line 48

def count
  errors.count
end

#each(&blk) {|error| ... } ⇒ Object

Iterate thru the errors and yields the given block

Parameters:

  • blk (Proc)

    the given block

Yields:

  • (error)

    a Lotus::Validations::Error

See Also:

Since:

  • 0.1.0



70
71
72
# File 'lib/lotus/validations/errors.rb', line 70

def each(&blk)
  errors.each(&blk)
end

#empty?TrueClass, FalseClass

Check if the set is empty

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.1.0



28
29
30
# File 'lib/lotus/validations/errors.rb', line 28

def empty?
  @errors.empty?
end

#for(attribute) ⇒ Object

Return the errors for the given attribute

Parameters:

  • attribute (Symbol)

    the name of the attribute

Since:

  • 0.1.0



108
109
110
# File 'lib/lotus/validations/errors.rb', line 108

def for(attribute)
  @errors.fetch(attribute) { [] }
end

#map(&blk) {|error| ... } ⇒ Object

Iterate thru the errors, yields the given block and collect the returning value.

Parameters:

  • blk (Proc)

    the given block

Yields:

  • (error)

    a Lotus::Validations::Error

See Also:

Since:

  • 0.1.0



83
84
85
# File 'lib/lotus/validations/errors.rb', line 83

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

#to_aArray

Return a flat collection of errors.

Returns:

  • (Array)

Since:

  • 0.2.1



141
142
143
# File 'lib/lotus/validations/errors.rb', line 141

def to_a
  errors.dup
end

#to_hLotus::Utils::Hash

Return a serializable Hash representation of the errors.

Returns:

  • (Lotus::Utils::Hash)

    the Hash

Since:

  • 0.2.1



132
133
134
# File 'lib/lotus/validations/errors.rb', line 132

def to_h
  Utils::Hash.new(@errors).deep_dup
end