NAME
Gears::Config::Reader - Base class for configuration readers
SYNOPSIS
package My::Config::Reader::JSON;
use v5.40;
use Mooish::Base -standard;
use JSON::MaybeXS;
extends 'Gears::Config::Reader';
sub handled_extensions ($self)
{
return qw(json);
}
sub parse ($self, $config, $filename)
{
return decode_json($self->_get_contents($filename));
}
DESCRIPTION
Gears::Config::Reader is an abstract base class for configuration file readers. Readers are responsible for determining if they can handle a particular file (based on extension) and parsing that file into a hash reference.
Subclasses must implement handled_extensions and parse methods to provide format-specific functionality.
EXTENDING
To create a new configuration reader:
- 1. Extend Gears::Config::Reader
- 2. Implement
handled_extensionsto return supported file extensions - 3. Implement
parseto read and parse the file
Refer to "SYNOPSIS" for an example. New reader must be included in the "readers" in Gears::Config array to be used during config loading.
INTERFACE
Methods
new
$object = $class->new(%args)
A standard Mooish constructor.
handles
$bool = $reader->handles($filename)
Returns true if this reader can handle the given filename, based on its extension. The default implementation checks if the file extension matches any of the extensions returned by "handled_extensions".
handled_extensions
@extensions = $reader->handled_extensions()
Must be implemented by subclasses
Returns a list of file extensions (without dots) that this reader can handle. Extensions must not contain dots.
Example:
sub handled_extensions ($self)
{
return qw(yaml yml);
}
parse
$hash_ref = $reader->parse($config, $filename)
Must be implemented by subclasses
Parses the configuration file and returns a hash reference. The $config parameter is the Gears::Config instance, which can be used for nested configuration loading if needed. Helper method _get_contents may be used to get the file content of a configuration file.
Example:
sub parse ($self, $config, $filename)
{
return decode_yaml($self->_get_contents($filename));
}