File Coverage

lib/List/Objects/WithUtils/Role/Array/WithJunctions.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
total 18 18 100.0


line stmt bran cond sub code
1         package List::Objects::WithUtils::Role::Array::WithJunctions;
2 93     93 use strictures 1;
  93        
  93        
3          
4 93     93 use List::Objects::WithUtils::Array::Junction ();
  93        
  93        
5          
6 93     93 use Role::Tiny;
  93        
  93        
7          
8         sub any_items {
9 68     68   List::Objects::WithUtils::Array::Junction::Any->new( @{ $_[0] } )
  68        
10         }
11          
12         sub all_items {
13 74     74   List::Objects::WithUtils::Array::Junction::All->new( @{ $_[0] } )
  74        
14         }
15          
16         1;
17          
18         =pod
19        
20         =head1 NAME
21        
22         List::Objects::WithUtils::Role::Array::WithJunctions - Add junctions
23        
24         =head1 SYNOPSIS
25        
26         ## Via List::Objects::WithUtils::Array ->
27         use List::Objects::WithUtils 'array';
28        
29         my $array = array(qw/ a b c /);
30        
31         if ( $array->any_items eq 'b' ) {
32         ...
33         }
34        
35         if ( $array->all_items eq 'a' ) {
36         ...
37         }
38        
39         if ( $array->any_items == qr/^b/ ) {
40         ...
41         }
42        
43         ## As a Role ->
44         use Role::Tiny::With;
45         with 'List::Objects::WithUtils::Role::Array',
46         'List::Objects::WithUtils::Role::Array::WithJunctions';
47        
48         =head1 DESCRIPTION
49        
50         These methods supply overloaded L<List::Objects::WithUtils::Array::Junction>
51         objects that can be compared with values using normal Perl comparison
52         operators.
53        
54         Regular expressions can be matched by providing a C<qr//> regular expression
55         object to the C<==> or C<!=> operators.
56        
57         There is no support for the C<~~> experimental smart-match operator.
58        
59         The junction objects returned are subclasses of
60         L<List::Objects::WithUtils::Array>, allowing manipulation of junctions (of
61         varying degrees of sanity) -- a simple case might be generating a new junction
62         out of an old junction:
63        
64         my $list = array(3, 4, 5);
65         if ( (my $anyof = $list->any_items) > 2 ) {
66         my $incr = $anyof->map(sub { $_[0] + 1 })->all_items;
67         if ( $incr > 3 ) {
68         ...
69         }
70         # Drop junction magic again:
71         my $plain = array( $incr->all );
72         }
73        
74         =head2 any_items
75        
76         Returns the overloaded B<any> object for the current array; a comparison is
77         true if any items in the array satisfy the condition.
78        
79         =head2 all_items
80        
81         Returns the overloaded B<all> object for the current array; a comparison is
82         true only if all items in the array satisfy the condition.
83        
84         =head1 AUTHOR
85        
86         Jon Portnoy <avenj@cobaltirc.org>
87        
88         =cut
89