package DateTimeX::Role::Immutable;

# ABSTRACT: A role that can be composed into a DateTime subclass to make it immutable

use strict;
use warnings;
use Carp;
use Role::Tiny;
use Sub::Install;
use DateTime;
use namespace::autoclean;

our $VERSION = '0.36';

my %mutators = (
    add_duration      => 'plus_duration',
    subtract_duration => 'minus_duration',
    add               => 'plus',
    subtract          => 'minus',
    truncate          => 'trunc',
    set               => 'with_component',
    set_year          => 'with_year',
    set_month         => 'with_month',
    set_day           => 'with_day',
    set_hour          => 'with_hour',
    set_minute        => 'with_minute',
    set_second        => 'with_second',
    set_nanosecond    => 'with_nanosecond',
## Consider: set_time_zone set_locale set_formatter

before keys %mutators => sub {
    my $orig = shift;
    my $self = shift;

    croak "Attempted to mutate a DateTime object";

while ( my ( $orig_method, $new_method ) = each %mutators ) {
    Sub::Install::install_sub( {
            code => sub {
                my $self = shift;
                my $new_dt = DateTime->from_object( object => $self );
                return bless $new_dt, ref $self;
            as => $new_method,
        } );

# Methods that return a duration, but mutate during processing
for my $method (
    qw(delta_md delta_days delta_ms subtract_datetime subtract_datetime_absolute)
    Sub::Install::install_sub( {
            code => sub {
                my $dt1 = DateTime->from_object( object => shift );
                my $dt2 = DateTime->from_object( object => shift );
                return $dt1->$method( $dt2, @_ );
            as => $method,
        } );

# Need to replace today, as the DateTime
sub today { shift->now(@_)->trunc( to => 'day' ) }




=head1 NAME

DateTimeX::Role::Immutable - A role that can be composed into a DateTime subclass to make it immutable

=head1 VERSION

version 0.36


    package Your::DateTime;
    use base 'DateTime';
    use Role::Tiny::With;
    with 'DateTimeX::Role::Immutable';


This is role that can be composed into a L<DateTime> subclass to make those
objects immutable. The C<set> methods are replaced with new methods that leave
the original object untouched, and return a new DateTime object with the
expected changes.

The following methods now thrown an exception:


and are replaced by these methods which return the changed value:


At the moment, C<set_time_zone>, C<set_locale>, and C<set_formatter> continue
to act as mutators. DateTime uses these internally and changing them creates
unexpected behavior. These methods also do not really change the time value.

See L<DateTime> for the rest of the documentation.

=head1 SEE ALSO

L<DateTimeX::Role::Immutable>, L<DateTime>, L<DateTime::Moonpig>,

=head1 AUTHOR

Mark Grimes, E<lt>mgrimes@cpan.orgE<gt>


This software is copyright (c) 2015 by Mark Grimes, E<lt>mgrimes@cpan.orgE<gt>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.