| 1 | package Context::Micro; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | 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 | 3 | 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 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 73 | 1 | my ($class, %opts) = @_; | |||||||||||
| 74 | 1 | ▶ | my $config = $opts{config} || {}; | ||||||||||
|
Condition50%1/2
$opts{'config'} || {}
MC/DC0%0/1
$opts{'config'} || {}
$opts{'config'}
Truth table50%1/2
A
A= $opts{'config'}
| |||||||||||||
| 75 | 1 | return bless { config => $config, container => {} }, $class; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | =head2 config | ||||||||||||
| 79 | |||||||||||||
| 80 | Provide config accessor. | ||||||||||||
| 81 | |||||||||||||
| 82 | =cut | ||||||||||||
| 83 | |||||||||||||
| 84 | 4 | ▶ | sub config { | ||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 4 | my $self = shift; | |||||||||||
| 86 | 4 | 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 | 4 | ▶ | sub entry { | ||||||||||
|
Subroutine
entry: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 96 | 4 | my ($self, $key, $code) = @_; | |||||||||||
| 97 | 4 | ▶ | return $self->{container}{$key} ? $self->{container}{$key} : do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 2 | my $o = $code->(); | |||||||||||
| 99 | 2 | $self->{container}{$key} = $o; | |||||||||||
| 100 | 2 | $o; | |||||||||||
| 101 | }; | ||||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 AUTHOR | ||||||||||||
| 105 | |||||||||||||
| 106 | ytnobody, C<< <ytnobody aaatttt gmail> >> | ||||||||||||
| 107 | |||||||||||||
| 108 | =head1 BUGS | ||||||||||||
| 109 | |||||||||||||
| 110 | Please report any bugs or feature requests to C<bug-context-micro at rt.cpan.org>, or through | ||||||||||||
| 111 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Context-Micro>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Context-Micro> | ||||||||||||
| 131 | |||||||||||||
| 132 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 133 | |||||||||||||
| 134 | L<http://annocpan.org/dist/Context-Micro> | ||||||||||||
| 135 | |||||||||||||
| 136 | =item * CPAN Ratings | ||||||||||||
| 137 | |||||||||||||
| 138 | L<http://cpanratings.perl.org/d/Context-Micro> | ||||||||||||
| 139 | |||||||||||||
| 140 | =item * Search CPAN | ||||||||||||
| 141 | |||||||||||||
| 142 | L<http://search.cpan.org/dist/Context-Micro/> | ||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||
| 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 | 3 | 1; # End of Context::Micro | |||||||||||