File Coverage

lib/Drogo/Dispatcher/Attributes.pm
Criterion Covered Total %
statement 44 49 89.8
branch 19 28 67.8
condition n/a
subroutine 7 8 87.5
pod 2 2 100.0
total 72 87 82.7


line stmt bran cond sub pod time code
1 1     1   9676 use attributes;
  1         2865  
  1         8  
2 1     1   74 use warnings;
  1         2  
  1         32  
3 1     1   4 use strict;
  1         2  
  1         528  
4              
5             package Drogo::Dispatcher::Attributes;
6              
7             =head1 NAME
8              
9             Drogo::Dispatcher::Attributes
10              
11             Attributes:
12             Index, Page, Action, ActionMatch, ActionRegex
13              
14             =head1 Synopsis
15              
16             Code attributes.
17              
18             =head1 Methods
19              
20             =cut
21              
22             our ( %dispatch_flags, %cached_flags );
23              
24             sub MODIFY_CODE_ATTRIBUTES
25             {
26 7     7   2307 my ($pack, $ref, @attr) = @_;
27              
28 7         14 for my $attr ( @attr ) {
29              
30             # ensure a $dispatch_flags{$pack} hashref, always
31 7 100       28 $dispatch_flags{$pack} = { }
32             unless exists $dispatch_flags{$pack};
33              
34 7 100       27 if ($attr eq 'Index')
    100          
    100          
    50          
    0          
35             {
36 2         10 $dispatch_flags{$pack}{$ref} = 'index';
37             }
38             elsif ($attr eq 'Action')
39             {
40 2         9 $dispatch_flags{$pack}{$ref} = 'action';
41             }
42             elsif ($attr eq 'ActionMatch')
43             {
44 2         9 $dispatch_flags{$pack}{$ref} = 'action_match';
45             }
46             elsif ($attr =~ /^ActionRegex/)
47             {
48 1 50       7 if ($attr =~ /^ActionRegex\(['"](.*)['"]\)$/)
49             {
50 1         5 my $regex = $1;
51              
52 1         2 eval { qr/$regex/ };
  1         18  
53 1 50       5 die "Invalid ActionRegex in $pack (ref) $attr: $@\n"
54             if $@;
55              
56 1         1446 $dispatch_flags{$pack}{$ref} = "action_regex-${regex}";
57             }
58             else
59             {
60 0         0 die "Invalid ActionRegex in $pack (ref): $attr\n";
61             }
62             }
63             elsif ($attr =~ /^Path/)
64             {
65 0         0 my $desc;
66 0 0       0 $desc = $1 if $attr =~ /\(['"](.*?)['"]\)$/;
67              
68 0         0 $dispatch_flags{$pack}{$ref} = "path-${desc}";
69             }
70             }
71              
72 7         25 return ();
73             }
74              
75 0     0   0 sub FETCH_CODE_ATTRIBUTES { $dispatch_flags{shift}{shift} }
76              
77             =head2 get_dispatch_flags
78              
79             Returns all autoflags specific to a package with inheritance.
80              
81             =cut
82              
83             sub get_dispatch_flags
84             {
85 13     13 1 17 my $self = shift;
86 13 50       20 my $class = ref $self ? ref $self : $self;
87              
88 13 100       45 $cached_flags{$class} = $class->get_package_dispatch_flags
89             unless exists $cached_flags{$class};
90              
91 13         35 return $cached_flags{$class};
92             }
93              
94             =head2 get_package_dispatch_flags
95              
96             Get autoflags, only specific to the called package.
97              
98             =cut
99              
100             sub get_package_dispatch_flags
101             {
102 2     2 1 3 my $self = shift;
103 2 50       5 my $class = ref $self ? ref $self : $self;
104 2         4 my @code_refs = keys %{$dispatch_flags{$class}};
  2         9  
105              
106 2         3 my %flag_methods;
107             {
108 1     1   235 no strict 'refs';
  1         4  
  1         205  
  2         3  
109 2         2 for my $key (keys %{"${class}::"})
  2         11  
110             {
111 27         32 my $code_ref = "${class}::${key}";
112              
113 27 100       84 if (defined &$code_ref)
114             {
115 10         14 my $code = \&$code_ref;
116              
117 37         89 $flag_methods{"$key"} = $dispatch_flags{"$class"}{"$code"}
118 10 100       12 if grep { "$code" eq "$_" } @code_refs;
119             }
120             }
121             }
122              
123 2         14 return \%flag_methods;
124             }
125              
126             =head1 AUTHORS
127              
128             Bizowie
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             Copyright (C) 2013 Bizowie
133              
134             This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
135              
136             =cut
137              
138             1;