File Coverage

blib/lib/DateTimeX/Role/Immutable.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 0 1 0.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             package DateTimeX::Role::Immutable;
2              
3             # ABSTRACT: A role that can be composed into a DateTime subclass to make it immutable
4              
5 4     4   2347 use strict;
  4         8  
  4         108  
6 4     4   21 use warnings;
  4         8  
  4         105  
7 4     4   20 use Carp;
  4         7  
  4         236  
8 4     4   21 use Role::Tiny;
  4         41  
  4         38  
9 4     4   3560 use Sub::Install;
  4         6795  
  4         19  
10 4     4   130 use DateTime;
  4         7  
  4         83  
11 4     4   19 use namespace::autoclean;
  4         7  
  4         27  
12              
13             our $VERSION = '0.36';
14              
15             my %mutators = (
16             add_duration => 'plus_duration',
17             subtract_duration => 'minus_duration',
18             add => 'plus',
19             subtract => 'minus',
20             truncate => 'trunc',
21             set => 'with_component',
22             set_year => 'with_year',
23             set_month => 'with_month',
24             set_day => 'with_day',
25             set_hour => 'with_hour',
26             set_minute => 'with_minute',
27             set_second => 'with_second',
28             set_nanosecond => 'with_nanosecond',
29             );
30             ## Consider: set_time_zone set_locale set_formatter
31              
32             before keys %mutators => sub {
33             my $orig = shift;
34             my $self = shift;
35              
36             croak "Attempted to mutate a DateTime object";
37             };
38              
39             while ( my ( $orig_method, $new_method ) = each %mutators ) {
40             Sub::Install::install_sub( {
41             code => sub {
42 14     14   6797 my $self = shift;
43 14         62 my $new_dt = DateTime->from_object( object => $self );
44 14         7267 $new_dt->$orig_method(@_);
45 14         7212 return bless $new_dt, ref $self;
46             },
47             as => $new_method,
48             } );
49             }
50              
51             # Methods that return a duration, but mutate during processing
52             for my $method (
53             qw(delta_md delta_days delta_ms subtract_datetime subtract_datetime_absolute)
54             )
55             {
56             Sub::Install::install_sub( {
57             code => sub {
58 5     5   2935 my $dt1 = DateTime->from_object( object => shift );
59 5         1716 my $dt2 = DateTime->from_object( object => shift );
60 5         1548 return $dt1->$method( $dt2, @_ );
61             },
62             as => $method,
63             } );
64             }
65              
66             # Need to replace today, as the DateTime
67 1     1 0 57 sub today { shift->now(@_)->trunc( to => 'day' ) }
68              
69             1;
70              
71             __END__