| 1 | package DBIx::Class::InflateColumn::Time; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 3 | ▶ | use base qw/DBIx::Class/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 3 | ▶ | use DateTime::Duration; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | our $VERSION = '0.0.1'; # VERSION | |||||||||||||
| 12 | # ABSTRACT: Automagically inflates time columns into DateTime::Duration objects | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =pod | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 NAME | ||||||||||||||
| 19 | |||||||||||||||
| 20 | DBIx::Class::InflateColumn::Time - Inflate and Deflate "time" columns into DateTime::Duration Objects | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | package HorseTrack::Database::Schema::Result::Race; | ||||||||||||||
| 25 | use base 'DBIx::Class::Core'; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | use strict; | ||||||||||||||
| 28 | use warnings; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | __PACKAGE__->load_components("InflateColumn::Time"); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | __PACKAGE__->add_columns( | ||||||||||||||
| 33 | race_number => { data_type => 'integer'}, | ||||||||||||||
| 34 | duration => { data_type => 'time'}, | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||
| 38 | |||||||||||||||
| 39 | This module can be used to automagically inflate database columns of data type "time" into | ||||||||||||||
| 40 | DateTime::Duration objects. It is used similiar to other InflateColumn DBIx modules. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | Once your Result is properly defined you can now pass DateTime::Duration objects into columns | ||||||||||||||
| 43 | of data_type time and retrieve DateTime::Duration objects from these columns as well | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =head2 Inflation | ||||||||||||||
| 46 | |||||||||||||||
| 47 | Inflation occurs whenever the data is being taken FROM the database. In this case the database | ||||||||||||||
| 48 | is storing the value with data_type of time, upon inflation a DateTime::Duration object is returned | ||||||||||||||
| 49 | from the resultset. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | package HorseTrack::Race; | ||||||||||||||
| 52 | |||||||||||||||
| 53 | use strict; | ||||||||||||||
| 54 | use warnings; | ||||||||||||||
| 55 | |||||||||||||||
| 56 | use Moose; | ||||||||||||||
| 57 | use namespace::autoclean; | ||||||||||||||
| 58 | |||||||||||||||
| 59 | use DateTime::Duration; | ||||||||||||||
| 60 | |||||||||||||||
| 61 | has 'race_number' => ( is => 'rw', isa => 'Int' ); | ||||||||||||||
| 62 | has 'duration' => ( is => 'rw', isa => 'DateTime::Duration' ); | ||||||||||||||
| 63 | |||||||||||||||
| 64 | sub retrieve { | ||||||||||||||
| 65 | my $self = shift; | ||||||||||||||
| 66 | |||||||||||||||
| 67 | my $result = $schema->resultset('...')->search({ race_number => 1 })->single; | ||||||||||||||
| 68 | |||||||||||||||
| 69 | my $race = $self->new({ | ||||||||||||||
| 70 | race_number => $result->race_number, | ||||||||||||||
| 71 | duration => $result->duration, | ||||||||||||||
| 72 | }); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | return $race; | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | __PACKAGE__->meta->make_immutable(); | ||||||||||||||
| 78 | 1; | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =head2 Deflation | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Deflation occurs whenever the data is being taken TO the database. In this case an object | ||||||||||||||
| 83 | of type DateTime::Duration is being stored into the a database columns with a data_type of "time". | ||||||||||||||
| 84 | Using the same object from the Inflation example: | ||||||||||||||
| 85 | |||||||||||||||
| 86 | $schema->resultset('...')->create({ | ||||||||||||||
| 87 | race_number => $self->race_number, | ||||||||||||||
| 88 | duration => $self->duration, | ||||||||||||||
| 89 | }); | ||||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||
| 93 | |||||||||||||||
| 94 | Strictly speaking, you don't actually call any of these methods yourself. DBIx handles the magic | ||||||||||||||
| 95 | provided you have included the InflateColumn::Time component in your Result. | ||||||||||||||
| 96 | |||||||||||||||
| 97 | Therefore, there are no public methods to be consumed. | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =cut | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | ▶ | sub register_column { | ||||||||||||
|
Subroutine
register_column: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 102 | 0 | my ($self, $column, $info, @rest) = @_; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | $self->next::method($column, $info, @rest); | |||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | ▶ | return unless $info->{data_type} eq 'time'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | $self->inflate_column( | |||||||||||||
| 109 | $column => { | ||||||||||||||
| 110 | inflate => \&_inflate, | ||||||||||||||
| 111 | deflate => \&_deflate, | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | ); | ||||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 6 | ▶ | sub _inflate { | ||||||||||||
|
Subroutine
_inflate: called CC=2 SCAR=6.9
| |||||||||||||||
| 117 | 6 | my $value = shift; | |||||||||||||
| 118 | |||||||||||||||
| 119 | 6 | my ($sign, $hours, $minutes, $seconds) = $value =~ m/(-?)0?(\d+):0?(\d+):0?(\d+)/g; | |||||||||||||
| 120 | |||||||||||||||
| 121 | ### Sign: (defined $sign ? "-" : "+") | ||||||||||||||
| 122 | ### Hours: $hours | ||||||||||||||
| 123 | ### Minutes: $minutes | ||||||||||||||
| 124 | ### Seconds: $seconds | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 6 | my $duration = DateTime::Duration->new({ | |||||||||||||
| 127 | hours => $hours, | ||||||||||||||
| 128 | minutes => $minutes, | ||||||||||||||
| 129 | seconds => $seconds, | ||||||||||||||
| 130 | }); | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 6 | ▶ | if($sign) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 3 | return $duration->inverse; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 3 | return $duration; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 8 | ▶ | sub _deflate { | ||||||||||||
|
Subroutine
_deflate: called CC=3 SCAR=11.0
| |||||||||||||||
| 140 | 8 | my $value = shift; | |||||||||||||
| 141 | |||||||||||||||
| 142 | # For time purposes we'll always assume that a day is 24 hours. | ||||||||||||||
| 143 | 8 | my $hours = $value->hours + ($value->days * 24); | |||||||||||||
| 144 | |||||||||||||||
| 145 | 8 | ▶ | my $time = ($value->is_negative ? '-' : '') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 146 | . sprintf( $hours >= 100 ? "%03d" : "%02d" , $hours) . ':' | ||||||||||||||
| 147 | . sprintf( "%02d", $value->minutes) . ':' | ||||||||||||||
| 148 | . sprintf( "%02d", $value->seconds); | ||||||||||||||
| 149 | |||||||||||||||
| 150 | 8 | return $time; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 3 | 1; | |||||||||||||
| 154 | |||||||||||||||
| 155 | __END__ | ||||||||||||||