Class: NextStation::Operation::ErrorDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/next_station/operation/errors.rb

Overview

Defines an error with its messages and optional help URL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ ErrorDefinition

Returns a new instance of ErrorDefinition.

Parameters:

  • type (Symbol)

    The error type.



38
39
40
41
42
# File 'lib/next_station/operation/errors.rb', line 38

def initialize(type)
  @type = type
  @messages = {}
  @help_url = nil
end

Instance Attribute Details

#help_url(url = nil) ⇒ String? (readonly)

Sets or returns the help URL for the error.

Parameters:

  • url (String, nil) (defaults to: nil)

    The URL to set.

Returns:

  • (String, nil)

    The current help URL.



35
36
37
# File 'lib/next_station/operation/errors.rb', line 35

def help_url
  @help_url
end

#messagesHash (readonly)

Returns Map of locales to message templates.

Returns:

  • (Hash)

    Map of locales to message templates.



33
34
35
# File 'lib/next_station/operation/errors.rb', line 33

def messages
  @messages
end

#typeSymbol (readonly)

Returns The error type.

Returns:

  • (Symbol)

    The error type.



31
32
33
# File 'lib/next_station/operation/errors.rb', line 31

def type
  @type
end

Instance Method Details

#message(hashes) ⇒ Object

Adds localized messages for the error.

Parameters:

  • hashes (Hash)

    A hash mapping locale symbols to message templates.



46
47
48
# File 'lib/next_station/operation/errors.rb', line 46

def message(hashes)
  @messages.merge!(hashes)
end

#resolve_message(lang, msg_keys) ⇒ String

Resolves the error message for a given language.

Parameters:

  • lang (Symbol, String)
  • msg_keys (Hash)

Returns:

  • (String)


70
71
72
73
# File 'lib/next_station/operation/errors.rb', line 70

def resolve_message(lang, msg_keys)
  template = @messages[lang.to_sym] || @messages[:en]
  template % msg_keys
end

#validate!Object

Validates whether the error definition is complete.

Raises:

  • (RuntimeError)

    if the English message is missing.



62
63
64
# File 'lib/next_station/operation/errors.rb', line 62

def validate!
  raise "English message is required for error type: #{@type}" unless @messages[:en]
end