blib/lib/Catmandu/Fix/Condition.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 95.7% 45 / 47 CC 3 SCAR 3.2
CC
3
Cov
95%
__ANON__
2.1
import
2.0
BEGIN
1.0
CRAP 1.4
« blib/lib/Catmandu/Fix/Condition/SimpleAllTest.pm ↑ Summary blib/lib/Catmandu/Plugin/Datestamps.pm »
1 package Catmandu::Fix::Condition;
2  
3 28 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 28 our $VERSION = '1.0201_02';
6  
7 28 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Catmandu::Fix::reject;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 with 'Catmandu::Fix::Base';
12  
13 73 ▶has pass_fixes => (is => 'rw', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
14 73 ▶has fail_fixes => (is => 'rw', default => sub {[]});
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 sub import {
17 18 ▶ my $target = caller;
Subroutine import: called CC=2 SCAR=7.0
18 18 my ($fix, %opts) = @_;
19  
20 18 ▶ if (my $sym = $opts{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sym = $opts{"as"}) 1 17 18
21 my $sub = sub {
22 2 ▶ my $data = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
23 2 ▶ if ($opts{clone}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"clone"}) 0 2 2
24 0 $data = Clone::clone($data);
25 }
26 2 my $cond = $fix->new(@_);
27 2 $cond->fail_fixes([Catmandu::Fix::reject->new]);
28 2 !!$cond->fix($data);
29 1 };
30 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 *{"${target}::$sym"} = $sub;
32 }
33 }
34  
35 28 1;
36  
37 __END__
38  
39 =pod
40  
41 =head1 NAME
42  
43 Catmandu::Fix::Condition - Role for all Catmandu::Fix conditionals
44  
45 =head1 SYNOPSIS
46  
47 if <Catmandu::Fix::Condition instance>
48 <pass_fixes>
49 else
50 <fail_fixes>
51 end
52  
53 =head1 DESCRIPTION
54  
55 All L<Catmandu::Fix> conditions need to implement Catmandu::Fix::Condition.
56 This subclass of L<Catmandu::Fix::Base> provides a list of fixes that need to
57 be executed when a conditional matches (pass_fixes) and conditional that need
58 to be executed when a conditional fails (fail_fixes).
59  
60 Conditions can be used as inline fixes as well:
61  
62 use Catmandu::Fix::Condition::exists as => 'has_field';
63
64 my $item = { foo => { bar => 1 } };
65  
66 has_field($item, 'foo.bar'); # true
67 has_field($item, 'doz'); # false
68  
69 =head1 EXAMPLES
70  
71 Catmandu core comes with the following conditions:
72  
73 =over
74  
75 =item
76  
77 L<all_equal|Catmandu::Fix::Condition::all_equal>
78  
79 =item
80  
81 L<all_match|Catmandu::Fix::Condition::all_match>
82  
83 =item
84  
85 L<any_equal|Catmandu::Fix::Condition::any_equal>
86  
87 =item
88  
89 L<any_match|Catmandu::Fix::Condition::any_match>
90  
91 =item
92  
93 L<exists|Catmandu::Fix::Condition::exists>
94  
95 =item
96  
97 L<greater_than|Catmandu::Fix::Condition::greater_than>
98  
99 =item
100  
101 L<in|Catmandu::Fix::Condition::in>
102  
103 =item
104  
105 L<is_array|Catmandu::Fix::Condition::is_array>
106  
107 =item
108  
109 L<is_false|Catmandu::Fix::Condition::is_false>
110  
111 =item
112  
113 L<is_null|Catmandu::Fix::Condition::is_null>
114  
115 =item
116  
117 L<is_number|Catmandu::Fix::Condition::is_number>
118  
119 =item
120  
121 L<is_object|Catmandu::Fix::Condition::is_object>
122  
123 =item
124  
125 L<is_string|Catmandu::Fix::Condition::is_string>
126  
127 =item
128  
129 L<is_true|Catmandu::Fix::Condition::is_true>
130  
131 =item
132  
133 L<less_than|Catmandu::Fix::Condition::less_than>
134  
135 =back
136  
137 =cut
« blib/lib/Catmandu/Fix/Condition/SimpleAllTest.pm ↑ Summary blib/lib/Catmandu/Plugin/Datestamps.pm »