Class: Stannum::Messages::DefaultStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/stannum/messages/default_strategy.rb

Overview

Strategy to generate error messages from gem configuration.

Constant Summary collapse

DEFAULT_LOAD_PATHS =

The default directories from which to load configured error messages.

[Stannum::Messages.locales_path].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil, load_paths: nil, locale: 'en') ⇒ DefaultStrategy

Returns a new instance of DefaultStrategy.

Parameters:

  • configuration (Hash{Symbol, Object}) (defaults to: nil)

    The configured messages.

  • load_paths (Array<String>) (defaults to: nil)

    The directories from which to load configured error messages.

  • locale (String) (defaults to: 'en')

    The locale used to load and scope configured messages.



26
27
28
29
30
# File 'lib/stannum/messages/default_strategy.rb', line 26

def initialize(configuration: nil, load_paths: nil, locale: 'en')
  @load_paths    = Array(load_paths) unless load_paths.nil?
  @locale        = locale
  @configuration = configuration
end

Instance Attribute Details

#localeString (readonly)

Returns The locale used to load and scope configured messages.

Returns:

  • (String)

    The locale used to load and scope configured messages.



33
34
35
# File 'lib/stannum/messages/default_strategy.rb', line 33

def locale
  @locale
end

Class Method Details

.load_pathsArray<String>

Returns The directories from which to load configured error messages.

Returns:

  • (Array<String>)

    The directories from which to load configured error messages.



17
18
19
# File 'lib/stannum/messages/default_strategy.rb', line 17

def self.load_paths
  @load_paths ||= DEFAULT_LOAD_PATHS.dup
end

Instance Method Details

#call(error_type, **options) ⇒ Object

Parameters:

  • error_type (String)

    The qualified path to the configured error message.

  • options (Hash)

    Additional properties to interpolate or to pass to the message proc.



39
40
41
42
43
44
45
46
47
# File 'lib/stannum/messages/default_strategy.rb', line 39

def call(error_type, **options)
  unless error_type.is_a?(String) || error_type.is_a?(Symbol)
    raise ArgumentError, 'error type must be a String or Symbol'
  end

  message = generate_message(error_type, options)

  interpolate_message(message, options)
end

#load_pathsArray<String>

Returns The directories from which to load configured error messages.

Returns:

  • (Array<String>)

    The directories from which to load configured error messages.



51
52
53
# File 'lib/stannum/messages/default_strategy.rb', line 51

def load_paths
  @load_paths || self.class.load_paths
end

#reload_configuration!DefaultStrategy

Reloads the configuration from the configured load_path.

This can be useful when the load_path is updated after creating the strategy, such as in an initializer for another gem.

Returns:



61
62
63
64
65
# File 'lib/stannum/messages/default_strategy.rb', line 61

def reload_configuration!
  @configuration = load_configuration

  self
end