LibTmux/Selection.pm

filter: Statementstmt 94.4% 68 / 72 Branchbran 71.8% 23 / 32 Conditioncond 61.9% 13 / 21 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 18 / 18 Podpod 100% 10 / 10 totaltotal 81.8% 135 / 165 CC 29 SCAR 16.0
CC
29
Cov
83%
count
9.5
_validate
8.1
_one
7.1
CRAP 4.9
« LibTmux/Platform/Linux.pm ↑ Summary LibTmux/Operation.pm »
1 package LibTmux::Selection;
2 4 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use LibTmux::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use LibTmux::Selection::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 138 ▶sub new ($class, @items) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
8 138 my $self = bless \@items, $class;
9 138 $self->_validate;
10 137 return $self;
11 }
12  
13 244 ▶sub _validate ($self) {
Subroutine _validate: called CC=8 SCAR=20.9
14 244 my $identity;
15 244 for my $i (0 .. $#$self) {
16 554 my $item = $self->[$i];
17 554 ▶ LibTmux::X::InvalidSelection->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[$i] and builtin::blessed($item) and $item->isa("LibTmux::Entity") and $item->kind eq $self->_kind 2 552 554
Condition77%7/9
exists $self->[$i] and builtin::blessed($item) and $item->isa("LibTmux::Entity") and $item->kind eq $self->_kind
!ll&&!rl&&r
20552
exists $self->[$i] and builtin::blessed($item) and $item->isa("LibTmux::Entity")
!ll&&!rl&&r
20552
exists $self->[$i] and builtin::blessed($item)
!ll&&!rl&&r
11552
MC/DC50%2/4
exists $self->[$i] and builtin::blessed($item) and $item->isa("LibTmux::Entity") and $item->kind eq $self->_kind
exists $self->[$i] builtin::blessed($item) $item->isa("LibTmux::Entity") $item->kind eq $self->_kind
Truth table60%3/5
A and B and C and D
A= exists $self->[$i]B= builtin::blessed($item)C= $item->isa("LibTmux::Entity")D= $item->kind eq $self->_kind
ABCDresult
0XXX0
10XX0
110X0
11100
11111
18 message => "member $i is not a " . $self->_kind . " entity")
19 unless exists($self->[$i])
20 && blessed($item)
21 && $item->isa('LibTmux::Entity')
22 && $item->kind eq $self->_kind;
23 552 my $owner = $item->server_id . "\0" . $item->generation;
24 552 ▶ $identity //= $owner;
Condition66%2/3
$identity //= $owner
l!l&&r!l&&!r
3282240
MC/DC0%0/2
$identity //= $owner
$identity $owner
Truth table66%2/3
A //= B
A= $identityB= $owner
ABresult
1X1
011
000
25 552 ▶ LibTmux::X::InvalidSelection->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $owner eq $identity 1 551 552
26 message => 'selection mixes server identities or generations')
27 unless $owner eq $identity;
28 }
29 241 return $self;
30 }
31  
32 59 ▶sub _filter ($self, $criteria) {
Subroutine _filter: called CC=4 SCAR=13.9
33 59 require LibTmux::Filter;
34 59 my $filter;
35 59 ▶ if (blessed($criteria) && $criteria->isa('LibTmux::Filter')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($criteria) and $criteria->isa('LibTmux::Filter')) { } 19 40 59
Condition66%2/3
builtin::blessed($criteria) and $criteria->isa("LibTmux::Filter")
!ll&&!rl&&r
40019
MC/DC50%1/2
builtin::blessed($criteria) and $criteria->isa("LibTmux::Filter")
builtin::blessed($criteria) $criteria->isa("LibTmux::Filter")
Truth table66%2/3
A and B
A= builtin::blessed($criteria)B= $criteria->isa("LibTmux::Filter")
ABresult
0X0
100
111
36 19 $filter = $criteria;
37 19 ▶ LibTmux::X::InvalidFilter->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $filter->entity eq $self->_kind 1 18 19
38 message => 'compiled filter has a different entity kind')
39 unless $filter->entity eq $self->_kind;
40 }
41 else {
42 40 $filter =
43 LibTmux::Filter->new(entity => $self->_kind, where => $criteria);
44 }
45 51 return $filter;
46 }
47  
48 59 ▶sub where ($self, $criteria) {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
49 59 my $filter = $self->_filter($criteria);
50 51 $self->_validate;
51 51 return ref($self)->new(CORE::grep { $filter->matches($_) } @$self);
52 }
53  
54 4 ▶sub grep ($self, $callback) {
Subroutine grep: called CC=4 SCAR=13.9
Pod documented
55 4 ▶ LibTmux::X::InvalidArgument->throw(message => 'grep requires one coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 4 4
56 unless ref($callback) eq 'CODE';
57 4 $self->_validate;
58 4 my @out;
59 4 for my $item (@$self) {
60 8 my $argument = $item;
61 8 local $_ = $item;
62 8 ▶ push @out, $item if scalar $callback->($argument);
Branch: 1 branch
BranchTrueFalseTotal
if scalar &$callback($argument) 6 1 7
63 }
64 3 return ref($self)->new(@out);
65 }
66 1 ▶sub to_array ($self) { $self->_validate; return [@$self] }
Subroutine to_array: called CC=1 SCAR=0.0
Pod documented
67  
68 49 ▶sub _matches ($self, @criteria) {
Subroutine _matches: called CC=3 SCAR=11.4
69 49 ▶ LibTmux::X::InvalidArgument->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @criteria > 1 0 49 49
70 message => 'expected at most one criterion')
71 if @criteria > 1;
72 49 ▶ my $filter = @criteria ? $self->_filter($criteria[0]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@criteria ? : 0 49 49
73 49 $self->_validate;
74 47 return $filter;
75 }
76  
77 36 ▶sub count ($self, @criteria) {
Subroutine count: called CC=4 SCAR=22.5
Pod documented
78 36 my $filter = $self->_matches(@criteria);
79 35 ▶ return scalar @$self unless $filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter 35 0 35
80 0 my $n = 0;
81 0 ▶ for my $item (@$self) { $n++ if $filter->matches($item) }
Branch: 1 branch
BranchTrueFalseTotal
if $filter->matches($item) 0 0 0
82 0 return $n;
83 }
84  
85 2 ▶sub exists ($self, @criteria) {
Subroutine exists: called CC=2 SCAR=7.6
Pod documented
86 2 ▶ defined($self->first(@criteria)) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->first(@criteria) ? : 0 1 1
87 }
88  
89 4 ▶sub first ($self, @criteria) {
Subroutine first: called CC=4 SCAR=15.2
Pod documented
90 4 my $filter = $self->_matches(@criteria);
91 3 for my $item (@$self) {
92 1 ▶ return $item if !$filter || $filter->matches($item);
Branch: 1 branch
BranchTrueFalseTotal
if not $filter or $filter->matches($item) 1 0 1
Condition33%1/3
not $filter or $filter->matches($item)
l!l&&r!l&&!r
100
MC/DC0%0/2
not $filter or $filter->matches($item)
not $filter $filter->matches($item)
Truth table33%1/3
not(A) or B
A= not $filterB= $filter->matches($item)
ABresult
1X1
011
000
93 }
94 2 return undef;
95 }
96  
97 9 ▶sub _one ($self, $optional, @criteria) {
Subroutine _one: called CC=7 SCAR=19.7
98 9 my $filter = $self->_matches(@criteria);
99 9 my @found;
100 9 for my $item (@$self) {
101 10 ▶ next if $filter && !$filter->matches($item);
Branch: 1 branch
BranchTrueFalseTotal
if $filter and not $filter->matches($item) 0 10 10
Condition33%1/3
$filter and not $filter->matches($item)
!ll&&!rl&&r
1000
MC/DC0%0/2
$filter and not $filter->matches($item)
$filter not $filter->matches($item)
Truth table33%1/3
A and not(B)
A= $filterB= not $filter->matches($item)
ABresult
0X0
100
111
102 10 push @found, $item;
103 LibTmux::X::MultipleMatches->throw(
104 entity => $self->_kind,
105 10 ▶ matched_ids => [map { $_->id } @found],
Branch: 1 branch
BranchTrueFalseTotal
if @found == 2 3 7 10
106 count => 'at least two',
107 message => 'expected at most one occurrence; matched at least two'
108 ) if @found == 2;
109 }
110 6 ▶ return $found[0] if @found;
Branch: 1 branch
BranchTrueFalseTotal
if @found 4 2 6
111 2 ▶ return undef if $optional;
Branch: 1 branch
BranchTrueFalseTotal
if $optional 1 1 2
112 1 LibTmux::X::NoMatch->throw(
113 entity => $self->_kind,
114 matched_ids => [],
115 count => 0,
116 message => 'expected exactly one occurrence; matched none'
117 );
118 }
119 7 ▶sub one ($self, @criteria) { $self->_one(false, @criteria) }
Subroutine one: called CC=1 SCAR=0.0
Pod documented
120 2 ▶sub one_or_undef ($self, @criteria) { $self->_one(true, @criteria) }
Subroutine one_or_undef: called CC=1 SCAR=0.0
Pod documented
121  
122 1 ▶sub iterator ($self) {
Subroutine iterator: called CC=1 SCAR=0.0
Pod documented
123 1 $self->_validate;
124 1 bless {items => [@$self], next => 0}, 'LibTmux::Selection::Iterator';
125 }
126  
127 =head1 NAME
128  
129 LibTmux::Selection - Native array selections with fixed query returns
130  
131 =head1 METHODS
132  
133 =head2 new
134  
135 C<new(@entities)> creates a concrete blessed array reference and validates every member. Construct an entity-specific selection such as L<LibTmux::PaneSelection>; an empty instance retains that schema.
136  
137 Members may come from separate captures of the same server generation; each
138 retains its own observation. Mixing servers or daemon generations throws
139 L<LibTmux::X/InvalidSelection>. Combining captures does not reconcile their times.
140  
141 =head2 where
142  
143 C<where($criteria_or_filter)> eagerly returns a new selection of the same concrete class in scalar, list, or void context. Complete validation precedes evaluation, including on empty input. See L<LibTmux::Filter/new>. Order and duplicate occurrences are preserved.
144  
145 =head2 grep
146  
147 C<grep($callback)> returns a new same-class selection. Invoke the callback in scalar context with exactly one entity argument and a localized C<$_>. Both bindings hold copies of the reference: assignment cannot replace a source slot. Restore C<$_> and propagate the original thrown value even on failure. Predicates must not edit the traversed sequence.
148  
149 =head2 count
150  
151 C<count($criteria?)> returns an integer. Empty input returns zero. Validate all members before evaluating the optional criterion; count every matching occurrence, including duplicates.
152  
153 =head2 exists
154  
155 C<exists($criteria?)> returns a native Boolean. Empty input returns false. After validating the entire membership, stop at the first matching entity. A reference itself remains true even when empty.
156  
157 =head2 first
158  
159 C<first($criteria?)> returns the first matching entity or undef. Input order determines first; after membership validation, later field reads may be skipped.
160  
161 =head2 one
162  
163 C<one($criteria?)> returns one entity. Zero throws L<NoMatch|LibTmux::X/NoMatch>; a second occurrence throws L<MultipleMatches|LibTmux::X/MultipleMatches>, reporting at least two and their IDs. Two occurrences of one identity are still ambiguous.
164  
165 =head2 one_or_undef
166  
167 C<one_or_undef($criteria?)> returns an entity or undef for absence. A second matching occurrence still throws L<MultipleMatches|LibTmux::X/MultipleMatches>.
168  
169 =head2 iterator
170  
171 Return a fresh local cursor with a copy of current membership. Its C<next> returns an entity or undef only at exhaustion. Later native array edits do not alter this traversal; the selection remains replayable.
172  
173 =head2 to_array
174  
175 Return a fresh, unblessed array reference containing the same entity references. Editing its membership cannot corrupt graph adjacency or this selection. Captured entity values remain read-only through public readers.
176  
177 =head1 EXECUTION
178  
179 All methods in this module operate on local captured data and perform no tmux
180 I/O. Ordering is deterministic; public query methods do not mutate membership.
181 Typed validation/evaluation errors propagate. See L<LibTmux::Server/snapshot>
182 for explicit acquisition and L<LibTmux::X> for machine-readable failures.
183  
184 =cut
« LibTmux/Platform/Linux.pm ↑ Summary LibTmux/Operation.pm »