- Parent Namespace
- SleepingKingStudios::Tools::Messages::Strategies
- Inherited Classes
- SleepingKingStudios::Tools::Messages::Strategies::HashStrategy
>
SleepingKingStudios::Tools::Messages::Strategy
>
Object
- Defined In
- lib/sleeping_king_studios/tools/messages/strategies/file_strategy.rb
Table Of Contents
Overview
Messaging strategy that loads message templates from the given file.
Internally, the parsed templates are flattened - a nested Hash of
{ foo: { bar: { baz: ‘template’ } } } is stored as a flat Hash with
{ ‘foo.bar.baz’ => ‘template’ }. This makes lookup of scoped keys more
efficient but prevents retrieving non-leaf nodes (such as “foo” or “foo.bar”
in the above Hash). If you need to retrieve Hash values, initialize the
strategy with flatten_templates: false.
Back To Top
Defined Under Namespace
- Classes
- FileError
Back To Top
Constructor
#initialize(file_name, flatten_templates: true) => FileStrategy
Parameters
- file_name (String) — the full path to the file with the templates
data.
- flatten_templates (true, false) — if true, the templates are
flattened internally. Defaults to true.
Returns
- (FileStrategy) — a new instance of FileStrategy
Back To Top
Instance Attributes
#file_name => String (readonly)
Returns
- (String) — the full path to the file with the templates data.
#templates => Hash (readonly)
- Inherited From
- SleepingKingStudios::Tools::Messages::Strategies::HashStrategy
Returns
- (Hash) — the templates used to generate messages.
Back To Top
Instance Methods
#call(key, parameters: {}, scope: nil, **options) => String
- Inherited From
- SleepingKingStudios::Tools::Messages::Strategy
Generates a formatted string for the given key, parameters, and options.
If the strategy does not define a message for the key, you can provide a
default value.
- If the default value is a Proc, it will be called with the fully
scoped key, as a positional argument as well as any additional
keywords passed to #call. The value returned by the Proc will be
returned by #call.
- If the default value is any other Object (including nil), the default
value will be returned by #call.
- If a default value is not provided, a missing message warning will be
generated and returned.
Parameters
- key (String, Symbol) — the key used to resolve the message.
- default (Object) — the default value to return if the strategy does
not define a message for the key.
- parameters (Hash) — the parameters used to generate the message,
such as values for a template string.
- scope (String) — 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.
Returns
- (String) — the generated template string, or a generic error
string if a message is not defined for the given key and parameters.
#fetch(key, default = nil, scope: nil, **options) => Object
#fetch(key, scope: nil, **options, &default) => Object
- Inherited From
- SleepingKingStudios::Tools::Messages::Strategy
Overloads
#fetch(key, default = nil, scope: nil, **options) => Object
Retrieves the messages template with the given scope and options.
If there is no template matching the given key, scope, and options,
returns the default value or raises a KeyError if no default value is
provided.
Parameters
- key (String, Symbol) — the key used to resolve the message.
- default (Object) — the default value to return if the strategy does
not define a message for the key.
- scope (String) — 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.
Returns
- (Object) — the stored template for the requested key, or the
provided default value.
#fetch(key, scope: nil, **options, &default) => Object
If there is no template matching the given key, scope, and options,
the block is called with the scoped key and the value returned by the
block is returned by #fetch.
Parameters
- key (String, Symbol) — the key used to resolve the message.
- scope (String) — 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.
Yield Parameters
- scoped_key (String) — the fully scoped key for the requested
template, combining the given key and the given scope, if any.
- options (Hash) — additional options for resolving the message.
Yield Returns
- (Object) — the default value for the given scoped key.
Returns
- (Object) — the stored template for the requested key, or the value
returned by the provided default block.
Raises
#get(key, scope: nil, **options) => Object
- Inherited From
- SleepingKingStudios::Tools::Messages::Strategy
Retrieves the messages template with the given scope and options.
Parameters
- key (String, Symbol) — the key used to resolve the message.
- scope (String) — 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.
Returns
- (Object) — the stored template for the requested key, or nil if
there is no matching template.
Back To Top
Back to
Documentation |
Reference
|
SleepingKingStudios
|
SleepingKingStudios::Tools
|
SleepingKingStudios::Tools::Messages
|
SleepingKingStudios::Tools::Messages::Strategies