MooseX-ConfigCascade

 view release on metacpan or  search on metacpan

lib/MooseX/ConfigCascade.pm  view on Meta::CPAN

package MooseX::ConfigCascade;

our $VERSION = '0.02';

use Moose::Role;
use MooseX::ConfigCascade::Util;

has cascade_util => (is => 'ro', lazy => 1, isa => 'MooseX::ConfigCascade::Util', default => sub{
    MooseX::ConfigCascade::Util->new(
        _to_set => $_[0],
        _role_name => __PACKAGE__
    );
});


sub BUILD{}
after BUILD => sub{
    my ($self,$args) = @_;
    my $util = $self->cascade_util;
    foreach my $k (keys %$args){ $util->_args->{$k} = 1 }
    $util->_parse_atts;
};


1;
__END__
=head1 NAME

MooseX::ConfigCascade - Set initial accessor values of your whole Moose-based project from a single config file

=head1 SYNOPSIS

    # /my_conf.json:

        "My::Bottle": {
            "label": {
                "logo": {
                    "company_name": "Bottle Company Name",
                    "slogan": "Bottle Slogan"
                }
            }
        },

        "My::Label": {
            "logo": {
                "company_name": "Label Company Nmae",
                "slogan": "Label Slogan"
            }
        },


        "My::Logo": {

            "company_name": "Logo Company Name",
            "slogan": "Logo Slogan",
           
        }

    # Packages:

    package Bottle;

    use Moose;
    with 'MooseX::ConfigCascade';  # MooseX::ConfigCascade is a Moose role

    has label => (is => 'rw', isa => 'My::Label', default => sub{
        My::Label->new;
    });
    

    package Label;

    use Moose;
    with 'MooseX::ConfigCascade';

    has logo => (is => 'rw', isa => 'My::Logo', default => sub {
        My::Logo->new;
    });
    

lib/MooseX/ConfigCascade.pm  view on Meta::CPAN

    use Moose;
    with 'MooseX::ConfigCascade';

    has size => (is => 'ro', isa => 'Pets::Size');

    # ...


    package Pets::BigDog;

    use Moose;
    extends 'Pets::Dog';

    # ...


    package Pets::SmallDog;

    use Moose;
    extends 'Pets::Dog';

    # ...


    package Pets::Size;

    use Moose;
    with 'MooseX::ConfigCascade';

    has height => (is => 'ro', isa => 'Int');
    has weight => (is => 'ro', isa => 'Int');


C<Pets::BigDog> and C<Pets::SmallDog> both inherit the C<size> attribute from C<Pets::Dog> - but the attributes in the C<Pets::Size> object contained in the C<size> attribute get assigned different defaults.


=head2 Loading Order

Loading of attributes happens after C<BUILD>. This means if you use C<BUILD> to assign values to attributes, L<MooseX::ConfigCascade> may overwrite those values (depending if there are values for those attributes specified in the config). If you want...

    after 'BUILD' => sub {
        # overwrite the attributes here
    };

(but perhaps you shouldn't be assigning config values to attributes in the first place if you are then going to want to overwrite them?)

You can also make sure objects get individual values by specifying them in the objects constructor in the normal way:

    my $widget = Widget->new( my_accessor => 'this value will win' );



=head1 METHODS

Remember B<not> to C<use MooseX::ConfigCascade>. It's a role, so you should state:

    with 'MooseX::ConfigCascade';

When you do this, a single new attribute is added to your class:

=head2 cascade_util

This is a L<MooseX::ConfigCascade::Util> object, which has 3 utility methods. So once you added the L<MooseX::ConfigCascade> role to your package, you can do:

    my $object = My::Package->new;

    $object->cascade_util->conf;     # access the config hash directly
    $object->cascade_util->path;     # the path to the config file (if any)
    $object->cascade_util->parser;   # the code ref to the subroutine which parses your config file

Note C<conf>, C<path> and C<parser> are all B<class attributes> of L<MooseX::ConfigCascade::Util>. That means it is intended that you generally set them by calling the class directly:

    MooseX::ConfigCascade::Util->path( '/path/to/config.yaml' );

    # etc ...

so you may not ever need to use C<cascade_util> at all. However, you may find it useful that you can access the full config from anywhere in your project:

    $whatever_object->cascade_util->conf;

See the documentation for L<MooseX::ConfigCascade::Util> for information about these methods.


=head1 SEE ALSO

L<MooseX::ConfigCascade::Util>
L<Moose>
L<MooseX::ClassAttribute>

=head1 AUTHOR

Tom Gracey E<lt>tomgracey@gmail.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2017 by Tom Gracey

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 2.493 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )