Class: SleepingKingStudios::Tools::Messages

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/messages.rb

Overview

Utility for generating configured, user-readable output strings.

Defined Under Namespace

Modules: Strategies Classes: Registry, Strategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

instance, #toolbelt

Constructor Details

#initialize(registry:, toolbelt: nil) ⇒ Messages

Returns a new instance of Messages.

Parameters:



17
18
19
20
21
22
# File 'lib/sleeping_king_studios/tools/messages.rb', line 17

def initialize(registry:, toolbelt: nil)
  super(toolbelt:)

  @registry =
    registry || SleepingKingStudios::Tools::Messages::Registry.global
end

Instance Attribute Details

#registrySleepingKingStudios::Tools::Messages::Registry (readonly)

Returns the strategies registry to use for the tool.

Returns:



26
27
28
# File 'lib/sleeping_king_studios/tools/messages.rb', line 26

def registry
  @registry
end

Instance Method Details

#message(key, parameters: {}, scope: nil, **options) ⇒ Object

Generates a message from the given key, scope, and parameters.

Internally, finds a messages strategy matching the combined key and scope and calls that strategy. If a strategy is not found, instead returns a generic failure message.

Parameters:

  • key (String, Symbol)

    the key used to resolve the message.

  • parameters (Hash) (defaults to: {})

    the parameters used to generate the message, such as values for a template string.

  • scope (String) (defaults to: nil)

    the namespace for the key. Combined with the given key to generate the scoped key value.

  • options (Hash)

    additional options for resolving or generating the message.



42
43
44
45
46
47
48
49
# File 'lib/sleeping_king_studios/tools/messages.rb', line 42

def message(key, parameters: {}, scope: nil, **)
  scoped_key = join_scope(key:, scope:)
  strategy   = registry.get(scoped_key)

  return missing_message(scoped_key, **) unless strategy

  strategy.call(key, parameters:, scope:, **)
end