File Coverage

blib/lib/Context/Micro.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 2 100.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 3 3 100.0
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Context::Micro;
2              
3 2     2   41797 use 5.006;
  2         6  
  2         72  
4 2     2   9 use strict;
  2         3  
  2         74  
5 2     2   9 use warnings FATAL => 'all';
  2         8  
  2         84  
6 2     2   8 use Exporter 'import';
  2         2  
  2         486  
7              
8             our @EXPORT = qw( new config entry );
9              
10             =head1 NAME
11              
12             Context::Micro - Micro Context Class
13              
14             =head1 VERSION
15              
16             Version 0.01
17              
18             =cut
19              
20             our $VERSION = '0.01';
21              
22              
23             =head1 SYNOPSIS
24              
25             First, your context class
26              
27             package MyApp::Context;
28             use Context::Micro;
29             use DBI;
30            
31             sub db {
32             my $self = shift;
33             $self->entry( db => sub {
34             my $config = $self->config->{db};
35             DBI->connect( @{$config->{connect_info}} );
36             } );
37             }
38            
39             1;
40              
41             after, in your application
42              
43             package MyApp;
44             use MyApp::Context;
45            
46             my $config = {
47             db => {
48             connect_info => [ ... ],
49             },
50             ...
51             };
52            
53             my $context = MyApp::Context->new(config => $config);
54            
55             sub my_task {
56             my $self = shift;
57             my $db = $context->db; ### $db is a singleton instance.
58             my $sth = $db->prepare(...);
59             ...
60             }
61            
62             1;
63            
64             =head1 EXPORT
65              
66             =head2 new( %hash )
67              
68             Provide constructor automatically into your context class.
69              
70             =cut
71              
72             sub new {
73 1     1 1 22 my ($class, %opts) = @_;
74 1   50     6 my $config = $opts{config} || {};
75 1         5 return bless { config => $config, container => {} }, $class;
76             }
77              
78             =head2 config
79              
80             Provide config accessor.
81              
82             =cut
83              
84             sub config {
85 4     4 1 2254 my $self = shift;
86 4         26 return $self->{config};
87             }
88              
89             =head2 entry
90              
91             Returns an object with specified name. Store a result of coderef when object is not exists.
92              
93             =cut
94              
95             sub entry {
96 4     4 1 4010533 my ($self, $key, $code) = @_;
97 4 100       32 return $self->{container}{$key} ? $self->{container}{$key} : do {
98 2         5 my $o = $code->();
99 2         27 $self->{container}{$key} = $o;
100 2         6 $o;
101             };
102             }
103              
104             =head1 AUTHOR
105              
106             ytnobody, C<< >>
107              
108             =head1 BUGS
109              
110             Please report any bugs or feature requests to C, or through
111             the web interface at L. I will be notified, and then you'll
112             automatically be notified of progress on your bug as I make changes.
113              
114              
115              
116              
117             =head1 SUPPORT
118              
119             You can find documentation for this module with the perldoc command.
120              
121             perldoc Context::Micro
122              
123              
124             You can also look for information at:
125              
126             =over 4
127              
128             =item * RT: CPAN's request tracker (report bugs here)
129              
130             L
131              
132             =item * AnnoCPAN: Annotated CPAN documentation
133              
134             L
135              
136             =item * CPAN Ratings
137              
138             L
139              
140             =item * Search CPAN
141              
142             L
143              
144             =back
145              
146              
147             =head1 LICENSE AND COPYRIGHT
148              
149             Copyright 2013 ytnobody.
150              
151             This program is free software; you can redistribute it and/or modify it
152             under the terms of the the Artistic License (2.0). You may obtain a
153             copy of the full license at:
154              
155             L
156              
157             Any use, modification, and distribution of the Standard or Modified
158             Versions is governed by this Artistic License. By using, modifying or
159             distributing the Package, you accept this license. Do not use, modify,
160             or distribute the Package, if you do not accept this license.
161              
162             If your Modified Version has been derived from a Modified Version made
163             by someone other than you, you are nevertheless required to ensure that
164             your Modified Version complies with the requirements of this license.
165              
166             This license does not grant you the right to use any trademark, service
167             mark, tradename, or logo of the Copyright Holder.
168              
169             This license includes the non-exclusive, worldwide, free-of-charge
170             patent license to make, have made, use, offer to sell, sell, import and
171             otherwise transfer the Package with respect to any patent claims
172             licensable by the Copyright Holder that are necessarily infringed by the
173             Package. If you institute patent litigation (including a cross-claim or
174             counterclaim) against any party alleging that the Package constitutes
175             direct or contributory patent infringement, then this Artistic License
176             to you shall terminate on the date that such litigation is filed.
177              
178             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
179             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
180             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
181             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
182             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
183             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
184             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
185             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186              
187              
188             =cut
189              
190             1; # End of Context::Micro