Class: Toolchain::Validations::ValidationErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/toolchain/validations/validation_errors.rb

Constant Summary collapse

Helpers =
Toolchain::Validations::Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidationErrors

Creates a new instance of Toolchain::Validations::ValidationErrors and sets the initial errors state to an empty Hash.



12
13
14
# File 'lib/toolchain/validations/validation_errors.rb', line 12

def initialize
  @errors = {}
end

Instance Attribute Details

#errorsHash (readonly)

Returns:

  • (Hash)


7
8
9
# File 'lib/toolchain/validations/validation_errors.rb', line 7

def errors
  @errors
end

Instance Method Details

#==(value) ⇒ Boolean

Proxy method to delegate the comparison of two values to the errors Hash.

Parameters:

  • value (Object)

Returns:

  • (Boolean)

    true if both values are equal.



64
65
66
# File 'lib/toolchain/validations/validation_errors.rb', line 64

def ==(value)
  errors == value
end

#[](key) ⇒ Object

Proxy method to allow individual values of the errors Hash to be accessed directly through the Toolchain::Validations::ValidationErrors object.

Parameters:

  • key (Symbol)

    The key of the value you want to retrieve.

Returns:

  • (Object)

    the value of the key you want to retrieve.



53
54
55
# File 'lib/toolchain/validations/validation_errors.rb', line 53

def [](key)
  errors[key]
end

#add(*args) ⇒ Object

Adds a new message to the Toolchain::Validations::ValidationErrors object.

Examples:

errors.add(:name, "is required")
errors.add(:config, :url, "isn't valid")

Parameters:

  • args (Array<Symbol, String>)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toolchain/validations/validation_errors.rb', line 24

def add(*args)
  message, key_path = args.pop, args
  key_path = [key_path].flatten

  Helpers.inject(key_path, errors) do |memo, key, last|
    if last
      memo[key] ||= []
      memo[key].push(message)
    else
      memo[key] ||= {}
    end

    memo[key]
  end
end

#empty?Boolean

Proxy method to check if there currently are any errors.

Returns:

  • (Boolean)

    true if there aren’t any errors.



72
73
74
# File 'lib/toolchain/validations/validation_errors.rb', line 72

def empty?
  errors.empty?
end

#resetObject

Resets all errors to an empty Hash.



42
43
44
# File 'lib/toolchain/validations/validation_errors.rb', line 42

def reset
  @errors = {}
end

#to_hashHash

Returns the Hash representation of the Toolchain::Validations::ValidationErrors.

Returns:

  • (Hash)

    the Hash representation of the Toolchain::Validations::ValidationErrors.



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

def to_hash
  errors
end