| 1 | package SPOPS::Tool::DateConvert; | ||
| 2 | |||
| 3 | # $Id: DateConvert.pm,v 1.10 2004/02/26 01:01:37 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | |||
| 9 | my $log = get_logger(); | ||
| 10 | |||
| 11 | my $DEFAULT_DATE_CLASS = 'DateTime'; | ||
| 12 | my $DEFAULT_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'; | ||
| 13 | |||
| 14 | my %REQUIRED = (); | ||
| 15 | |||
| 16 | sub ruleset_factory { | ||
| 17 | my ( $class, $ruleset ) = @_; | ||
| 18 | push @{ $ruleset->{post_fetch_action} }, \&convert_to_object; | ||
| 19 | push @{ $ruleset->{pre_save_action} }, \&convert_to_string; | ||
| 20 | push @{ $ruleset->{post_save_action} }, \&convert_to_object; | ||
| 21 | $log->is_info && | ||
| 22 | $log->info( "DateConvert added post_fetch, pre/post_save rules to [$class]" ); | ||
| 23 | return __PACKAGE__; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub convert_to_object { | ||
| 27 | my ( $self ) = @_; | ||
| 28 | my ( $date_fields, $date_class ) = _init( $self ); | ||
| 29 | unless ( ref $date_fields eq 'ARRAY' and scalar @{ $date_fields } ) { | ||
| 30 | $log->warn( "Using date conversion for ", ref( $self ), " but there ", | ||
| 31 | "are no date fields in 'convert_date_field'" ); | ||
| 32 | return 1; | ||
| 33 | } | ||
| 34 | foreach my $field ( @{ $date_fields } ) { | ||
| 35 | $self->{ $field } = eval { | ||
| 36 | _create_date_object( $self, $date_class, $self->{ $field } ) | ||
| 37 | }; | ||
| 38 | return undef if ( $@ ); | ||
| 39 | } | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _create_date_object { | ||
| 44 | my ( $self, $date_class, $date ) = @_; | ||
| 45 | return undef unless ( $date ); | ||
| 46 | if ( ref $date eq $date_class ) { | ||
| 47 | return $date; | ||
| 48 | } | ||
| 49 | if ( $date_class eq 'Class::Date' ) { | ||
| 50 | return Class::Date->new( $date ); | ||
| 51 | } | ||
| 52 | elsif ( $date_class eq 'Time::Piece' ) { | ||
| 53 | my $date_format = $self->CONFIG->{convert_date_format} | ||
| 54 | || $DEFAULT_DATE_FORMAT; | ||
| 55 | return Time::Piece->strptime( $date, $date_format ); | ||
| 56 | } | ||
| 57 | elsif ( $date_class eq 'DateTime' ) { | ||
| 58 | my $date_format = $self->CONFIG->{convert_date_format} | ||
| 59 | || $DEFAULT_DATE_FORMAT; | ||
| 60 | return DateTime::Format::Strptime->new( pattern => $date_format ) | ||
| 61 | ->parse_datetime( $date ); | ||
| 62 | } | ||
| 63 | die "Given date class [$date_class] is not supported for conversion\n"; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub convert_to_string { | ||
| 67 | my ( $self ) = @_; | ||
| 68 | my ( $date_fields, $date_class ) = _init( $self ); | ||
| 69 | unless ( ref $date_fields eq 'ARRAY' and scalar @{ $date_fields } ) { | ||
| 70 | $log->warn( "Using date conversion for ", ref( $self ), " but there ", | ||
| 71 | "are no date fields in 'convert_date_field'" ); | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | foreach my $field ( @{ $date_fields } ) { | ||
| 75 | $self->{ $field } = _create_date_string( $self, $date_class, $self->{ $field } ); | ||
| 76 | } | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _create_date_string { | ||
| 81 | my ( $self, $date_class, $date_object ) = @_; | ||
| 82 | |||
| 83 | # NULLs are ok... | ||
| 84 | |||
| 85 | unless ( $date_object ) { | ||
| 86 | return undef; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Strings are semi-ok... | ||
| 90 | |||
| 91 | unless ( ref( $date_object ) ) { | ||
| 92 | $log->info( "Expected date object of type '$date_class' but item ", | ||
| 93 | "is not a reference, which probably means it's a ", | ||
| 94 | "string representation of a date ($date_object). ", | ||
| 95 | "Hope it's in the right format, it's going in as is." ); | ||
| 96 | return $date_object | ||
| 97 | } | ||
| 98 | |||
| 99 | # ... but getting something that's not an object when we expect | ||
| 100 | # something is a different matter and deserves a warning | ||
| 101 | |||
| 102 | unless ( ref( $date_object ) eq $date_class ) { | ||
| 103 | $log->warn( "Expected date object of type '$date_class' but ", | ||
| 104 | "got '", ref( $date_object ), "'; not converting." ); | ||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | my $date_format = $self->CONFIG->{convert_date_format} | ||
| 108 | || $DEFAULT_DATE_FORMAT; | ||
| 109 | if ( $date_class eq 'Class::Date' ) { | ||
| 110 | return $date_object->strftime( $date_format ); | ||
| 111 | } | ||
| 112 | elsif ( $date_class eq 'Time::Piece' ) { | ||
| 113 | return $date_object->strftime( $date_format ); | ||
| 114 | } | ||
| 115 | elsif ( $date_class eq 'DateTime' ) { | ||
| 116 | return $date_object->strftime( $date_format ); | ||
| 117 | } | ||
| 118 | die "Given date class [$date_class] is not supported for conversion\n"; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _init { | ||
| 122 | my ( $self ) = @_; | ||
| 123 | my $date_fields = $self->CONFIG->{convert_date_field}; | ||
| 124 | return () unless ( ref $date_fields eq 'ARRAY' | ||
| 125 | and scalar @{ $date_fields } ); | ||
| 126 | my $date_class = $self->CONFIG->{convert_date_class} | ||
| 127 | || $DEFAULT_DATE_CLASS; | ||
| 128 | unless ( $REQUIRED{ $date_class } ) { | ||
| 129 | _require_class( $date_class ); | ||
| 130 | } | ||
| 131 | return ( $date_fields, $date_class ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _require_class { | ||
| 135 | my ( $date_class ) = @_; | ||
| 136 | eval "require $date_class"; | ||
| 137 | if ( $@ ) { | ||
| 138 | die "Cannot bring in date library $date_class: $@\n"; | ||
| 139 | } | ||
| 140 | $REQUIRED{ $date_class }++; | ||
| 141 | |||
| 142 | # HACK! | ||
| 143 | if ( $date_class eq 'DateTime' ) { | ||
| 144 | require DateTime::Format::Strptime; | ||
| 145 | $REQUIRED{ 'DateTime::Format::Strptime' }++; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |