Mode/Set.pm

filter: Statementstmt 100% 78 / 78 Branchbran 72.2% 13 / 18 Conditioncond 60.0% 6 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 24 / 24 Podpod 88.8% 8 / 9 totaltotal 89.6% 130 / 145 CC 14 SCAR 6.7
CC
14
Cov
92%
BUILD
3.2
clone_from_params
3.1
next
3.0
CRAP 2.0
« Toolkit/TS6.pm ↑ Summary Toolkit/Role/CaseMap.pm »
1 package IRC::Mode::Set;
2 1 $IRC::Mode::Set::VERSION = '0.092002';
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use IRC::Mode::Single;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IRC::Toolkit::Modes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶my $str_to_arr = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
15 6 ▶ ref $_[0] eq 'ARRAY' ? $_[0] : [ split //, $_[0] ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 4 2 6
16 1 };
17  
18 has param_always => (
19 lazy => 1,
20 is => 'ro',
21 coerce => $str_to_arr,
22 2 ▶ builder => sub { [ qw/ b k o h v / ] },
Subroutine _build_param_always: called CC=1 SCAR=0.0
23 1 );
24  
25 has param_on_set => (
26 lazy => 1,
27 is => 'ro',
28 coerce => $str_to_arr,
29 2 ▶ builder => sub { [ 'l' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 );
31  
32 has mode_array => (
33 lazy => 1,
34 is => 'ro',
35 predicate => 'has_mode_array',
36 3 ▶ builder => sub {
Subroutine _build_mode_array: called CC=2 SCAR=7.2
37 3 my ($self) = @_;
38 3 ▶ mode_to_array( $self->mode_string,
Branch: 1 branch
BranchTrueFalseTotal
$self->has_params ? : 0 3 3
39 param_always => $self->param_always,
40 param_set => $self->param_on_set,
41 (
42 $self->has_params ? (params => $self->params)
43 : ()
44 ),
45 );
46 },
47 1 );
48  
49 has params => (
50 lazy => 1,
51 is => 'ro',
52 predicate => 'has_params',
53 1 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
54 1 ▶ ref $_[0] eq 'ARRAY' ? $_[0] : [ split ' ', $_[0] ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 0 1
55 },
56 1 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
57 1 ▶ [ map {; defined $_->[2] ? $_->[2] : () } @{ $_[0]->mode_array } ]
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[2] ? : 2 2 4
58 },
59 1 );
60  
61  
62 1 ▶sub as_string { $_[0]->mode_string }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
63  
64 has mode_string => (
65 lazy => 1,
66 is => 'ro',
67 predicate => 'has_mode_string',
68 5 ▶ builder => sub { array_to_mode $_[0]->mode_array },
Subroutine _build_mode_string: called CC=1 SCAR=0.0
69 1 );
70  
71  
72 1 ▶sub split_mode_set {
Subroutine split_mode_set: called CC=3 SCAR=11.0
Pod documented
73 1 my ($self, $max) = @_;
74 1 ▶ $max ||= 4;
Condition50%1/2
$max ||= 4
l!l
10
MC/DC0%0/1
$max ||= 4
$max
Truth table50%1/2
A
A= $max
Aresult
11
00
75  
76 1 my @new;
77 1 my @queue = @{ $self->mode_array };
78 1 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 2 1 3
79 2 my @spl = splice @queue, 0, $max;
80 2 push @new, blessed($self)->new(
81 mode_array => [ @spl ],
82 )
83 }
84  
85 @new
86 1 }
87  
88  
89 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
90 1 my ($self) = @_;
91 1 (blessed $self)->new(mode_array => dclone($self->mode_array))
92 }
93  
94 1 ▶sub clone_from_mode {
Subroutine clone_from_mode: called CC=2 SCAR=7.0
Pod documented
95 1 my ($self, $mode) = @_;
96 1 my @match = grep {; $_->[1] eq $mode } @{ $self->mode_array };
97 1 ▶ return unless @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 0 1 1
98 1 blessed($self)->new(
99 mode_array => dclone(\@match),
100 )
101 }
102  
103 1 ▶sub clone_from_params {
Subroutine clone_from_params: called CC=3 SCAR=11.2
Pod documented
104 1 my ($self, $regex) = @_;
105 my @match = grep {;
106 7 ▶ defined($_->[2]) and $_->[2] =~ m/$regex/
Condition66%2/3
defined $_->[2] && $_->[2] =~ /$regex/
!ll&&!rl&&r
061
MC/DC50%1/2
defined $_->[2] && $_->[2] =~ /$regex/
defined $_->[2] $_->[2] =~ /$regex/
Truth table66%2/3
A && B
A= defined $_->[2]B= $_->[2] =~ /$regex/
ABresult
0X0
100
111
107 1 } @{ $self->mode_array };
108 1 ▶ return unless @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 0 1 1
109 1 blessed($self)->new(
110 mode_array => dclone(\@match),
111 )
112 }
113  
114  
115 1 ▶sub modes_as_objects {
Subroutine modes_as_objects: called CC=1 SCAR=0.0
Pod documented
116 1 map {; IRC::Mode::Single->new(@$_) } @{ $_[0]->mode_array };
117 }
118  
119  
120 has _iter => (
121 lazy => 1,
122 is => 'rw',
123 1 ▶ builder => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 );
125  
126 4 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
127 4 my ($self, %param) = @_;
128 4 my $cur = $self->_iter;
129 4 $self->_iter($cur+1);
130 4 ▶ my $item = $self->mode_array->[$cur] || return;
Condition50%1/2
$self->mode_array->[$cur] || (return)
l!l
40
MC/DC0%0/1
$self->mode_array->[$cur] || (return)
$self->mode_array->[$cur]
Truth table50%1/2
A
A= $self->mode_array->[$cur]
Aresult
11
00
131 $param{as_object} ?
132 4 ▶ IRC::Mode::Single->new(@$item)
Branch: 1 branch
BranchTrueFalseTotal
$param{'as_object'} ? : 1 3 4
133 : $item
134 }
135  
136 2 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
137 2 my ($self) = @_;
138 2 $self->_iter(0);
139 2 $self
140 }
141  
142 =pod
143  
144 =for Pod::Coverage BUILD has_\w+
145  
146 =cut
147  
148 9 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
149 9 my ($self) = @_;
150 9 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_mode_array or $self->has_mode_string 0 9 9
Condition66%2/3
$self->has_mode_array or $self->has_mode_string
l!l&&r!l&&!r
630
MC/DC0%0/2
$self->has_mode_array or $self->has_mode_string
$self->has_mode_array $self->has_mode_string
Truth table66%2/3
A or B
A= $self->has_mode_arrayB= $self->has_mode_string
ABresult
1X1
011
000
151 "Expected to be constructed with either a mode_string or mode_array"
152 unless $self->has_mode_array or $self->has_mode_string;
153 }
154
155  
156 1 1;
157  
158 =pod
159  
160 =head1 NAME
161  
162 IRC::Mode::Set - A set of parsed IRC mode changes
163  
164 =head1 SYNOPSIS
165  
166 ## Construct a new set of changes from a mode string:
167 my $from_string = IRC::Mode::Set->new(
168 mode_string => '+o-o+v avenj Joah Gilded',
169  
170 ## Optionally specify modes that take parameters (always or when set)
171 ## Defaults:
172 param_always => 'bkohv',
173 param_on_set => 'l',
174 );
175  
176 my $mode_array = $from_string->mode_array;
177 ## $mode_array looks like:
178 ## [
179 ## [ '+', 'o', 'avenj' ],
180 ## [ '-', 'o', 'Joah' ],
181 ## [ '+', 'v', 'Gilded' ],
182 ## ]
183  
184 ## Iterate over each mode change:
185 while (my $change = $from_string->next) {
186 ## $change is set to each individual array as seen above, in turn
187 }
188  
189 ## Reset ->next() iterator to top:
190 $from_string->reset;
191  
192 ## Like above loop, but get IRC::Mode::Single objects:
193 while (my $this_mode = $from_string->next(as_object => 1) ) {
194 ## $this_mode is an IRC::Mode::Single
195 }
196  
197 ## Construct a new set of changes from an ARRAY
198 ## (such as produced by IRC::Toolkit::Modes):
199 my $from_array = IRC::Mode::Set->new(
200 mode_array => $mode_array,
201 );
202  
203 ## Get an IRC-appropriate string back out:
204 my $str_from_array = $from_array->mode_string;
205  
206 ## Split a Set into multiple Sets with a max of $count items each
207 ## (defaults to 4 changes per set if none specified)
208 my @sets = $from_array->split_mode_set( 3 );
209
210 ## Create a new Set containing matching items from this Set:
211 my $modes_match = $from_array->clone_from_mode('v');
212 my $args_match = $from_array->clone_from_params('Joah');
213  
214 =head1 DESCRIPTION
215  
216 These objects provide a simple parser interface to IRC mode changes.
217  
218 An understanding of the C<CHANMODES=> directive in C<ISUPPORT> will help
219 immensely -- see L<http://www.irc.org/tech_docs/005.html>
220  
221 =head2 new
222  
223 my $set = IRC::Mode::Set->new(
224 mode_string => '+o-o avenj Joah',
225 );
226  
227 ## Or with IRC::Toolkit::Modes ->
228 my $mode_array = mode_to_array($string);
229 my $set = IRC::Mode::Set->new(
230 mode_array => $mode_array,
231 );
232  
233 Create a new IRC::Mode::Set from either a string or an ARRAY produced by
234 L<IRC::Toolkit::Modes>.
235  
236 B<param_always> can be specified (as a string or an ARRAY of modes) to
237 indicate modes that are expected to always take a parameter. Defaults to
238 'bkohv'
239  
240 B<param_on_set> can be specified (as a string or an ARRAY of modes) to
241 indicate modes that are expected to take a parameter only when set. Defaults
242 to 'l'
243  
244 See L<IRC::Toolkit::ISupport> for an easy way to retrieve these values from a
245 parsed ISUPPORT (005) numeric.
246  
247 =head2 clone
248  
249 Clone the instanced Mode::Set.
250  
251 =head2 clone_from_mode
252  
253 Takes a single mode character.
254  
255 Returns a new Mode::Set composed of only modes in the existing set containing
256 the specified mode character.
257  
258 =head2 clone_from_params
259  
260 Takes a pattern or regexp object.
261  
262 Returns a new Mode::Set composed of only modes in the existing set with
263 parameters matching the pattern.
264  
265 =head2 mode_array
266  
267 Returns the array-of-arrays containing each change in the Set.
268  
269 This is a data structure in the form of:
270  
271 [
272 [ $mode_flag, $mode_char, $param ],
273 ...
274 ]
275  
276 Also see L<IRC::Toolkit::Modes/mode_to_array>
277  
278 Predicate: B<has_mode_array>
279  
280 =head2 modes_as_objects
281  
282 Returns a list of L<IRC::Mode::Single> objects constructed from our current
283 L</mode_array>.
284  
285 =head2 mode_string
286  
287 Returns the string representing the mode change.
288  
289 Predicate: B<has_mode_string>
290  
291 =head2 as_string
292  
293 B<as_string> is an alias for B<mode_string> to retain compatibility with
294 L<IRC::Mode::Single>.
295  
296 =head2 params
297  
298 Retrieve only the parameters to the mode change (as an ARRAY)
299  
300 Predicate: B<has_params>
301  
302 =head2 next
303  
304 Iterates the array-of-arrays composing the Set.
305  
306 Returns the next ARRAY in the set (or empty list if none left).
307  
308 If C<< as_object => 1 >> is specified, an L<IRC::Mode::Single> object is
309 returned.
310  
311 Reset to top by calling L</reset>.
312  
313 =head2 reset
314  
315 Resets the L</next> iterator.
316  
317 =head2 split_mode_set
318  
319 Given an integer parameter C<$x>, splits a Set into smaller Sets containing at
320 most C<$x> single mode changes.
321  
322 Defaults to 4, which is a common C<ISUPPORT MODES=> setting.
323  
324 =head1 AUTHOR
325  
326 Jon Portnoy <avenj@cobaltirc.org>
327  
328 =cut
« Toolkit/TS6.pm ↑ Summary Toolkit/Role/CaseMap.pm »