Class: Stannum::Messages::DefaultLoader
- Inherits:
-
Object
- Object
- Stannum::Messages::DefaultLoader
- Defined in:
- lib/stannum/messages/default_loader.rb
Overview
Loads and parses messages configuration files and merges configuration data.
Instance Attribute Summary collapse
-
#file_paths ⇒ Array<String>
readonly
The directories from which to load the configuration files.
-
#locale ⇒ String
readonly
The name of the locale for which to load configuration.
Instance Method Summary collapse
-
#call ⇒ Hash<Symbol, Object>
(also: #load)
Loads and parses each file, then deep merges the data from each file.
-
#initialize(file_paths:, locale: 'en') ⇒ DefaultLoader
constructor
A new instance of DefaultLoader.
Constructor Details
#initialize(file_paths:, locale: 'en') ⇒ DefaultLoader
Returns a new instance of DefaultLoader.
15 16 17 18 |
# File 'lib/stannum/messages/default_loader.rb', line 15 def initialize(file_paths:, locale: 'en') @file_paths = file_paths @locale = locale end |
Instance Attribute Details
#file_paths ⇒ Array<String> (readonly)
Returns the directories from which to load the configuration files.
22 23 24 |
# File 'lib/stannum/messages/default_loader.rb', line 22 def file_paths @file_paths end |
#locale ⇒ String (readonly)
Returns the name of the locale for which to load configuration.
25 26 27 |
# File 'lib/stannum/messages/default_loader.rb', line 25 def locale @locale end |
Instance Method Details
#call ⇒ Hash<Symbol, Object> Also known as: load
Loads and parses each file, then deep merges the data from each file.
The configuration file should be either a Ruby file or a YAML file, with the filename of the format locale.extname, e.g. en.rb or en-gb.yml, and located in one of the directories defined in #file_paths.
The contents of each file should be either a Ruby Hash or a YAML document containing an associative array, with a single key equal to the locale. The value of the key must be a Hash or associative array, which contains the scoped messages to load.
Each file is read in order and parsed into a Hash. Each hash is then deep merged in sequence, with nested hashes merged together instead of overwritten.
43 44 45 46 47 48 49 |
# File 'lib/stannum/messages/default_loader.rb', line 43 def call file_paths.reduce({}) do |config, file_path| loaded = load_configuration_file(file_path) deep_update(config, loaded) end end |