File Coverage

blib/lib/DBIx/VersionedSubs/Hash.pm
Criterion Covered Total %
statement 35 39 89.7
branch 4 6 66.6
condition 4 4 100.0
subroutine 10 11 90.9
pod 4 4 100.0
total 57 64 89.0


line stmt bran cond sub pod time code
1             package DBIx::VersionedSubs::Hash;
2 3     3   1746 use strict;
  3         4  
  3         118  
3 3     3   16 use base 'DBIx::VersionedSubs';
  3         4  
  3         1823  
4 3     3   20 use vars qw($VERSION);
  3         5  
  3         112  
5 3     3   17 use Carp qw(carp croak);
  3         5  
  3         1203  
6              
7             $VERSION = '0.07';
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
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.
46              
47             If you pass in a hashref to the C 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 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             sub new {
57 3     3 1 89 my ($package,$args) = @_;
58 3   100     24 my $code = delete $args->{ code } || {};
59 3         12 my $self = bless $args, $package;
60 3         76 $self->setup( %$args );
61 3         8 $self->{ code } = $code;
62 3         11 $self;
63             };
64              
65             sub create_sub {
66 2     2 1 7 my ($self,$name,$code) = @_;
67 2         5 my $package = ref $self;
68 2         23 my $ref = $self->eval_sub($package,$name,$code);
69 2 50       10 if ($ref) {
70 2 50       10 if ($name eq 'BEGIN') {
71 0         0 $ref->($self);
72             return undef
73 0         0 } else {
74 2         11 $self->{code}->{$name} = $ref;
75 2         12 $self->code_source->{$name} = $code;
76             }
77             };
78 2         21 $ref
79             };
80              
81             sub destroy_sub {
82 0     0 1 0 my ($self,$name) = @_;
83 0         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             sub dispatch {
97 3     3 1 17498 my $self= shift;
98 3         9 my $name= shift;
99 3   100 1   28 my $code= $self->{code}->{$name} || sub {croak "Undefined subroutine '$name' called."};
  1         265  
100 3         77 goto &$code;
101             };
102              
103             # Install our accessors
104             for (qw(code_source code_live code_history code_version verbose dsn)) {
105             my $name = $_;
106 3     3   21 no strict 'refs';
  3         4  
  3         306  
107             *{__PACKAGE__ . "::$name"} = sub {
108 46 100   46   309 @_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}
109             };
110             };
111              
112             1
113              
114             __END__