Class: CMDx::Errors

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cmdx/errors.rb

Overview

Collection of validation and execution errors organized by attribute. Provides methods to add, query, and format error messages for different attributes in a task or workflow execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

Initialize a new error collection.



26
27
28
# File 'lib/cmdx/errors.rb', line 26

def initialize
  @messages = {}
end

Instance Attribute Details

#messagesHash{Symbol => Set<String>} (readonly)

Returns the internal hash of error messages by attribute.

Examples:

errors.messages # => { email: #<Set: ["must be valid", "is required"]> }

Returns:

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

    Hash mapping attribute names to error message sets



19
20
21
# File 'lib/cmdx/errors.rb', line 19

def messages
  @messages
end

Instance Method Details

#add(attribute, message) ⇒ Object

Add an error message for a specific attribute.

Examples:

errors = CMDx::Errors.new
errors.add(:email, "must be valid format")
errors.add(:email, "cannot be blank")

Parameters:

  • attribute (Symbol)

    The attribute name associated with the error

  • message (String)

    The error message to add



41
42
43
44
45
46
# File 'lib/cmdx/errors.rb', line 41

def add(attribute, message)
  return if message.empty?

  messages[attribute] ||= Set.new
  messages[attribute] << message
end

#for?(attribute) ⇒ Boolean

Check if there are any errors for a specific attribute.

Examples:

errors.for?(:email) # => true
errors.for?(:name)  # => false

Parameters:

  • attribute (Symbol)

    The attribute name to check for errors

Returns:

  • (Boolean)

    true if the attribute has errors, false otherwise



59
60
61
62
63
# File 'lib/cmdx/errors.rb', line 59

def for?(attribute)
  return false unless messages.key?(attribute)

  !messages[attribute].empty?
end

#full_messagesHash{Symbol => Array<String>}

Convert errors to a hash format with arrays of full messages.

Examples:

errors.full_messages # => { email: ["email must be valid format", "email cannot be blank"] }

Returns:

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

    Hash with attribute keys and message arrays



73
74
75
76
77
# File 'lib/cmdx/errors.rb', line 73

def full_messages
  messages.each_with_object({}) do |(attribute, messages), hash|
    hash[attribute] = messages.map { |message| "#{attribute} #{message}" }
  end
end

#to_hHash{Symbol => Array<String>}

Convert errors to a hash format with arrays of messages.

Examples:

errors.to_h # => { email: ["must be valid format", "cannot be blank"] }

Returns:

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

    Hash with attribute keys and message arrays



87
88
89
# File 'lib/cmdx/errors.rb', line 87

def to_h
  messages.transform_values(&:to_a)
end

#to_hash(full = false) ⇒ Hash{Symbol => Array<String>}

Convert errors to a hash format with optional full messages.

Examples:

errors.to_hash # => { email: ["must be valid format", "cannot be blank"] }
errors.to_hash(true) # => { email: ["email must be valid format", "email cannot be blank"] }

Parameters:

  • full (Boolean) (defaults to: false)

    Whether to include full messages with attribute names

Returns:

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

    Hash with attribute keys and message arrays



101
102
103
# File 'lib/cmdx/errors.rb', line 101

def to_hash(full = false)
  full ? full_messages : to_h
end

#to_sString

Convert errors to a human-readable string format.

Examples:

errors.to_s # => "email must be valid format. email cannot be blank"

Returns:

  • (String)

    Formatted error messages joined with periods



113
114
115
# File 'lib/cmdx/errors.rb', line 113

def to_s
  full_messages.values.flatten.join(". ")
end