DBIx/Class/CDBICompat/ColumnsAsHash.pm

filter: Statementstmt 30.9% 13 / 42 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 40.0% 4 / 10 Podpod 0.0% 0 / 2 totaltotal 26.5% 17 / 64 CC 7 SCAR 20.0
CC
7
Cov
25%
_make_columns_as_hash
12.0
FETCH
12.0
STORE
12.0
CRAP 7.4
« DBIx/Class/Schema.pm ↑ Summary DBIx/Class/CDBICompat/AttributeAPI.pm »
1 package
2 DBIx::Class::CDBICompat::ColumnsAsHash;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 =head1 NAME
9  
10 DBIx::Class::CDBICompat::ColumnsAsHash - Emulates the behavior of Class::DBI where the object can be accessed as a hash of columns.
11  
12 =head1 SYNOPSIS
13  
14 See DBIx::Class::CDBICompat for usage directions.
15  
16 =head1 DESCRIPTION
17  
18 Emulates the I<undocumented> behavior of Class::DBI where the object can be accessed as a hash of columns. This is often used as a performance hack.
19  
20 my $column = $result->{column};
21  
22 =head2 Differences from Class::DBI
23  
24 If C<DBIC_CDBICOMPAT_HASH_WARN> is true it will warn when a column is accessed as a hash key.
25  
26 =cut
27  
28 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my $class = shift;
30  
31 0 my $new = $class->next::method(@_);
32  
33 0 $new->_make_columns_as_hash;
34  
35 0 return $new;
36 }
37  
38 0 ▶sub inflate_result {
Subroutine inflate_result: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my $class = shift;
40  
41 0 my $new = $class->next::method(@_);
42  
43 0 $new->_make_columns_as_hash;
44  
45 0 return $new;
46 }
47  
48  
49 0 ▶sub _make_columns_as_hash {
Subroutine _make_columns_as_hash: not called CC=3 SCAR=24.8
50 0 my $self = shift;
51  
52 0 for my $col ($self->columns) {
53 0 ▶ if( exists $self->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$col}) 0 0 0
54 0 warn "Skipping mapping $col to a hash key because it exists";
55 }
56  
57 0 tie $self->{$col}, 'DBIx::Class::CDBICompat::Tied::ColumnValue',
58 $self, $col;
59 }
60 }
61  
62  
63 package DBIx::Class::CDBICompat::Tied::ColumnValue;
64  
65 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use Scalar::Util qw(weaken isweak);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68  
69 0 ▶sub TIESCALAR {
Subroutine TIESCALAR: not called CC=1 SCAR=6.9
70 0 my($class, $obj, $col) = @_;
71 0 my $self = [$obj, $col];
72 0 weaken $self->[0];
73  
74 0 return bless $self, $_[0];
75 }
76  
77 0 ▶sub FETCH {
Subroutine FETCH: not called CC=3 SCAR=24.8
78 0 my $self = shift;
79 0 my($obj, $col) = @$self;
80  
81 0 my $class = ref $obj;
82 0 my $id = $obj->id;
83 carp "Column '$col' of '$class/$id' was fetched as a hash"
84 0 ▶ if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBIC_CDBICOMPAT_HASH_WARN"} 0 0 0
85  
86 return $obj->column_info($col)->{_inflate_info}
87 0 ▶ ? $obj->get_inflated_column($col)
Branch: 1 branch
BranchTrueFalseTotal
$obj->column_info($col)->{'_inflate_info'} ? : 0 0 0
88 : $obj->get_column($col);
89 }
90  
91 0 ▶sub STORE {
Subroutine STORE: not called CC=3 SCAR=24.8
92 0 my $self = shift;
93 0 my($obj, $col) = @$self;
94  
95 0 my $class = ref $obj;
96 0 my $id = $obj->id;
97 carp "Column '$col' of '$class/$id' was stored as a hash"
98 0 ▶ if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBIC_CDBICOMPAT_HASH_WARN"} 0 0 0
99  
100 return $obj->column_info($col)->{_inflate_info}
101 0 ▶ ? $obj->set_inflated_column($col => shift)
Branch: 1 branch
BranchTrueFalseTotal
$obj->column_info($col)->{'_inflate_info'} ? : 0 0 0
102 : $obj->set_column($col => shift);
103 }
104  
105 =head1 FURTHER QUESTIONS?
106  
107 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
108  
109 =head1 COPYRIGHT AND LICENSE
110  
111 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
112 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
113 redistribute it and/or modify it under the same terms as the
114 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
115  
116 =cut
117  
118 2 1;
« DBIx/Class/Schema.pm ↑ Summary DBIx/Class/CDBICompat/AttributeAPI.pm »