File Coverage

blib/lib/LINQ/Field.pm
Criterion Covered Total %
statement 31 31 100.0
branch 14 14 100.0
condition 8 8 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 63 63 100.0


line stmt bran cond sub pod time code
1 2     2   36 use 5.006;
  2         7  
2 2     2   13 use strict;
  2         4  
  2         39  
3 2     2   9 use warnings;
  2         25  
  2         140  
4              
5             package LINQ::Field;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.001';
9              
10 2     2   12 use Class::Tiny qw( index name value params );
  2         3  
  2         22  
11              
12             sub BUILD {
13 84     84 1 2899 my ( $self ) = ( shift );
14            
15 84 100       189 if ( not $self->{params} ) {
16 7         12 $self->{params} = {};
17             }
18            
19 84 100       173 if ( not defined $self->{name} ) {
20 80 100 100     479 if ( defined $self->{params}{as} ) {
    100          
21 14         35 $self->{name} = $self->{params}{as};
22             }
23             elsif ( !ref $self->{value} and $self->{value} =~ /\A[^\W0-9]\w*\z/ ) {
24 63         178 $self->{name} = $self->{value};
25             }
26             else {
27 3         14 $self->{name} = sprintf( '_%d', $self->{index} );
28             }
29             } #/ if ( not defined $self...)
30            
31             } #/ sub BUILD
32              
33             sub getter {
34 64     64 1 837 my ( $self ) = @_;
35 64   100     189 $self->{getter} ||= $self->_build_getter;
36             }
37              
38             sub _build_getter {
39 59     59   79 my ( $self ) = @_;
40            
41 59         965 my $attr = $self->value;
42            
43 59 100       322 if ( ref( $attr ) eq 'CODE' ) {
44 4         17 return $attr;
45             }
46            
47 55         210 require Scalar::Util;
48            
49             return sub {
50 106     106   267 my $blessed = Scalar::Util::blessed( $_ );
51 106 100 100     386 if ( ( $blessed || '' ) =~ /\AObject::Adhoc::__ANON__::/ ) {
52 86         126 $blessed = undef;
53             }
54 106 100       578 scalar( $blessed ? $_->$attr : $_->{$attr} );
55 55         386 };
56             } #/ sub _build_getter
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding utf-8
65              
66             =head1 NAME
67              
68             LINQ::Field - represents a single field in a LINQ::FieldSet
69              
70             =head1 DESCRIPTION
71              
72             See L<LINQ::FieldSet> for details.
73              
74             This is used internally by LINQ and you probably don't need to know about it
75             unless you're writing very specific extensions for LINQ.
76              
77             =head1 CONSTRUCTOR
78              
79             =over
80              
81             =item C<< new( ARGSHASH ) >>
82              
83             =back
84              
85             =begin trustme
86              
87             =item BUILD
88              
89             =end trustme
90              
91             =head1 METHODS
92              
93             =over
94              
95             =item C<index>
96              
97             The order of a field within a fieldset. Starts at zero.
98              
99             =item C<name>
100              
101             The name of a field.
102              
103             =item C<value>
104              
105             The hashref key or method name used to find the value for the field from
106             a hashref or object; or a coderef which will get the value from C<< $_ >>.
107              
108             =item C<params>
109              
110             Additional metadata for the field.
111              
112             =item C<getter>
113              
114             A coderef which will get the value of a field from C<< $_ >>.
115              
116             =back
117              
118             =head1 BUGS
119              
120             Please report any bugs to
121             L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
122              
123             =head1 SEE ALSO
124              
125             L<LINQ::FieldSet>.
126              
127             =head1 AUTHOR
128              
129             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
130              
131             =head1 COPYRIGHT AND LICENCE
132              
133             This software is copyright (c) 2021 by Toby Inkster.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =head1 DISCLAIMER OF WARRANTIES
139              
140             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
141             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
142             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.