| 1 | package DBIx::Class::InflateColumn::DateTime::Duration; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | DBIx::Class::InflateColumn::DateTime::Duration - Auto create | ||||||||||
| 6 | DateTime::Duration objects from columns | ||||||||||
| 7 | |||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||
| 9 | |||||||||||
| 10 | Load this component and then declare one or more columns as duration columns. | ||||||||||
| 11 | |||||||||||
| 12 | package Holiday; | ||||||||||
| 13 | __PACKAGE__->load_components(qw/InflateColumn::DateTime::Duration Core/); | ||||||||||
| 14 | __PACKAGE__->add_columns( | ||||||||||
| 15 | length => { | ||||||||||
| 16 | datatype => 'varchar', | ||||||||||
| 17 | size => 255, | ||||||||||
| 18 | is_nullable => 1, | ||||||||||
| 19 | is_duration => 1, | ||||||||||
| 20 | }, | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | Then you can treat the specified column as a L<DateTime::Duration> object. | ||||||||||
| 24 | |||||||||||
| 25 | print 'days: ', $holiday->length->delta_days, "\n"; | ||||||||||
| 26 | print 'hours: ', $holiday->length->delta_hours, "\n"; | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | This module inflates/deflates designated columns into L<DateTime::Duration> objects. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 1 | our $VERSION = '0.01002'; | |||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use base qw(DBIx::Class); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use DateTime::Format::Duration::XSD; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | =head1 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =head2 register_column | ||||||||||
| 47 | |||||||||||
| 48 | Chains with the "register_column" in L<DBIx::Class::Row> method, and sets up duration | ||||||||||
| 49 | columns appropriately. This would not normally be directly called by end users. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 3 | ▶ | sub register_column { | ||||||||
|
Subroutine
register_column: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 54 | 3 | my ($self, $column, $info, @rest) = @_; | |||||||||
| 55 | 3 | $self->next::method($column, $info, @rest); | |||||||||
| 56 | |||||||||||
| 57 | 3 | ▶ | return unless defined $info->{is_duration}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | $self->inflate_column( | ||||||||||
| 60 | $column => { | ||||||||||
| 61 | 5 | ▶ | inflate => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 62 | 5 | my ($value, $obj) = @_; | |||||||||
| 63 | 5 | my $duration; | |||||||||
| 64 | |||||||||||
| 65 | 5 | ▶ | if ($value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 5 | my $parser = DateTime::Format::Duration::XSD->new; | |||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 5 | $duration = $parser->parse_duration($value); | |||||||||
| 70 | } | ||||||||||
| 71 | 0 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 72 | 0 | $self->throw_exception('Could not parse duration from ' . $value); | |||||||||
| 73 | } | ||||||||||
| 74 | 5 | } | |||||||||
| 75 | |||||||||||
| 76 | 5 | return $duration; | |||||||||
| 77 | }, | ||||||||||
| 78 | 1 | ▶ | deflate => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 79 | 1 | my ($value, $obj) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | return unless (ref $value eq 'DateTime::Duration'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 1 | my $parser = DateTime::Format::Duration::XSD->new; | |||||||||
| 84 | |||||||||||
| 85 | 1 | return $parser->format_duration($value); | |||||||||
| 86 | }, | ||||||||||
| 87 | } | ||||||||||
| 88 | 1 | ); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | =head1 SEE ALSO | ||||||||||
| 92 | |||||||||||
| 93 | L<DateTime::Duration>, | ||||||||||
| 94 | L<DBIx::Class::InflateColumn>, | ||||||||||
| 95 | L<DBIx::Class>. | ||||||||||
| 96 | |||||||||||
| 97 | =head1 AUTHOR | ||||||||||
| 98 | |||||||||||
| 99 | Pete Smith, E<lt>pete@cubabit.netE<gt> | ||||||||||
| 100 | |||||||||||
| 101 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 102 | |||||||||||
| 103 | Copyright (C) 2010 by Pete Smith | ||||||||||
| 104 | |||||||||||
| 105 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 106 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||
| 107 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 1 | 1; | |||||||||
| 112 | |||||||||||