File Coverage

blib/lib/Devel/PerlySense/Bookmark/Definition.pm
Criterion Covered Total %
statement 61 61 100.0
branch 12 12 100.0
condition n/a
subroutine 13 13 100.0
pod 3 3 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Devel::PerlySense::Bookmark::Definition - A Bookmark definition
4              
5             =head1 DESCRIPTION
6              
7              
8             =cut
9              
10              
11              
12              
13              
14 68     68   223 use strict;
  68         89  
  68         1544  
15 68     68   194 use warnings;
  68         65  
  68         1268  
16 68     68   200 use utf8;
  68         81  
  68         274  
17              
18             package Devel::PerlySense::Bookmark::Definition;
19             $Devel::PerlySense::Bookmark::Definition::VERSION = '0.0218';
20              
21              
22              
23              
24 68     68   2882 use Spiffy -Base;
  68         83  
  68         337  
25 68     68   41972 use Carp;
  68     68   82  
  68     68   984  
  68         185  
  68         75  
  68         2164  
  68         209  
  68         81  
  68         2681  
26 68     68   221 use Data::Dumper;
  68         87  
  68         2013  
27              
28 68     68   235 use Devel::PerlySense;
  68         89  
  68         373  
29 68     68   27722 use Devel::PerlySense::Bookmark::Match;
  68         99  
  68         343  
30              
31              
32              
33              
34              
35             =head1 PROPERTIES
36              
37             =head2 moniker
38              
39             The moniker of the Bookmark.
40              
41             Default: ""
42              
43             =cut
44             field "moniker" => "";
45              
46              
47              
48              
49              
50             =head2 raRexText
51              
52             Regexp texts to be evaled as qr definitions.
53              
54             Bookmarks are matched in this order.
55              
56             Default: []
57              
58             =cut
59             field "raRexText" => [];
60              
61              
62              
63              
64              
65             =head2 rhQrRex
66              
67             Hash ref with (keys: regexp texts; values: qr objects).
68              
69             Default: {}
70              
71             =cut
72             field "rhQrRex" => {};
73              
74              
75              
76              
77              
78             =head1 METHODS
79              
80             =head2 newFromConfig(moniker, rex)
81              
82             Create new PerlySense::Bookmark::Definition object. Give it $moniker and
83             parse the regex definitions in $ref (either a scalar or an array ref
84             with scalars).
85              
86             Die on errors, like if the rex definitions aren't valid Perl, or if
87             they don't result in a qr object.
88              
89             =cut
90 15     15 1 3504 sub newFromConfig {
91 15         49 my ($moniker, $rex) = Devel::PerlySense::Util::aNamedArg(["moniker", "rex"], @_);
92              
93 15         33 $self = bless {}, $self; #Create the object. It looks weird because of Spiffy
94 15 100       228 $self->moniker($moniker)
95             or die("Bad Bookmark definition: No 'moniker' specified' in " . Dumper({@_}));
96              
97 14 100       136 my $raRex = ref $rex ? $rex : [ $rex ];
98              
99 14         22 for my $rex (@$raRex) {
100 18         34 push(@{$self->raRexText}, $rex);
  18         244  
101 18         110 my $qr = $self->parseRex($rex);
102 16         217 $self->rhQrRex->{$rex} = $qr;
103             }
104              
105 12         109 return($self);
106             }
107              
108              
109              
110              
111              
112             =head2 parseRex($rex)
113              
114             Perl eval the $rex string to create a qr// object and return it.
115              
116             Die on eval errors, or if the result isn't a qr.
117              
118             =cut
119 18     18 1 19 sub parseRex {
120 18         22 my ($rex) = @_;
121              
122 18         958 my $qr = eval $rex; ## no critic
123 18 100       63 $@ and die("Perl syntax error encountered when parsing Bookmark regex ($rex):\n$@");
124 17 100       47 ref $qr eq "Regexp" or die("Bookmark regex definition ($rex) doesn't result in a regex (a qr// object)\n");
125 16         25 return $qr;
126             }
127              
128              
129              
130              
131              
132             =head2 aMatch(file, source)
133              
134             Return a Bookmark::Match object for each time this bookmark matches a
135             line in source.
136              
137             =cut
138 8     8 1 16 sub aMatch {
139 8         22 my ($file, $source) = Devel::PerlySense::Util::aNamedArg(["file", "source"], @_);
140              
141 8         11 my @aMatch;
142 8         12 my $row = 0;
143 8         470 for my $line (split(/\r?\n/, $source)) {
144 1158         715 $row++;
145              
146 1158         658 for my $rexText (@{$self->raRexText}) {
  1158         13076  
147 1319         17423 my $qr = $self->rhQrRex->{$rexText};
148              
149 1319 100       6002 if($line =~ $qr) {
150 14 100       38 my $text = defined($1) ? $1 : $line;
151            
152 14         48 push(
153             @aMatch,
154             Devel::PerlySense::Bookmark::Match->new(
155             oDefinition => $self,
156             file => $file,
157             line => $line,
158             text => $text,
159             row => $row,
160             ),
161             );
162 14         25 last;
163             }
164             }
165             }
166              
167 8         107 return(@aMatch);
168             }
169              
170              
171              
172              
173              
174             1;
175              
176              
177              
178              
179              
180             __END__
181              
182             =encoding utf8
183              
184             =head1 AUTHOR
185              
186             Johan Lindstrom, C<< <johanl@cpan.org> >>
187              
188             =head1 BUGS
189              
190             Please report any bugs or feature requests to
191             C<bug-devel-perlysense@rt.cpan.org>, or through the web interface at
192             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-PerlySense>.
193             I will be notified, and then you'll automatically be notified of progress on
194             your bug as I make changes.
195              
196             =head1 ACKNOWLEDGEMENTS
197              
198             =head1 COPYRIGHT & LICENSE
199              
200             Copyright 2005 Johan Lindstrom, All Rights Reserved.
201              
202             This program is free software; you can redistribute it and/or modify it
203             under the same terms as Perl itself.
204              
205             =cut