Transfigure/Type/DBIx/Recursive.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Transfigure/Type/DBIx.pm ↑ Summary Transfigure/Value.pm »
1 package Data::Transfigure::Type::DBIx::Recursive 1.03;
2 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: transfigures DBIx::Class::Rows into hashrefs, traversing (some) relationships
6  
7 =head1 NAME
8  
9 Data::Transfigure::Type::DBIx - transfigures DBIx::Class::Rows into hashrefs,
10 traversing (some) relationships
11  
12 =head1 DESCRIPTION
13  
14 C<Data::Transfigure::Type::DBIx> is used to transfigure L<DBIx::Class::Row>
15 instances into JSON-able structures, using C<get_inflated_columns> to get make
16 a hashref from the object's keys (column names) and values.
17  
18 This transfigurator traverses to_one-type relationships, inserting the related
19 object into the resulting hashref with its relationship name as the key, instead
20 of the foreign key column name.
21  
22 =cut
23  
24 0 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=2.6
25  
26 use Data::Transfigure::Type;
27 class Data::Transfigure::Type::DBIx::Recursive : isa(Data::Transfigure::Type) {
28  
29 =head1 FIELDS
30  
31 I<none>
32  
33 =cut
34  
35 sub BUILDARGS ($class) {
36 $class->SUPER::BUILDARGS(
37 type => q(DBIx::Class::Row),
38 handler => sub ($data) {
39 my %cols = $data->get_inflated_columns;
40 foreach my $rel ($data->result_source->relationships) {
41 my $info = $data->result_source->relationship_info($rel);
42 if ($info->{attrs}->{accessor} eq 'single') {
43 delete(@cols{keys($info->{attrs}->{fk_columns}->%*)});
44 $cols{$rel} = $data->$rel;
45 }
46 }
47 return {%cols};
48 }
49 );
50 }
51  
52 }
53  
54 =pod
55  
56 =head1 AUTHOR
57  
58 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
59  
60 =head1 LICENSE
61  
62 Copyright (c) 2024 Mark Tyrrell
63  
64 Permission is hereby granted, free of charge, to any person obtaining a copy
65 of this software and associated documentation files (the "Software"), to deal
66 in the Software without restriction, including without limitation the rights
67 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
68 copies of the Software, and to permit persons to whom the Software is
69 furnished to do so, subject to the following conditions:
70  
71 The above copyright notice and this permission notice shall be included in all
72 copies or substantial portions of the Software.
73  
74 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
76 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
80 SOFTWARE.
81  
82 =cut
83  
84 1;
85  
86 __END__
« Transfigure/Type/DBIx.pm ↑ Summary Transfigure/Value.pm »