File Coverage

blib/lib/Pickles/Container.pm
Criterion Covered Total %
statement 58 61 95.0
branch 13 20 65.0
condition 4 5 80.0
subroutine 16 17 94.1
pod 0 10 0.0
total 91 113 80.5


line stmt bran cond sub pod time code
1             package Pickles::Container;
2 4     4   5390 use strict;
  4         11  
  4         125  
3 4     4   21 use Carp ();
  4         11  
  4         786  
4              
5             sub new {
6 7     7 0 575 my ($class, %args) = @_;
7 7         56 bless {
8             components => {},
9             home => $args{home},
10             objects => {},
11             scoped_objects => {},
12             }, $class;
13             }
14              
15             sub Pickles::Container::ScopeGuard::DESTROY {
16 6     6   2958 my $self = shift;
17 6         52 $self->{container}->clear_scope();
18             }
19              
20             sub new_scope {
21 6     6 0 55 my $self = shift;
22 6         33 return bless { container => $self }, 'Pickles::Container::ScopeGuard';
23             }
24              
25             sub clear_scope {
26 6     6 0 11 my $self = shift;
27 6         57 $self->{scoped_objects} = {};
28             }
29              
30             sub load {
31 6     6 0 12 my ($self, $file) = @_;
32              
33 6         16 my $o = \®ister;
34 4     4   23 no warnings 'redefine';
  4         9  
  4         2355  
35             local *register = sub($$;$) {
36 14     14   1138 $o->( $self, @_ );
37 6         40 };
38             local *load_file = sub(@) {
39 1     1   6 my $c = $self->get('config');
40             # XXX what if there's no config?
41 1         4 my $file = $c->path_to(@_);
42              
43 1         751 delete $INC{$file};
44 1         1190 my $rv = require $file;
45 1 50       5 Carp::croak("Failed to parse file $file: $@") if $@;
46 1 50       4 Carp::croak("Failed to run file (did you return a true value?)") unless $rv;
47 1         9 return $rv;
48 6         35 };
49 6         3353 my $result = do $file;
50 6 50       25 die "Failed to parse file $file: $@" if $@;
51 6 50       16 die "Failed to run file (did you return a true value?)" unless $result;
52 6         66 $self;
53             }
54              
55             sub components {
56 18     18 0 27 my $self = shift;
57 18         48 my $h = $self->{components};
58 18 50       45 if (! $h) {
59 0         0 $self->{components} = ($h = {});
60             }
61 18         95 return $h;
62             }
63              
64 0     0 0 0 sub home { $_[0]->{home} }
65 15     15 0 89 sub objects { $_[0]->{objects} }
66 6     6 0 22 sub scoped_objects { $_[0]->{scoped_objects} }
67              
68             sub register {
69 23     23 0 109 my ($self, $name, $component, $opts) = @_;
70              
71 23 100       74 if (ref $component eq 'CODE') {
72 10   100     55 $opts ||= {};
73 10         39 my %data = (
74             %$opts,
75             initializer => $component,
76             );
77 10         40 $self->components->{ $name } = \%data;
78             } else {
79 13         101 $self->objects->{$name} = $component;
80             }
81             }
82              
83             sub get {
84 16     16 0 98 my ($self, $name, @args) = @_;
85 16   66     90 my $object = $self->{objects}->{$name} || $self->{scoped_objects}->{$name};
86 16 100       41 if (! $object) {
87 8         24 my $data = $self->components->{ $name };
88 8         30 $object = $self->_construct_object($data, @args);
89 8 50       86 if ($object) {
90 8 100       24 if ($data->{persistent}) {
91 2         6 $self->objects->{$name} = $object;
92             } else {
93 6         22 $self->scoped_objects->{$name} = $object;
94             }
95             }
96             }
97 16         85 return $object;
98             }
99              
100             sub _construct_object {
101 8     8   14 my ($self, $data, @args) = @_;
102 8 50       22 if (! $data) {
103 0         0 return ();
104             }
105 8         147 $data->{initializer}->( $self, @args );
106             }
107              
108             1;
109              
110             __END__