Class: ElectionBuddy::ErrorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/election_buddy/error_formatter.rb

Overview

Formats error messages from hash responses

Examples:

ErrorFormatter.format({ "email" => ["is invalid", "is required"] })
#=> "Email: is invalid, is required"

Class Method Summary collapse

Class Method Details

.format(error_hash) ⇒ String?

Formats error messages from a hash into a human-readable string

Parameters:

  • error_hash (Hash, nil)

    Hash containing error keys and messages

Returns:

  • (String, nil)

    Formatted error message or nil if no errors



15
16
17
18
19
20
21
22
23
# File 'lib/election_buddy/error_formatter.rb', line 15

def self.format(error_hash)
  return if error_hash.nil? || error_hash.empty?

  error_hash.map do |key, value|
    formatted_key = key.gsub("_", " ").split.map(&:capitalize).join(" ")
    formatted_value = value.is_a?(Array) ? value.join(", ") : value
    "#{formatted_key}: #{formatted_value}"
  end.join(", ")
end