File Coverage

blib/lib/LINQ/FieldSet.pm
Criterion Covered Total %
statement 47 47 100.0
branch 8 8 100.0
condition 12 12 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 78 78 100.0


line stmt bran cond sub pod time code
1 4     4   1936 use 5.006;
  4         15  
2 4     4   24 use strict;
  4         6  
  4         96  
3 4     4   20 use warnings;
  4         6  
  4         258  
4              
5              
6             our $AUTHORITY = 'cpan:TOBYINK';
7             our $VERSION = '0.003';
8              
9             use Class::Tiny qw( fields seen_asterisk );
10 4     4   27 use LINQ::Util::Internal ();
  4         7  
  4         24  
11 4     4   1522  
  4         8  
  4         1852  
12             my ( $self ) = ( shift );
13             return ();
14 59     59   84 }
15 59         293  
16             my ( $class, @args ) = ( shift, @_ );
17            
18             if ( @args == 1 and ref( $args[0] ) eq 'HASH' ) {
19 60     60 1 1104 return $args[0];
20             }
21 60 100 100     189
22 1         4 require LINQ::Field;
23            
24             my %known = $class->_known_parameter_names;
25 59         2016
26             my @fields;
27 59         173 my $idx = 0;
28             my $seen_asterisk;
29 59         107
30 59         87 ARG: while ( @args ) {
31 59         67 my $value = shift @args;
32             if ( $value eq '*' ) {
33 59         130 $seen_asterisk = 1;
34 78         128 next ARG;
35 78 100       173 }
36 5         10 my %params = ();
37 5         14 while ( @args and !ref $args[0] and $args[0] =~ /\A-/ ) {
38             my $p_name = substr( shift( @args ), 1 );
39 73         108 if ( $known{$p_name} ) {
40 73   100     448 $params{$p_name} = shift( @args );
      100        
41 77         169 }
42 77 100       186 elsif ( exists $known{$p_name} ) {
    100          
43 60         229 $params{$p_name} = 1;
44             }
45             else {
46 16         65 LINQ::Util::Internal::throw(
47             'CallerError',
48             message => "Unknown field parameter '$p_name'",
49 1         8 );
50             }
51             } #/ while ( @args and !ref $args...)
52            
53             my $field = 'LINQ::Field'->new(
54             value => $value,
55             index => ++$idx,
56 72         245 params => \%params,
57             );
58             push @fields, $field;
59             } #/ ARG: while ( @args )
60            
61 72         447 return {
62             fields => \@fields,
63             seen_asterisk => $seen_asterisk,
64             };
65 58         254 } #/ sub BUILDARGS
66              
67             my ( $self ) = ( shift );
68             $self->{fields_hash} ||= $self->_build_fields_hash;
69             }
70              
71 11     11 1 2109 my ( $self ) = ( shift );
72 11   100     105 my %fields;
73             foreach my $field ( @{ $self->fields } ) {
74             $fields{ $field->name } = $field;
75             }
76 7     7   13 return \%fields;
77 7         10 }
78 7         9  
  7         130  
79 17         338 1;
80              
81 7         85  
82              
83             =pod
84              
85             =encoding utf-8
86              
87             =head1 NAME
88              
89             LINQ::FieldSet - base class for LINQ::FieldSet::{Selection,Assertion}
90              
91             =head1 DESCRIPTION
92              
93             This is used internally by LINQ and you probably don't need to know about it
94             unless you're writing very specific extensions for LINQ.
95              
96             =head1 CONSTRUCTOR
97              
98             =over
99              
100             =item C<< new( ARGSLIST ) >>
101              
102             Constructs a fieldset from a list of fields like:
103              
104             'LINQ::FieldSet'->new(
105             'field1', -param1 => 'value1', -param2,
106             'field2', -param1 => 'value2',
107             );
108              
109             The list of allowed parameters and whether each one takes a value is returned
110             by the C<_known_parameter_names> method. This is empty in LINQ::FieldSet itself
111             so doing anything interesting with this class probably requires subclassing.
112              
113             =back
114              
115             =begin trustme
116              
117             =item BUILDARGS
118              
119             =end trustme
120              
121             =head1 METHODS
122              
123             =over
124              
125             =item C<fields>
126              
127             An arrayref of fields in this fieldset.
128              
129             =item C<fields_hash>
130              
131             The same, but as a hashref keyed on field name.
132              
133             =item C<seen_asterisk>
134              
135             Whether "*" was seen by the constructor.
136              
137             =back
138              
139             =head1 BUGS
140              
141             Please report any bugs to
142             L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
143              
144             =head1 SEE ALSO
145              
146             L<LINQ::FieldSet>.
147              
148             =head1 AUTHOR
149              
150             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
151              
152             =head1 COPYRIGHT AND LICENCE
153              
154             This software is copyright (c) 2021 by Toby Inkster.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =head1 DISCLAIMER OF WARRANTIES
160              
161             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
162             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
163             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.