Rssfilter/Rule.pm

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