blib/lib/MooseX/Types/Moose/MutualCoercion.pm

filter: Statementstmt 97.7% 87 / 89 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 25 / 25 Podpod n/a totaltotal 96.6% 114 / 118 CC 4 SCAR 2.7
CC
4
Cov
96%
__ANON__
3.2
_ensure_class_loaded
2.0
BEGIN
1.0
CRAP 1.3
↑ Summary
1 package MooseX::Types::Moose::MutualCoercion;
2  
3  
4 # ****************************************************************
5 # perl dependency
6 # ****************************************************************
7  
8 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # ****************************************************************
12 # pragma(ta)
13 # ****************************************************************
14  
15 # Moose turns strict/warnings pragmata on,
16 # however, kwalitee scorer cannot detect such mechanism.
17 # (Perl::Critic can it, with equivalent_modules parameter)
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 # ****************************************************************
23 # MOP dependency(-ies)
24 # ****************************************************************
25  
26 1 ▶use MooseX::Types -declare => [qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
27 NumToInt
28 ScalarRefToStr ArrayRefToLines
29 StrToClassName
30 StrToScalarRef
31 StrToArrayRef LinesToArrayRef
32 HashRefToArrayRef HashKeysToArrayRef HashValuesToArrayRef
33 OddArrayRef EvenArrayRef
34 ArrayRefToHashRef ArrayRefToHashKeys
35 ArrayRefToRegexpRef
36 1 )];
37 1 ▶use MooseX::Types::Common::String qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
38 NonEmptyStr
39 1 );
40 1 ▶use MooseX::Types::Moose qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
41 Str
42 Num
43 Int
44 ClassName
45 RoleName
46 Ref
47 ScalarRef
48 ArrayRef
49 HashRef
50 RegexpRef
51 1 );
52  
53  
54 # ****************************************************************
55 # general dependency(-ies)
56 # ****************************************************************
57  
58 1 ▶use Class::Load qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
59 load_class
60 is_class_loaded
61 1 );
62  
63  
64 # ****************************************************************
65 # public class variable(s)
66 # ****************************************************************
67  
68 1 our $VERSION = "0.04";
69  
70  
71 # ****************************************************************
72 # namespace cleaner
73 # ****************************************************************
74  
75 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77  
78 # ****************************************************************
79 # subtype(s) and coercion(s)
80 # ****************************************************************
81  
82 # ================================================================
83 # to Int
84 # ================================================================
85  
86 1 subtype NumToInt,
87 as Int;
88  
89 coerce NumToInt,
90 from Num,
91 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 1 int $_;
93 1 };
94  
95 # ================================================================
96 # to Str
97 # ================================================================
98  
99 1 foreach my $type (
100 ScalarRefToStr, ArrayRefToLines,
101 ) {
102 2 subtype $type,
103 as Str;
104 }
105  
106 coerce ScalarRefToStr,
107 from ScalarRef[Str],
108 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 1 $$_;
110 1 };
111  
112 coerce ArrayRefToLines,
113 from ArrayRef[Str],
114 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 ( join $/, @$_ ) . $/;
116 1 };
117  
118 # ================================================================
119 # to ClassName
120 # ================================================================
121  
122 1 subtype StrToClassName,
123 as ClassName;
124  
125 coerce StrToClassName,
126 from NonEmptyStr,
127 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 1 _ensure_class_loaded($_);
129 1 };
130  
131 # ================================================================
132 # to ScalarRef
133 # ================================================================
134  
135 1 subtype StrToScalarRef,
136 as ScalarRef[Str];
137  
138 coerce StrToScalarRef,
139 from Str,
140 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 1 \do{ $_ };
142 1 };
143  
144 # ================================================================
145 # to ArrayRef
146 # ================================================================
147  
148 1 foreach my $type (
149 StrToArrayRef, LinesToArrayRef,
150 HashRefToArrayRef, HashKeysToArrayRef, HashValuesToArrayRef,
151 ) {
152 5 subtype $type,
153 as ArrayRef;
154 }
155  
156 coerce StrToArrayRef,
157 from Str,
158 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 [ $_ ];
160 1 };
161  
162 coerce LinesToArrayRef,
163 from Str,
164 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 ( my $new_line = $/ ) =~ s{(.)}{[$1]}xmsg;
166 1 [ split m{ (?<= $new_line ) }xms, $_ ];
167 1 };
168  
169 coerce HashRefToArrayRef,
170 from HashRef,
171 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 1 my $hashref = $_;
173 [
174 map {
175 1 $_, $hashref->{$_};
176 } sort keys %$hashref
177 ];
178 1 };
179  
180 coerce HashKeysToArrayRef,
181 from HashRef,
182 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 1 [ sort keys %$_ ];
184 1 };
185  
186 coerce HashValuesToArrayRef,
187 from HashRef,
188 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 1 my $hashref = $_;
190 [
191 map {
192 1 $hashref->{$_};
193 } sort keys %$hashref
194 ];
195 1 };
196  
197 subtype OddArrayRef,
198 as ArrayRef,
199 2 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
200 2 scalar @$_ % 2;
201 1 };
202  
203 subtype EvenArrayRef,
204 as ArrayRef,
205 3 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 3 ! scalar @$_ % 2;
207 1 };
208  
209 1 foreach my $type (OddArrayRef, EvenArrayRef) {
210 coerce $type,
211 from ArrayRef,
212 2 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
213 2 push @$_, undef;
214 2 $_;
215 2 };
216 }
217  
218 # ================================================================
219 # to HashRef
220 # ================================================================
221  
222 1 foreach my $type (
223 ArrayRefToHashRef, ArrayRefToHashKeys,
224 ) {
225 2 subtype $type,
226 as HashRef;
227 }
228  
229 coerce ArrayRefToHashRef,
230 from EvenArrayRef,
231 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
232 1 my %hash = @$_; # Note: "{ @$_ }" is invalid (need "return").
233 1 \%hash;
234 1 };
235  
236 coerce ArrayRefToHashKeys,
237 from ArrayRef,
238 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 1 my %hash;
240 1 @hash{@$_} = ();
241 1 \%hash;
242 1 };
243  
244 # ================================================================
245 # to RegexpRef
246 # ================================================================
247  
248 1 subtype ArrayRefToRegexpRef,
249 as RegexpRef;
250  
251 coerce ArrayRefToRegexpRef,
252 from ArrayRef,
253 1 ▶ via {
Subroutine __ANON__: called CC=3 SCAR=11.6
254 1 eval {
255 1 require Regexp::Assemble;
256 };
257 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
258 0 my $pattern_string = join '|', @$_;
259 0 qr{$pattern_string};
260 }
261 else {
262 1 my $regexp = Regexp::Assemble->new;
263 1 foreach my $pattern (@$_) {
264 3 $regexp->add($pattern);
265 }
266 1 $regexp->re;
267 }
268 1 };
269  
270  
271 # ****************************************************************
272 # subroutine(s)
273 # ****************************************************************
274  
275 1 ▶sub _ensure_class_loaded {
Subroutine _ensure_class_loaded: called CC=2 SCAR=7.1
276 1 my $class = shift;
277  
278 1 ▶ load_class($class)
Branch: 1 branch
BranchTrueFalseTotal
unless is_class_loaded($class) 1 0 1
279 unless is_class_loaded($class);
280  
281 1 return $class;
282 }
283  
284  
285 # ****************************************************************
286 # return true
287 # ****************************************************************
288  
289 1 1;
290 __END__
↑ Summary