File Coverage

blib/lib/Scope/Session.pm
Criterion Covered Total %
statement 50 61 81.9
branch 11 18 61.1
condition n/a
subroutine 15 21 71.4
pod 7 7 100.0
total 83 107 77.5


line stmt bran cond sub pod time code
1             package Scope::Session;
2 3     3   130058 use strict;
  3         10  
  3         202  
3 3     3   17 use warnings;
  3         6  
  3         91  
4 3     3   4755 use Error qw/:try/;
  3         25448  
  3         21  
5 3     3   952 use Carp qw/croak/;
  3         6  
  3         3163  
6             {
7             package Scope::Session::Notes;
8             our $DATA_STORE = {};
9 0     0   0 sub get_instance { bless {} => shift; }
10 0     0   0 sub set { $DATA_STORE->{ $_[1] } = $_[2];}
11 0     0   0 sub get { $DATA_STORE->{ $_[1] };}
12 0     0   0 sub exists { exists $DATA_STORE->{ $_[1] };}
13             }
14             our $VERSION = q{0.02};
15             our $_INSTANCE = undef;
16             our $_IS_IN_SESSION = 0;
17              
18             sub get_instance {
19 6     6 1 295 my ($class) = @_;
20 6 100       14 unless( $_INSTANCE ){
21 5         12 $_INSTANCE = $class->_new_instance;
22             }
23 6         14 return $_INSTANCE;
24             }
25              
26             sub _new_instance{
27 5     5   7 my ($class) = @_;
28 5         14 return bless {},$class;
29             }
30             sub _on_error {
31 3     3   5 my ( $self, $error ) = @_;
32 4         8 my @handlers = map { $_->{handler} }
  4         14  
33 3         4 grep { $error->isa( $_->{target} ) } @{ $self->_handlers };
  3         9  
34              
35 3 100       7 if ( scalar @handlers > 0 ) {
36 2         6 $_->($error) for @handlers;
37             }
38             else {
39 1         11 croak $error;
40             }
41             }
42              
43             sub _handlers {
44 7     7   6 my $self = shift;
45 7 100       16 $self->{_handlers} = [] unless $self->{_handlers};
46 7         24 return $self->{_handlers};
47             }
48              
49             sub add_error_handler{
50 4     4 1 22 my ($self,$target,$handler) = @_;
51 4         4 push @{ $self->_handlers },{
  4         7  
52             target => '' . $target,
53             handler => $handler,
54             };
55             }
56              
57             sub is_started {
58 0 0   0 1 0 return ($_IS_IN_SESSION) ? 1 : 0;
59             }
60              
61             sub notes {
62 0     0 1 0 my $class = shift;
63 0         0 my $length = scalar(@_);
64 0 0       0 if ( $length == 0 ) {
    0          
65 0         0 return Scope::Session::Notes->get_instance;
66             }
67             elsif ( $length == 1 ) {
68 0         0 return Scope::Session::Notes->get(shift);
69             }
70             else {
71 0         0 return Scope::Session::Notes->set(@_);
72             }
73             }
74              
75             sub _option {
76 2 50   2   6 my $self = (ref $_[0]) ? shift : shift->get_instance;
77 2 100       9 $self->{_option} = {} unless $self->{_option};
78 2         8 return $self->{_option};
79             }
80              
81             sub set_option {
82 1     1 1 3 my ( $self, $key, $val ) = @_;
83 1         4 $self->_option->{$key} = $val;
84             }
85              
86             sub get_option {
87 1     1 1 3 my ( $self, $key ) = @_;
88 1         2 return $self->_option->{$key};
89             }
90              
91              
92             sub start(&){
93 6     6 1 2112 my $code = shift;
94 6         11 my $class = __PACKAGE__;
95              
96 6 100       31 croak(q{scope session is alreay started})
97             if( $_IS_IN_SESSION );
98              
99 5         7 local $Scope::Session::Notes::DATA_STORE = {};
100 5         7 local $_INSTANCE = undef;
101 5         7 local $_IS_IN_SESSION = 1;
102 5         16 my $instance = $class->get_instance;
103             try{
104 5     5   109 $code->($instance);
105             }
106             catch Error::Simple with{
107 3     3   914 my $error = shift;
108 3         8 $instance->_on_error( $error );
109 5         29 };
110              
111             }
112              
113             1;
114              
115             __END__