File Coverage

blib/lib/Bread/Board/LazyLoader/Site.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package # hide from PAUSE
2             Bread::Board::LazyLoader::Site;
3              
4             # DEPRECATED - use Bread::Board::LazyLoader qw(load_container)
5              
6 1     1   13088 use strict;
  1         2  
  1         19  
7 1     1   3 use warnings;
  1         1  
  1         16  
8              
9             # ABSTRACT: loads tree of IOC files alongside pm file
10              
11              
12             # imports methods root into caller's namespace
13              
14 1     1   345 use Bread::Board::LazyLoader;
  0            
  0            
15             use Carp qw(confess croak);
16             use Class::Load;
17              
18             # import imports into caller namespace
19             # root - returns the appropriate root
20              
21             sub import {
22             my $this = shift;
23             my ( $caller_package, $caller_filename ) = caller;
24              
25             my $to_import = $this->_build( $caller_package, $caller_filename, @_ );
26             for my $method ( keys %$to_import ) {
27             no strict 'refs';
28             *{ join '::', $caller_package, $method } = $to_import->{$method};
29             }
30             }
31              
32             sub _throw {
33             croak join '', __PACKAGE__, '->import: ', @_, "\n";
34             }
35              
36             sub _build {
37             my ( $this, $caller_package, $caller_filename, %args ) = @_;
38              
39             # base is a package which loader we use and add to
40             my $base = delete $args{base};
41             if ($base) {
42             Class::Load::load_class($base);
43             $base->can('loader')
44             or _throw "base package '$base' has no method loader";
45             }
46              
47             # load all ioc files "belonging" to perl file
48             # given $dir/Manggis/Core.pm
49             # loads all *.ioc files under $dir/Manggis/Core/
50             my $dir = delete $args{dir}
51             || do {
52              
53             # we add files according to *.pm
54             $caller_filename =~ /^(.*)\.pm$/;
55             $1;
56             };
57              
58             -d $dir or _throw "There is no directory $dir to look for ioc files";
59              
60             my $suffix = delete $args{suffix} || 'ioc';
61              
62             !%args
63             or _throw sprintf
64             "Unrecognized or ambiguous parameters (%s)", join( ', ', keys %args );
65              
66             return {
67             loader => sub {
68             my $loader = Bread::Board::LazyLoader->new;
69             $loader->add_tree( $dir, $suffix );
70             return $loader;
71             },
72             root => sub {
73             my $this = shift;
74             return $this->loader->build( $base ? $base->root(@_) : @_ );
75             },
76             };
77             }
78              
79             1;
80              
81             # vim: expandtab:shiftwidth=4:tabstop=4:softtabstop=0:textwidth=78:
82              
83             __END__
84              
85             =pod
86              
87             =encoding UTF-8
88              
89             =head1 NAME
90              
91             Bread::Board::LazyLoader::Site - loads tree of IOC files alongside pm file
92              
93             =head1 VERSION
94              
95             version 0.14
96              
97             =head1 SYNOPSIS
98              
99             In module dir we have files, each one containing the definition of
100             one Bread::Board::Container
101              
102             lib/My/Site/Supp/Database.ioc
103             lib/My/Site/Root.ioc
104             lib/My/Site/Config.ioc
105             lib/My/Site/Database.ioc
106             lib/My/Site/Planner.ioc
107             lib/My/Site/AQ.ioc
108              
109             the "site" module C<lib/My/Site.pm> is defined like
110              
111             package My::Site;
112             use strict;
113             use warnings;
114              
115             use Bread::Board::LazyLoader::Site;
116              
117             1;
118              
119             in the script
120              
121             use My::Site;
122              
123             my $root = My::Site->root;
124             my $db_container = $root->fetch('Database');
125             my $dbh = $root->resolve(service => 'Database/dbh');
126              
127             =head1 DESCRIPTION
128              
129             Site module is a module with a class method C<root> returning an instance of C<Bread::Board::Container>.
130              
131             C<Bread::Board::LazyLoader::Site> just imports such C<root> method.
132              
133             =head2 import parameters
134              
135             use Bread::Board::LazyLoader::Site %params;
136              
137             =over 4
138              
139             =item dir
140              
141             Directory searched for container files. By default it is the directory with the same name
142             as module file without suffix.
143              
144             =item suffix
145              
146             Suffix of container files. By default C<ioc>.
147              
148             =item base
149              
150             Another site module. All container files are loaded on top of the base file containers.
151              
152             =back
153              
154             =head1 AUTHOR
155              
156             Roman Daniel <roman@daniel.cz>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2016 by Roman Daniel.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut