Config::Registry - Settings bundler.
package Org::Style;
use strictures 2;
use Types::Standard qw( Str );
use Moo;
use namespace::clean;
extends 'Config::Registry';
__PACKAGE__->schema({
border_color => Str,
});
1;
package MyApp::Style;
use strictures 2;
use Moo;
use namespace::clean;
extends 'Org::Style';
__PACKAGE__->document({
border_color => '#333',
});
__PACKAGE__->publish();
1;
use MyApp::Style;
my $style = MyApp::Style->fetch();
print '<table style="border-color:' . $style->border_color() . '">';
This module provides a framework for a pattern we've seen emerge in ZipRecruiter code as we've been working to separate our monolithic application into smaller and more manageable code bases.
The concept is pretty straightforward. A registry consists of a schema class and one or more document classes. The schema is used to validate the documents, and the documents are used to configure the features of an application.
__PACKAGE__->schema({
border_color => Str,
});
The schema is a hash ref of attribute name and Type::Tiny pairs. These pairs get turned into required Moo attributes when "publish" is called.
Top-level schema keys may have a hash ref, rather than a type, as
their value. This hash ref will be used directly to construct the
Moo attribute. The required
option defaults on, and the is
option default to ro
. You can of course override these in the
hash ref.
For example, the above code could be written as:
__PACKAGE__->schema({
border_color => { isa => Str },
});
The attribute can be made optional by passing an options hash ref:
__PACKAGE__->schema({
border_color => { isa => Str, required => 0 },
});
Non-top level keys can be made optional using Type::Standard's
Optional
type modifier:
__PACKAGE__->schema({
border_colors => Dict[
top => Optional[ Str ],
right => Optional[ Str ],
bottom => Optional[ Str ],
left => Optional[ Str ],
],
});
See "Create a Schema Role" for a complete example.
__PACKAGE__->document({
border_color => '#333',
});
A document is a hash ref of attribute name value pairs.
A document is used as the default arguments when new
is called
on the registry class.
See "Create a Document Class" for a complete example.
my $registry = $class->fetch();
Returns the singleton instance of the registry class.
__PACKAGE__->schema( \%schema );
Sets the schema hash ref. If a schema hash ref has already been set then "merge" will be used to combine the passed in schema with the existing schema.
See "SCHEMAS" for more information about the schema hash ref itself.
__PACKAGE__->document( \%doc );
Sets the document hash ref. If a document hash ref has already been set then "merge" will be used to combine the passed in document with the existing document.
See "DOCUMENTS" for more information about the document hash ref itself.
__PACKAGE__->publish();
Turns the "schema" hash ref into Moo attributes and enables the registry class to be instantiated.
Please submit bugs and feature requests to the Config-Registry GitHub issue tracker:
https://github.com/bluefeet/Config-Registry/issues
Thanks to ZipRecruiter for encouraging their employees to contribute back to the open source ecosystem. Without their dedication to quality software development this distribution would not exist.
Aran Clary Deltac <bluefeet@gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.