File Coverage

blib/lib/LINQ/FieldSet/Selection.pm
Criterion Covered Total %
statement 55 55 100.0
branch 11 12 100.0
condition 8 8 100.0
subroutine 14 14 100.0
pod 2 2 100.0
total 90 91 100.0


line stmt bran cond sub pod time code
1 1     1   18 use 5.006;
  1         4  
2 1     1   5 use strict;
  1         2  
  1         20  
3 1     1   5 use warnings;
  1         1  
  1         64  
4              
5             package LINQ::FieldSet::Selection;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.001';
9              
10 1     1   511 use Class::Tiny;
  1         1837  
  1         4  
11 1     1   68 use parent qw( LINQ::FieldSet );
  1         2  
  1         5  
12              
13 1     1   51 use overload q[&{}] => 'coderef';
  1         2  
  1         6  
14              
15             sub _known_parameter_names {
16 13     13   25 my ( $self ) = ( shift );
17            
18             return (
19 13         37 $self->SUPER::_known_parameter_names,
20             'as' => 1,
21             );
22             }
23              
24             sub target_class {
25 9     9 1 3017 my ( $self ) = ( shift );
26 9   100     49 $self->{target_class} ||= $self->_build_target_class;
27             }
28              
29             sub _build_target_class {
30 6     6   11 my ( $self ) = ( shift );
31 6         25 require Object::Adhoc;
32 6         10 return Object::Adhoc::make_class( [ keys %{ $self->fields_hash } ] );
  6         18  
33             }
34              
35             sub coderef {
36 8     8 1 1065 my ( $self ) = ( shift );
37 8   100     41 $self->{coderef} ||= $self->_build_coderef;
38             }
39              
40             sub _build_coderef {
41 5     5   8 my ( $self ) = ( shift );
42 5         8 my @fields = @{ $self->fields };
  5         124  
43 5         35 my $bless = $self->target_class;
44 5         952 my $asterisk = $self->seen_asterisk;
45             return sub {
46 6     6   13 my %output = ();
47 6 100       14 if ( $asterisk ) {
48 1         5 %output = %$_;
49             }
50 6         11 for my $field ( @fields ) {
51 16         93 $output{ $field->name } = $field->getter->( $_ );
52             }
53 6 100       168 $asterisk ? Object::Adhoc::object( \%output ) : bless( \%output, $bless );
54 5         62 };
55             } #/ sub _build_coderef
56              
57             sub _sql_selection {
58 4     4   74 my ( $self, $name_quoter ) = ( shift, @_ );
59             $name_quoter ||= sub {
60 5     5   8 my $name = shift;
61 5         22 return sprintf( '"%s"', quotemeta( $name ) );
62 4   100     26 };
63 4 100       96 return if $self->seen_asterisk;
64            
65 3         21 my @cols;
66 3         6 for my $field ( @{ $self->fields } ) {
  3         53  
67 8         155 my $orig_name = $field->value;
68 8         152 my $aliased = $field->name;
69 8 100       46 return if ref( $orig_name );
70             # uncoverable branch true
71 7 50       13 return if !defined( $aliased );
72            
73 7 100       16 if ( $aliased eq $orig_name ) {
74 5         10 push @cols, $name_quoter->( $orig_name );
75             }
76             else {
77 2         6 push @cols, sprintf(
78             '%s AS %s',
79             $name_quoter->( $orig_name ),
80             $name_quoter->( $aliased ),
81             );
82             }
83             } #/ for my $field ( @{ $self...})
84 2         18 return join( q[, ], @cols );
85             } #/ sub _sql_selection
86              
87             1;
88              
89             __END__
90              
91             =pod
92              
93             =encoding utf-8
94              
95             =head1 NAME
96              
97             LINQ::FieldSet::Selection - represents an SQL-SELECT-like transformation
98              
99             =head1 DESCRIPTION
100              
101             LINQ::FieldSet::Selection is a subclass of L<LINQ::FieldSet>.
102              
103             This is used internally by LINQ and you probably don't need to know about it
104             unless you're writing very specific extensions for LINQ. The end user
105             interface is the C<fields> function in L<LINQ::Util>.
106              
107             =head1 CONSTRUCTOR
108              
109             =over
110              
111             =item C<< new( ARGSLIST ) >>
112              
113             Constructs a fieldset from a list of fields like:
114              
115             'LINQ::FieldSet::Selection'->new(
116             'field1', -param1 => 'value1', -param2,
117             'field2', -param1 => 'value2',
118             );
119              
120             Allowed parameters are:
121             C<< -as >> (followed by a value).
122              
123             =back
124              
125             =head1 METHODS
126              
127             =over
128              
129             =item C<target_class>
130              
131             The class data selected by this selection will be blessed into.
132              
133             =item C<coderef>
134              
135             Gets a coderef for this assertion; the coderef operates on C<< $_ >>.
136              
137             =back
138              
139             =head1 OVERLOADING
140              
141             This class overloads
142             C<< &{} >> to call the C<< coderef >> method.
143              
144             =head1 BUGS
145              
146             Please report any bugs to
147             L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
148              
149             =head1 SEE ALSO
150              
151             L<LINQ::FieldSet>, L<LINQ::Util>.
152              
153             =head1 AUTHOR
154              
155             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
156              
157             =head1 COPYRIGHT AND LICENCE
158              
159             This software is copyright (c) 2021 by Toby Inkster.
160              
161             This is free software; you can redistribute it and/or modify it under
162             the same terms as the Perl 5 programming language system itself.
163              
164             =head1 DISCLAIMER OF WARRANTIES
165              
166             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
167             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
168             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.