Bread-Board-LazyLoader

 view release on metacpan or  search on metacpan

lib/Bread/Board/LazyLoader/Obj.pm  view on Meta::CPAN

package # hide from PAUSE
  Bread::Board::LazyLoader::Obj;

# DEPRECATED - use Bread::Board::LazyLoader qw(load_container)

use Moose;

# ABSTRACT: lazy loader for Bread::Board containers


use Moose::Util ();
use Bread::Board;
use List::MoreUtils qw(uniq);
#use Bread::Board::LazyLoader::Container;
use Carp qw(confess);

# default name
has name => ( is => 'ro', required => 1, default => 'Root' );

# remember the subs returned from builder files
has cache_codes => ( is => 'ro', default => 1 );

# builders (files and codes) for current container
has builders => ( is => 'ro', isa => 'ArrayRef', default => sub { [] }, );

has container_class => (
    is      => 'ro',
    default => 'Bread::Board::Container',
);

sub get_builder_paths {
    my $this = shift;
    my $prefix = shift // '';

    return grep { $prefix eq '' || m{^$prefix(?:/|$)} }
        map { $_->[0] } @{ $this->builders };
}

sub _normalize_path {
    my ($path) = @_;

    return
        defined $path
        ? join( '/', grep { length($_) > 0 } split m{/}, $path )
        : '';
}

sub _sub_path {
    my ($parent, $name) = @_;

    return join '/', grep { $_ ne ''} $parent, $name;
}

sub add_builder {
    my ($this, $path, $code) = @_;

    push @{$this->builders}, [ $path, $code ];
}


sub add_file {
    my ( $this, $file, $where ) = @_;

    -f $file or confess "No file '$file' found";

    $this->add_builder(
        _normalize_path($where),
        sub {
            my ($this, $c) = @_;
            $this->apply_file($c, $file);
        }
    );
}

sub add_code {
    my ( $this, $code, $where ) = @_;

    ref($code) eq 'CODE'
        or confess "\$builder->add_code( CODEREF, [ \$under ])\n";
    $this->add_builder(
        _normalize_path($where),
        sub {
            my ($this, $c) = @_;
            $this->apply_code($c, $code);



( run in 0.775 second using v1.01-cache-2.11-cpan-98e64b0badf )