Rssfilter/Rule.pm

filter: Statementstmt 87.3% 138 / 158 Branchbran 50.0% 28 / 56 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 30 / 35 Podpod n/a totaltotal 77.4% 199 / 257 CC 32 SCAR 13.9
CC
32
Cov
77%
coerce_module_name_to_sub
10.0
filter
8.2
__ANON__
6.0
CRAP 4.0
« Rssfilter/Feed.pm ↑ Summary Rssfilter/Feed/Storage.pm »
1 # ABSTRACT: match and filter RSS feeds
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7  
8 package App::Rssfilter::Rule;
9 {
10 17 $App::Rssfilter::Rule::VERSION = '0.07';
11 }
12  
13 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Module::Runtime qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Class::Inspector qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 with 'App::Rssfilter::Logger';
18  
19  
20  
21 17 has condition => (
22 is => 'ro',
23 required => 1,
24 );
25  
26  
27 6 ▶has _match => (
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 6 6
28 17 is => 'lazy',
29 init_arg => undef,
30 default => method { $self->coerce_attr( attr => $self->condition, type => 'match' ) },
31 17 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine match: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
if scalar @_ > 1 0 3 3
32 3  
33  
34 method match( $item ) {
35 return $self->_match->( $item );
36 1 ▶}
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
37 17  
38  
39 has condition_name => (
40 17 is => 'lazy',
41 default => method { $self->nice_name_for( $self->condition, 'matcher' ) },
42 );
43  
44  
45 has action => (
46 9 ▶ is => 'ro',
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 9 9
47 17 required => 1,
48 );
49  
50 0 ▶ 
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine filter: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
51 0 has _filter => (
52 is => 'lazy',
53 init_arg => undef,
54 default => method { $self->coerce_attr( attr => $self->action, type => 'filter' ) },
55 );
56  
57 0  
58 method filter( $item ) {
59 $self->logger->debugf(
60 'applying %s since %s matched %s',
61 0 ▶ $self->action_name,
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
62 17 $self->condition_name,
63 $item->find('guid, link, title')->first->text
64 );
65 17 ▶ return $self->_filter->( $item, $self->condition_name );
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine constrain: called CC=5 SCAR=17.1
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 3 3
unless ($Method::Signatures::mutc{"cache"}{"Mojo::DOM"} ||= "Method::Signatures"->_make_constraint("Mojo::DOM"))->check($Mojo_DOM) 0 3 3
if scalar @_ > 1 0 3 3
Condition66%2/3
$Method::Signatures::mutc{"cache"}{"Mojo::DOM"} ||= "Method::Signatures"->_make_constraint("Mojo::DOM")
l!l&&r!l&&!r
120
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Mojo::DOM"} ||= "Method::Signatures"->_make_constraint("Mojo::DOM")
$Method::Signatures::mutc{"cache"}{"Mojo::DOM"} "Method::Signatures"->_make_constraint("Mojo::DOM")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Mojo::DOM"}B= "Method::Signatures"->_make_constraint("Mojo::DOM")
ABresult
1X1
011
000
66 3 }
67 0 ▶ 
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
68 2  
69 has action_name => (
70 is => 'lazy',
71 default => method { $self->nice_name_for( $self->action, 'filter' ) },
72 );
73 17 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine nice_name_for: called CC=4 SCAR=14.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
unless scalar @_ > 1 0 1 1
if scalar @_ > 2 0 1 1
74 17 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
75 17 ▶method constrain( Mojo::DOM $Mojo_DOM ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 my $count = 0;
77 0 $Mojo_DOM->find( 'item' )->grep( sub { $self->match( shift ) } )->each( sub { $self->filter( shift ); ++$count; } );
78 1 return $count;
79 0 }
80  
81 # internal helper methods
82  
83 17 ▶method nice_name_for( $attr, $type ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BUILDARGS: called CC=2 SCAR=6.9
84 9 ▶ use feature 'switch';
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %args) 1 8 9
85 1 use experimental 'smartmatch';
86 1 given( ref $attr ) {
87 when( 'CODE' ) { return "unnamed RSS ${type}"; }
88 9 when( q{} ) { return $attr }
89 default { return $_ }
90 }
91 17 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BUILD: called CC=4 SCAR=14.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 9 9
if scalar @_ > 1 0 9 9
92  
93 9 method BUILDARGS( %args ) {
94 if ( 1 == keys %args ) {
95 @args{'condition','action'} = each %args;
96 17 ▶ delete $args{ $args{ condition } };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine coerce_attr: called CC=5 SCAR=16.3
Branch: 2 branches
BranchTrueFalseTotal
if keys %args 0 15 15
if scalar @_ > 4 0 15 15
97 15 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 0 15 15
98 17 ▶ return \%args;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 17 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
100 15  
101 15 method BUILD( $args ) {
102 7 # validate coercion of condition & action
103 $self->$_ for qw< _filter _match >;
104 8 }
105 6  
106 method coerce_attr( :$attr, :$type ) {
107 2 die "can't use an undefined value to $type RSS items" if not defined $attr;
108 2 ▶ use feature 'switch';
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $attr->can($type)) { } 0 2 2
109 0 ▶ use experimental 'smartmatch';
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 given( ref $attr ) {
111 when( 'CODE' ) {
112 return $attr;
113 2 }
114 when( q{} ) { # not a ref
115 return $self->coerce_module_name_to_sub( module_name => $attr, type => $type );
116 }
117 default {
118 if( my $method = $attr->can( $type ) ) {
119 17 ▶ return sub { $attr->$type( @_ ); }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine coerce_module_name_to_sub: called CC=9 SCAR=23.0
Branch: 2 branches
BranchTrueFalseTotal
if keys %args 0 6 6
if scalar @_ > 4 0 6 6
120 6 }
121 else
122 {
123 die "${_}::$type does not exist";
124 }
125 }
126 }
127 }
128  
129 method coerce_module_name_to_sub( :$module_name, :$type ) {
130 my ($namespace, $additional_args) =
131 6 ▶ $module_name =~ m/
Condition50%1/2
$additional_args // ""
l!l
06
MC/DC0%0/1
$additional_args // ""
$additional_args
Truth table50%1/2
A
A= $additional_args
Aresult
11
00
132 6 ▶ \A
Branch: 1 branch
BranchTrueFalseTotal
unless ($namespace =~ /::/msx) 0 6 6
133 0 ( [^\[]+ ) # namespace
134 (?: # followed by optional
135 \[
136 6 ( .* ) # additional arguments
137 \] # in square brackets
138 6 ▶ )? # optional, remember?
Branch: 1 branch
BranchTrueFalseTotal
unless ("Class::Inspector"->loaded($namespace)) 0 6 6
139 0 \z
140 /xms;
141 my @additional_args = split q{,}, $additional_args // q{};
142 if( $namespace !~ /::/xms ) {
143 6 $namespace = join q{::}, qw< App Rssfilter >, ucfirst( $type ), $namespace;
144 6 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace->can("new")) 2 4 6
145 2  
146 $namespace =~ s/\A :: //xms; # '::anything'->can() will die
147  
148 if( not Class::Inspector->loaded( $namespace ) ) {
149 6 ▶ Module::Runtime::require_module $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $invocant->can($type)) { } 2 4 6
150 2 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($invocant eq $namespace) { } 2 0 2
151 1 ▶ 
Subroutine __ANON__: called CC=1 SCAR=0.0
152 1 # create an object if we got an OO package
153 2 my $invocant = $namespace;
154 if( $namespace->can( 'new' ) ) {
155 $invocant = $namespace->new( @additional_args );
156 }
157 0 ▶ 
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 # return a wrapper
159 0 if( my $method = $invocant->can( $type ) ) {
160 if( $invocant eq $namespace ) {
161 return sub {
162 $method->( @_, @additional_args) ;
163 };
164 4 }
165 else
166 {
167 17 return sub {
168 $invocant->$method( @_ );
169 };
170 }
171 }
172 else
173 {
174 die "${namespace}::$type does not exist";
175 }
176 }
177 1;
178  
179 __END__
« Rssfilter/Feed.pm ↑ Summary Rssfilter/Feed/Storage.pm »