Class: Servus::Support::MessageResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/support/message_resolver.rb

Overview

Resolves message templates with interpolation support.

Handles multiple template formats:

  • String: Static or with %key / %s interpolation
  • Symbol: I18n key lookup with fallback
  • Hash: Inline translations keyed by locale
  • Proc: Dynamic template evaluated at runtime

Examples:

Basic string interpolation

resolver = MessageResolver.new(
  template: 'Hello, %<name>s!',
  data: { name: 'World' }
)
resolver.resolve # => "Hello, World!"

With I18n symbol

resolver = MessageResolver.new(template: :greeting)
resolver.resolve # => I18n.t('greeting') or "Greeting" fallback

With inline translations

resolver = MessageResolver.new(
  template: { en: 'Hello', es: 'Hola' }
)
resolver.resolve # => "Hello" (or "Hola" if I18n.locale == :es)

With proc and context

resolver = MessageResolver.new(
  template: -> { "Balance: #{balance}" }
)
resolver.resolve(context: ) # => "Balance: 100"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:, data: nil, i18n_scope: nil) ⇒ MessageResolver

Creates a new message resolver.

Parameters:

  • template (String, Symbol, Hash, Proc, nil)

    the message template

  • data (Hash, Proc, nil) (defaults to: nil)

    interpolation data or block returning data

  • i18n_scope (String) (defaults to: nil)

    prefix for I18n lookups (default: nil)



51
52
53
54
55
# File 'lib/servus/support/message_resolver.rb', line 51

def initialize(template:, data: nil, i18n_scope: nil)
  @template   = template
  @data       = data
  @i18n_scope = i18n_scope
end

Instance Attribute Details

#dataHash, ... (readonly)

Returns the interpolation data or data-providing block.

Returns:

  • (Hash, Proc, nil)

    the interpolation data or data-providing block



41
42
43
# File 'lib/servus/support/message_resolver.rb', line 41

def data
  @data
end

#i18n_scopeString? (readonly)

Returns the I18n scope prefix for symbol templates.

Returns:

  • (String, nil)

    the I18n scope prefix for symbol templates



44
45
46
# File 'lib/servus/support/message_resolver.rb', line 44

def i18n_scope
  @i18n_scope
end

#templateString, ... (readonly)

Returns the message template.

Returns:

  • (String, Symbol, Hash, Proc, nil)

    the message template



38
39
40
# File 'lib/servus/support/message_resolver.rb', line 38

def template
  @template
end

Instance Method Details

#resolve(context: nil) ⇒ String

Resolves the template to a final string.

Parameters:

  • context (Object, nil) (defaults to: nil)

    object for evaluating Proc templates/data blocks

Returns:

  • (String)

    the resolved and interpolated message



61
62
63
64
65
66
# File 'lib/servus/support/message_resolver.rb', line 61

def resolve(context: nil)
  resolved_template = resolve_template(context)
  resolved_data     = resolve_data(context)

  interpolate(resolved_template, resolved_data)
end