Class: Stannum::Messages::DefaultLoader

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

Overview

Loads and parses messages configuration files and merges configuration data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_paths:, locale: 'en') ⇒ DefaultLoader

Returns a new instance of DefaultLoader.

Parameters:

  • file_paths (Array<String>)

    The directories from which to load the configuration files.

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

    The name of the locale for which to load configuration.



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_pathsArray<String> (readonly)

Returns the directories from which to load the configuration files.

Returns:

  • (Array<String>)

    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

#localeString (readonly)

Returns the name of the locale for which to load configuration.

Returns:

  • (String)

    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

#callHash<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.

Returns:

  • (Hash<Symbol, Object>)

    the merged configuration data.



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