class Toolchain::Validations::ValidationErrors
Constants
- Helpers
Attributes
@return [Hash]
Public Class Methods
Creates a new instance of Toolchain::Validations::ValidationErrors
and sets the initial errors state to an empty Hash.
# File lib/toolchain/validations/validation_errors.rb, line 12 def initialize @errors = {} end
Public Instance Methods
Proxy method to delegate the comparison of two values to the errors Hash.
@param value [Object]
@return [Boolean] true if both values are equal.
# File lib/toolchain/validations/validation_errors.rb, line 64 def ==(value) errors == value end
Proxy method to allow individual values of the errors Hash to be accessed directly through the Toolchain::Validations::ValidationErrors
object.
@param key [Symbol] The key of the value you want to retrieve.
@return [Object] the value of the key you want to retrieve.
# File lib/toolchain/validations/validation_errors.rb, line 53 def [](key) errors[key] end
Adds a new message to the Toolchain::Validations::ValidationErrors
object.
@param args [Array<Symbol, String>]
@example
errors.add(:name, "is required") errors.add(:config, :url, "isn't valid")
# 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
Proxy method to check if there currently are any errors.
@return [Boolean] true if there aren't any errors.
# File lib/toolchain/validations/validation_errors.rb, line 72 def empty? errors.empty? end
Resets all errors to an empty Hash.
# File lib/toolchain/validations/validation_errors.rb, line 42 def reset @errors = {} end
@return [Hash] the Hash representation of the
Toolchain::Validations::ValidationErrors.
# File lib/toolchain/validations/validation_errors.rb, line 79 def to_hash errors end