Created
December 24, 2009 00:28
-
-
Save perigrin/262929 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use 5.10.0; | |
| use MooseX::Declare; | |
| use Bread::Board; | |
| class ParentConfig { | |
| has base_directory => ( is => 'ro', isa => 'Str', required => 1 ); | |
| has media_directory => ( | |
| isa => 'Str', | |
| is => 'ro', | |
| lazy_build => 1, | |
| ); | |
| sub _build_media_directory { shift->base_directory . '/media' } | |
| } | |
| class HostConfig { | |
| has hostname => ( is => 'ro', isa => 'Str', required => 1 ); | |
| has base_directory => ( is => 'ro', isa => 'Str', required => 1 ); | |
| } | |
| my $c = container ExampleConfig => as { | |
| service base_directory => '/var/tmp/myapp.config'; | |
| service parent_config => ( | |
| class => 'ParentConfig', | |
| lifecycle => 'Singleton', | |
| dependencies => [ depends_on('base_directory'), ], | |
| ); | |
| service hostnames => ( | |
| block => sub { | |
| return [ | |
| qw( | |
| foo.com | |
| bar.com | |
| baz.com | |
| ) | |
| ]; | |
| } | |
| ); | |
| service host_configs => ( | |
| block => sub { | |
| my @hosts; | |
| for my $name ( @{ $_[0]->param('hostnames') } ) { | |
| push @hosts => HostConfig->new( | |
| base_directory => $_[0]->param('parent_config'), | |
| hostname => $name | |
| ); | |
| } | |
| return \@hosts; | |
| }, | |
| dependencies => | |
| [ depends_on('hostnames'), depends_on('parent_config'), ], | |
| ); | |
| service app_config => ( | |
| block { | |
| return { | |
| parent_config => $_[0]->param('parent_config'), | |
| host_configs => $_[0]->param('host_configs'), | |
| }; | |
| } | |
| ); | |
| }; | |
| my $config = $c->fetch('app_config')->get; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment