File Coverage

blib/lib/LINQ/FieldSet/Single.pm
Criterion Covered Total %
statement 28 35 80.0
branch 1 4 25.0
condition 2 3 66.6
subroutine 9 11 81.8
pod 3 3 100.0
total 43 56 76.7


line stmt bran cond sub pod time code
1 1     1   33 use 5.006;
  1         4  
2 1     1   6 use strict;
  1         2  
  1         27  
3 1     1   5 use warnings;
  1         2  
  1         89  
4              
5             package LINQ::FieldSet::Single;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.002';
9              
10 1     1   8 use Class::Tiny;
  1         2  
  1         11  
11 1     1   98 use parent qw( LINQ::FieldSet );
  1         1  
  1         10  
12              
13             use overload (
14             'fallback' => !!1,
15 0     0   0 q[bool] => sub { !! 1 },
16 1         14 q[""] => 'to_string',
17             q[&{}] => 'coderef',
18 1     1   101 );
  1         4  
19              
20             sub new {
21 4     4 1 9 my $class = shift;
22 4         8 my $field = shift;
23 4         31 my $self = $class->SUPER::new( $field );
24            
25 4 50       79 if ( wantarray ) {
26 4         31 return ( $self, @_ );
27             }
28            
29 0 0       0 if ( @_ ) {
30 0         0 LINQ::Util::Internal::throw(
31             'CallerError',
32             'message' => 'Unexpected extra parameters; single field name expected',
33             );
34             }
35            
36 0         0 return $self;
37             }
38              
39             sub coderef {
40 24     24 1 42 my ( $self ) = ( shift );
41 24   66     79 $self->{coderef} ||= $self->_build_coderef;
42             }
43              
44             sub _build_coderef {
45 4     4   8 my ( $self ) = ( shift );
46 4         9 my ( $field ) = @{ $self->fields };
  4         152  
47 4         39 return $field->getter;
48             }
49              
50             sub to_string {
51 0     0 1   my ( $self ) = ( shift );
52 0           sprintf 'field(%s)', join q[, ], map $_->name, @{ $self->fields };
  0            
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding utf-8
62              
63             =head1 NAME
64              
65             LINQ::FieldSet::Single - similar to LINQ::FieldSet::Selection, but only selects one field
66              
67             =head1 DESCRIPTION
68              
69             LINQ::FieldSet::Single is a subclass of L<LINQ::FieldSet>.
70              
71             This is used internally by LINQ and you probably don't need to know about it
72             unless you're writing very specific extensions for LINQ. The end user
73             interface is the C<field> function in L<LINQ::Util>.
74              
75             =head1 CONSTRUCTOR
76              
77             =over
78              
79             =item C<< new( NAME, EXTRAARGS ) >>
80              
81             Constructs a fieldset like:
82              
83             'LINQ::FieldSet::Single'->new( 'name', @stuff );
84              
85             If called in scalar context, and C<< @stuff >> isn't the empty list, then
86             will throw an exception.
87              
88             If called in list context, will return the newly constructed object followed
89             by unadulterated C<< @stuff >>.
90              
91             =back
92              
93             =head1 METHODS
94              
95             =over
96              
97             =item C<coderef>
98              
99             Gets a coderef for this assertion; the coderef operates on C<< $_ >>.
100              
101             =item C<to_string>
102              
103             Basic string representation of the fieldset.
104              
105             =back
106              
107             =head1 OVERLOADING
108              
109             This class overloads
110             C<< "" >> to call the C<< to_string >> method and
111             C<< &{} >> to call the C<< coderef >> method.
112              
113             =head1 BUGS
114              
115             Please report any bugs to
116             L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
117              
118             =head1 SEE ALSO
119              
120             L<LINQ::FieldSet>, L<LINQ::Util>.
121              
122             =head1 AUTHOR
123              
124             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
125              
126             =head1 COPYRIGHT AND LICENCE
127              
128             This software is copyright (c) 2021 by Toby Inkster.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.
132              
133             =head1 DISCLAIMER OF WARRANTIES
134              
135             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
136             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
137             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.