Class: Domainic::Command::Result::ErrorSet

Inherits:
Object
  • Object
show all
Defined in:
lib/domainic/command/result/error_set.rb

Overview

A flexible container for managing and formatting command errors. The ErrorSet provides a consistent interface for working with errors from various sources including simple strings, arrays, hashes, standard errors, and objects implementing a compatible to_h interface (like ActiveModel::Errors).

Examples:

Basic usage

errors = ErrorSet.new("Something went wrong")
errors[:generic] #=> ["Something went wrong"]
errors.full_messages #=> ["generic Something went wrong"]

Hash-style errors

errors = ErrorSet.new(
  name: "can't be blank",
  email: ["invalid format", "already taken"]
)
errors[:name] #=> ["can't be blank"]
errors[:email] #=> ["invalid format", "already taken"]

ActiveModel compatibility

user = User.new
user.valid? #=> false
errors = ErrorSet.new(user.errors)
errors[:email] #=> ["can't be blank"]

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(errors = nil) ⇒ ErrorSet

Creates a new ErrorSet instance

Parameters:

  • errors (String, Array, Hash, StandardError, #to_h, nil) (defaults to: nil)

    The errors to parse

Raises:

  • (ArgumentError)

    If the errors cannot be parsed

Since:

  • 0.1.0



41
42
43
# File 'lib/domainic/command/result/error_set.rb', line 41

def initialize(errors = nil)
  @lookup = Parser.new(errors).parse!
end

Instance Method Details

#[](key) ⇒ Array<String>?

Retrieves error messages for a specific key

Parameters:

  • key (String, Symbol)

    The error key to lookup

Returns:

  • (Array<String>, nil)

    The error messages for the key

Since:

  • 0.1.0



50
51
52
# File 'lib/domainic/command/result/error_set.rb', line 50

def [](key)
  @lookup[key.to_sym]
end

#add(key, message) ⇒ void

This method returns an undefined value.

Adds a new error message for a specific key

Parameters:

  • key (String, Symbol)

    The error key

  • message (String, Array<String>)

    The error message(s)

Since:

  • 0.1.0



61
62
63
64
65
# File 'lib/domainic/command/result/error_set.rb', line 61

def add(key, message)
  key = key.to_sym
  @lookup[key] ||= []
  @lookup[key].concat(Array(message)) # steep:ignore ArgumentTypeMismatch
end

#clearvoid

This method returns an undefined value.

Clear all errors from the set

Since:

  • 0.1.0



71
72
73
# File 'lib/domainic/command/result/error_set.rb', line 71

def clear
  @lookup = {}
end

#empty?Boolean

Check if the error set is empty

Returns:

  • (Boolean)

    true if the error set is empty, false otherwise

Since:

  • 0.1.0



79
80
81
# File 'lib/domainic/command/result/error_set.rb', line 79

def empty?
  @lookup.empty?
end

#full_messagesArray<String> Also known as: to_a, to_array, to_ary

Returns all error messages with their keys

Returns:

  • (Array<String>)

    All error messages prefixed with their keys

Since:

  • 0.1.0



87
88
89
90
91
# File 'lib/domainic/command/result/error_set.rb', line 87

def full_messages
  @lookup.each_with_object([]) do |(key, messages), result|
    result.concat(messages.map { |message| "#{key} #{message}" })
  end
end

#messagesHash{Symbol => Array<String>} Also known as: to_h, to_hash

Returns a hash of all error messages

Returns:

  • (Hash{Symbol => Array<String>})

    All error messages grouped by key

Since:

  • 0.1.0



100
101
102
# File 'lib/domainic/command/result/error_set.rb', line 100

def messages
  @lookup.dup.freeze
end