blib/lib/SPOPSx/Tool/DateTime.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 86 CC 4 SCAR 14.8
CC
4
Cov
0%
_require_format
6.0
convert_to_date
6.0
convert_to_string
6.0
CRAP 4.4
1 package SPOPSx::Tool::DateTime;
2  
3 use strict;
4 use warnings;
5  
6 use Log::Log4perl;
7 my $log = Log::Log4perl->get_logger(__PACKAGE__);
8  
9 our $VERSION = '0.02';
10  
11 =head1 NAME
12  
13 SPOPSx::Tool::DateTime - SPOPS extension for arbitrarily formatted DateTime fields
14  
15 =head1 SYNOPSIS
16  
17 %conf = (
18 table_alias => {
19 # ...
20 datetime_format => {
21 atime => 'DateTime::Format::MySQL',
22 mtime => 'DateTime::Format::Baby',
23 ctime => DateTime::Format::Strptime->new(pattern => '%D %T'),
24 },
25 # ...
26 }
27 );
28  
29 =head1 DESCRIPTION
30  
31 This module allows for L<DateTime> objects to be stored to and loaded from a database field. This module differs from the L<SPOPS::Tool::DateConvert> tool that is distributed with SPOPS in that it allows for the use of arbitrary L<DateTime> format objects. (See L<http://datetime.perl.org/> for details on DateTime and formatting.)
32  
33 =cut
34  
35 sub ruleset_factory {
36 my ($class, $rstab) = @_;
37 push @{ $rstab->{post_fetch_action} }, \&convert_to_date;
38 push @{ $rstab->{pre_save_action} }, \&convert_to_string;
39 push @{ $rstab->{post_save_action} }, \&convert_to_date;
40 $log->is_info &&
41 $log->info("DateTime ruleset added post_fetch, pre/post_save rules to [$class]");
42 return __PACKAGE__;
43 }
44  
45 sub _require_format {
46 my $class = shift;
47 my $format = shift;
48 $format =~ /^[\w:]+$/
49 or die "Bad format package $format for $class.";
50 eval "require $format";
51 warn "Possible error including $format: $@" if $@;
52 $log->is_info &&
53 $log->info("DateTime ruleset required $format.");
54 }
55  
56 sub convert_to_date {
57 my $self = shift;
58 my $config = $self->CONFIG;
59  
60 while (my ($field, $format) = each %{ $config->{datetime_format} }) {
61 defined $self->{$field} or next;
62  
63 $log->debug("Converting $self->{$field} to datetime.");
64  
65 unless (ref $format) {
66 _require_format($config->{class}, $format);
67 $config->{datetime_format}{$field} = $format = $format->new;
68 }
69
70 $self->{$field} = $format->parse_datetime($self->{$field});
71 }
72  
73 return __PACKAGE__;
74 }
75  
76 sub convert_to_string {
77 my $self = shift;
78 my $config = $self->CONFIG;
79  
80 while (my ($field, $format) = each %{ $config->{datetime_format} }) {
81 defined $self->{$field} or next;
82  
83 $log->debug("Converting $self->{$field} to string.");
84  
85 unless (ref $format) {
86 _require_format($config->{class}, $format);
87 $config->{datetime_format}{$field} = $format = $format->new;
88 }
89
90 $self->{$field} = $format->format_datetime($self->{$field});
91 }
92  
93 return __PACKAGE__;
94 }
95  
96 =head1 INSTALLATION
97  
98 Typical:
99  
100 perl Build.PL
101 ./Build
102 ./Build test
103 ./Build install
104  
105 or:
106  
107 perl Makefile.PL
108 make
109 make test
110 make install
111  
112 =head1 SEE ALSO
113  
114 L<SPOPS>, L<DateTime>, L<http://datetime.perl.org/>, L<SPOPS::Tool::DateConvert>
115  
116 =head1 AUTHOR
117  
118 Andrew Sterling Hanenkamp, E<lt>hanenkamp@users.sourceforge.netE<gt>
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 Copyright 2005 Andrew Sterling Hanenkamp. All Rights Reserved.
123  
124 This library is distributed and licensed under the same terms as Perl itself.
125  
126 =cut
127  
128 1