| 1 | package DBIx::Class::FormatColumns; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use vars qw/$VERSION/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw/DBIx::Class/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use DateTime::Format::DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | __PACKAGE__->mk_classdata( format_datatype_columns_only => 0 ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | DBIx::Class::FormatColumns - Creates format accessors for you | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | package Artist; | ||||||||||||||||||||||||||||
| 22 | __PACKAGE__->load_components(qw/FormatColumns Core/); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | __PACKAGE__->add_columns( | ||||||||||||||||||||||||||||
| 25 | message => {}, | ||||||||||||||||||||||||||||
| 26 | date_start => { accessor => 'start_date', data_type => 'datetime' } | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | __PACKAGE__->format_columns; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # accessing the data | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | print $rc->message_format_ashtml, "\n"; | ||||||||||||||||||||||||||||
| 33 | print $rc->date_start_format_full_datetime, "\n"; | ||||||||||||||||||||||||||||
| 34 | print $rc->start_date_format_long_date, "\n"; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | This modul creates format accessors for you. | ||||||||||||||||||||||||||||
| 40 | It tries to be smart and uses the I<data_type> property of the column | ||||||||||||||||||||||||||||
| 41 | to know which kind of format accessors it should create. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 DateTime, Date formats | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | If you did not use L<DBIx::Class::InflateColumns> to I<inflate> your datetime/date column, | ||||||||||||||||||||||||||||
| 46 | it uses L<DateTime::Format::DBI> to convert the value to an L<DateTime> object. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head3 format_full_date | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Calls DateTime->locale->full_date_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 51 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head3 format_long_date | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Calls DateTime->locale->long_date_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 56 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head3 format_medium_date | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Calls DateTime->locale->medium_date_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 61 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head3 format_short_date | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Calls DateTime->locale->short_date_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 66 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head3 format_full_time | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Calls DateTime->locale->full_time_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 71 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head3 format_long_time | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Calls DateTime->locale->long_time_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 76 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head3 format_medium_time | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Calls DateTime->locale->medium_time_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 81 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head3 format_short_time | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Calls DateTime->locale->short_time_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 86 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head3 format_full_datetime | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Calls DateTime->locale->full_datetime_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 91 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head3 format_long_datetime | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Calls DateTime->locale->long_datetime_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 96 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head3 format_medium_datetime | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Calls DateTime->locale->medium_datetime_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 101 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head3 format_short_datetime | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Calls DateTime->locale->short_datetime_format to retrieve the format string to put it into | ||||||||||||||||||||||||||||
| 106 | a DateTime->strftime call. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 Data formats | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head3 format_ashtml | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Uses L<HTML::Entities> to encode all entities. But it also replaces all linebreaks with I< <br /> >. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 METHODS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 format_columns( [ @columns ] ) | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Creates format accessors for every column in I<@columns>. | ||||||||||||||||||||||||||||
| 120 | Uses __PACKAGE__->columns if I<@columns> is empty; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub format_columns { | ||||||||||||||||||||||||||
|
Subroutine
format_columns: not called CC=11 SCAR=47.1
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 0 | my ($self, @columns) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | @columns = $self->columns | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | unless scalar(@columns) > 0; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | foreach my $col (@columns) { | |||||||||||||||||||||||||||
| 131 | 0 | my $ci = $self->column_info( $col ); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | my $type = exists $ci->{data_type} | ||||||||||||||||||||||||||||
| 134 | ? lc $ci->{data_type} | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | next if $self->format_datatype_columns_only | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->format_datatype_columns_only and $type eq ""
MC/DC0%0/2
$self->format_datatype_columns_only and $type eq ""
$self->format_datatype_columns_only
$type eq ""
Truth table0%0/3
A and B
A= $self->format_datatype_columns_onlyB= $type eq ""
| |||||||||||||||||||||||||||||
| 138 | && $type eq ''; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my @names = ( $col ); | |||||||||||||||||||||||||||
| 141 | push @names, $ci->{accessor} | ||||||||||||||||||||||||||||
| 142 | if exists $ci->{accessor} | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | && $ci->{accessor} ne $col; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $ci->{"accessor"} and $ci->{"accessor"} ne $col
MC/DC0%0/2
exists $ci->{"accessor"} and $ci->{"accessor"} ne $col
exists $ci->{"accessor"}
$ci->{"accessor"} ne $col
Truth table0%0/3
A and B
A= exists $ci->{"accessor"}B= $ci->{"accessor"} ne $col
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | foreach my $name (@names) { | |||||||||||||||||||||||||||
| 146 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if( $type =~ m!^date! ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my @methods = qw/ | |||||||||||||||||||||||||||
| 151 | full_date_format long_date_format medium_date_format short_date_format | ||||||||||||||||||||||||||||
| 152 | full_time_format long_time_format medium_time_format short_time_format | ||||||||||||||||||||||||||||
| 153 | full_datetime_format long_datetime_format medium_datetime_format short_datetime_format | ||||||||||||||||||||||||||||
| 154 | /; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | foreach my $method (@methods) { | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $method =~ m!^(.*)_format$!; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | *{"${self}::$name\_format_$1"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 0 | my $dt = $self->$col; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | unless ( ref($dt) =~ m!DateTime! ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | my $parser = DateTime::Format::DBI->new( $self->result_source->storage->dbh ); | |||||||||||||||||||||||||||
| 164 | 0 | $dt = $parser->parse_datetime( $dt ); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | return $dt->strftime( $dt->locale->$method ); | |||||||||||||||||||||||||||
| 167 | 0 | }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | *{"${self}::$name\_format_ashtml"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 174 | 0 | my $data = $self->$col; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | $data = encode_entities($data); | |||||||||||||||||||||||||||
| 177 | 0 | $data =~ s!\r?\n\r?!<br />\n!g; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | return $data; | |||||||||||||||||||||||||||
| 180 | 0 | }; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 format_datatype_columns_only( $boolean ) | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | If set to a true value, format_columns will only format columns | ||||||||||||||||||||||||||||
| 189 | that have a I<data_type> configured. The default value is 0. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Sascha Kiefer <esskar@cpan.org> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 198 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 201 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | 1; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||