Class: Lotus::Validations::Error

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

Overview

A single validation error for an attribute

Since:

  • 0.1.0

Constant Summary collapse

NAMESPACE_SEPARATOR =

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

Since:

  • 0.2.4

'.'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, validation, expected, actual, namespace = nil) ⇒ Error

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 a validation error

Parameters:

  • attribute_name (Symbol)

    the name of the attribute

  • validation (Symbol)

    the name of the validation

  • expected (Object)

    the expected value

  • actual (Object)

    the actual value

  • namespace (String) (defaults to: nil)

    the optional namespace

Since:

  • 0.1.0



74
75
76
77
78
79
80
81
# File 'lib/lotus/validations/error.rb', line 74

def initialize(attribute_name, validation, expected, actual, namespace = nil)
  @attribute_name = attribute_name.to_s
  @validation = validation
  @expected = expected
  @actual = actual
  @namespace = namespace
  @attribute = [@namespace, attribute_name].compact.join(NAMESPACE_SEPARATOR)
end

Instance Attribute Details

#actualObject (readonly)

The actual value

Returns:

  • (Object)

    the actual value

Since:

  • 0.1.0



45
46
47
# File 'lib/lotus/validations/error.rb', line 45

def actual
  @actual
end

#attributeObject

Returns the namespaced attribute name

In cases where the error was pulled up from nested validators, ‘attribute` will be a namespaced string containing parent attribute names separated by a period.

Examples:

error = Error.new(:name, :presence, true, nil, 'author')

error.attribute      # => "author.name"
error.attribute_name # => "name"

See Also:

Since:

  • 0.1.0



62
63
64
# File 'lib/lotus/validations/error.rb', line 62

def attribute
  @attribute
end

#attribute_nameSymbol (readonly)

The name of the attribute

Examples:

error = Error.new(:name, :presence, true, nil, 'author')

error.attribute      # => "author.name"
error.attribute_name # => "name"

Returns:

  • (Symbol)

    the name of the attribute

See Also:

Since:

  • 0.2.4



24
25
26
# File 'lib/lotus/validations/error.rb', line 24

def attribute_name
  @attribute_name
end

#expectedObject (readonly)

The expected value

Returns:

  • (Object)

    the expected value

Since:

  • 0.1.0



38
39
40
# File 'lib/lotus/validations/error.rb', line 38

def expected
  @expected
end

#validationSymbol (readonly)

The name of the validation

Returns:

  • (Symbol)

    the name of the validation

Since:

  • 0.1.0



31
32
33
# File 'lib/lotus/validations/error.rb', line 31

def validation
  @validation
end

Instance Method Details

#==(other) ⇒ Object

Check if self equals to ‘other`

Since:

  • 0.1.0



86
87
88
89
90
91
92
# File 'lib/lotus/validations/error.rb', line 86

def ==(other)
  other.is_a?(self.class) &&
    other.attribute  == attribute  &&
    other.validation == validation &&
    other.expected   == expected   &&
    other.actual     == actual
end