blib/lib/DBIx/VersionedSubs/Hash.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 66.6% 4 / 6 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 86.4% 64 / 74 CC 6 SCAR 6.2
CC
6
Cov
88%
create_sub
3.2
dispatch
2.0
new
2.0
CRAP 1.9
« blib/lib/DBIx/VersionedSubs/AutoLoad.pm ↑ Summary
1 package DBIx::VersionedSubs::Hash;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use base 'DBIx::VersionedSubs';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 $VERSION = '0.09';
8  
9 =head1 NAME
10  
11 DBIx::VersionedSubs::Hash - store subroutines in a simple hash
12  
13 =head1 SYNOPSIS
14  
15 package My::App;
16 use strict;
17 use base 'DBIx::VersionedSubs::Hash';
18  
19 __PACKAGE__->{code} = {
20 say_hello => sub {print "Hello World"},
21 };
22  
23 package main;
24 use strict;
25  
26 my $app = My::App->new({code => {},dsn => $dsn );
27 while (my $request = Some::Server->get_request) {
28 $app->update_code(); # update code from the DB
29 $app->handle_request->($request);
30 }
31  
32 =head1 ABSTRACT
33  
34 This module overrides some methods in L<DBIx::VersionedSubs>
35 and replaces the normal namespace based code storage
36 with simple storage in a hash.
37 This is useful if you want multiple code versions
38 in a mod_perl environment for example.
39  
40 =cut
41  
42 =head2 C<< Package->new({ %ARGS }) >>
43  
44 Creates a new object and initializes it from the class
45 default values as inherited from L<DBIx::VersionedSubs>.
46  
47 If you pass in a hashref to the C<code> key, all subroutines will
48 be stored in it. You can also use this feature to pass in a package
49 hash (like C< %My::App:: >), then this module will be almost identical
50 in usage to L<DBIx::VersionedSubs> itself. The difference
51 between the two is that subroutine names with characters outside of C<\w>
52 will not create subroutines in other namespaces with this module.
53  
54 =cut
55  
56 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
57 3 my ($package,$args) = @_;
58 3 ▶ my $code = delete $args->{ code } || {};
Condition100%2/2
delete $args->{'code'} || {}
l!l
21
MC/DC100%1/1
delete $args->{'code'} || {}
delete $args->{'code'}
Truth table100%2/2
A
A= delete $args->{'code'}
Aresult
11
00
59 3 my $self = bless $args, $package;
60 3 $self->setup( %$args );
61 3 $self->{ code } = $code;
62 3 $self;
63 };
64  
65 2 ▶sub create_sub {
Subroutine create_sub: called CC=3 SCAR=11.7
Pod documented
66 2 my ($self,$name,$code) = @_;
67 2 my $package = ref $self;
68 2 my $ref = $self->eval_sub($package,$name,$code);
69 2 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 2 0 2
70 2 ▶ if ($name eq 'BEGIN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'BEGIN') { } 0 2 2
71 0 $ref->($self);
72 return undef
73 0 } else {
74 2 $self->{code}->{$name} = $ref;
75 2 $self->code_source->{$name} = $code;
76 }
77 };
78 2 $ref
79 };
80  
81 0 ▶sub destroy_sub {
Subroutine destroy_sub: not called CC=1 SCAR=6.9
Pod documented
82 0 my ($self,$name) = @_;
83 0 delete $self->{code}->{$name};
84 };
85  
86 =head2 C<< $app->dispatch( FUNCTION, ARGS ) >>
87  
88 This is a shorthand method for
89  
90 return $self->{code}->{$function}->(@ARGS);
91  
92 except with error checking
93  
94 =cut
95  
96 3 ▶sub dispatch {
Subroutine dispatch: called CC=2 SCAR=7.0
Pod documented
97 3 my $self= shift;
98 3 my $name= shift;
99 3 ▶ my $code= $self->{code}->{$name} || sub {croak "Undefined subroutine '$name' called."};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$self->{'code'}{$name} || sub { croak("Undefined subroutine '${name}' called."); }
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->{'code'}{$name} || sub { croak("Undefined subroutine '${name}' called."); }
$self->{'code'}{$name} sub { croak("Undefined subroutine '${name}' called."); }
Truth table66%2/3
A || B
A= $self->{'code'}{$name}B= sub { croak("Undefined subroutine '${name}' called."); }
ABresult
1X1
011
000
100 3 goto &$code;
101 };
102  
103 # Install our accessors
104 3 for (qw(code_source code_live code_history code_version verbose dsn)) {
105 18 my $name = $_;
106 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 18 ▶ *{__PACKAGE__ . "::$name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
108 46 ▶ @_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 17 29 46
109 18 };
110 };
111  
112 3 1
113  
114 __END__
« blib/lib/DBIx/VersionedSubs/AutoLoad.pm ↑ Summary