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 3     3   74 use 5.006;
  3         10  
2 3     3   17 use strict;
  3         24  
  3         155  
3 3     3   19 use warnings;
  3         30  
  3         227  
4              
5             package LINQ::Field;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.002';
9              
10 3     3   20 use Class::Tiny qw( index name value params );
  3         27  
  3         38  
11              
12             sub BUILD {
13 78     78 1 3068 my ( $self ) = ( shift );
14            
15 78 100       207 if ( not $self->{params} ) {
16 7         15 $self->{params} = {};
17             }
18            
19 78 100       181 if ( not defined $self->{name} ) {
20 74 100 100     589 if ( defined $self->{params}{as} ) {
    100          
21 11         27 $self->{name} = $self->{params}{as};
22             }
23             elsif ( !ref $self->{value} and $self->{value} =~ /\A[^\W0-9]\w*\z/ ) {
24 61         188 $self->{name} = $self->{value};
25             }
26             else {
27 2         9 $self->{name} = sprintf( '_%d', $self->{index} );
28             }
29             } #/ if ( not defined $self...)
30            
31             } #/ sub BUILD
32              
33             sub getter {
34 68     68 1 827 my ( $self ) = @_;
35 68   100     251 $self->{getter} ||= $self->_build_getter;
36             }
37              
38             sub _build_getter {
39 63     63   98 my ( $self ) = @_;
40            
41 63         1057 my $attr = $self->value;
42            
43 63 100       356 if ( ref( $attr ) eq 'CODE' ) {
44 4         20 return $attr;
45             }
46            
47 59         231 require Scalar::Util;
48            
49             return sub {
50 130     130   351 my $blessed = Scalar::Util::blessed( $_ );
51 130 100 100     506 if ( ( $blessed || '' ) =~ /\AObject::Adhoc::__ANON__::/ ) {
52 86         149 $blessed = undef;
53             }
54 130 100       1039 scalar( $blessed ? $_->$attr : $_->{$attr} );
55 59         376 };
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.