File Coverage

blib/lib/LINQ/FieldSet/Selection.pm
Criterion Covered Total %
statement 42 43 97.6
branch 4 4 100.0
condition 6 6 100.0
subroutine 13 14 92.8
pod 3 3 100.0
total 68 70 97.1


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