Class: Stannum::Messages::DefaultStrategy
- Inherits:
-
Object
- Object
- Stannum::Messages::DefaultStrategy
- 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
-
#locale ⇒ String
readonly
The locale used to load and scope configured messages.
Class Method Summary collapse
-
.load_paths ⇒ Array<String>
The directories from which to load configured error messages.
Instance Method Summary collapse
- #call(error_type, **options) ⇒ Object
-
#initialize(configuration: nil, load_paths: nil, locale: 'en') ⇒ DefaultStrategy
constructor
A new instance of DefaultStrategy.
-
#load_paths ⇒ Array<String>
The directories from which to load configured error messages.
-
#reload_configuration! ⇒ DefaultStrategy
Reloads the configuration from the configured load_path.
Constructor Details
#initialize(configuration: nil, load_paths: nil, locale: 'en') ⇒ DefaultStrategy
Returns a new instance of DefaultStrategy.
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
#locale ⇒ String (readonly)
Returns 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_paths ⇒ Array<String>
Returns 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
39 40 41 42 43 44 45 46 47 |
# File 'lib/stannum/messages/default_strategy.rb', line 39 def call(error_type, **) unless error_type.is_a?(String) || error_type.is_a?(Symbol) raise ArgumentError, 'error type must be a String or Symbol' end = (error_type, ) (, ) end |
#load_paths ⇒ Array<String>
Returns 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.
61 62 63 64 65 |
# File 'lib/stannum/messages/default_strategy.rb', line 61 def reload_configuration! @configuration = load_configuration self end |