Boorstein, Daniel B wrote:
I realize this is my problem since I broke encapsulation, but I'm
wondering if others have dealt with this already. Is there a designated
means for derived classes to add custom instance data? I've perused the
docs, but didn't see anything specific for authors of derived classes.

There's been a few times where I wanted to hang data off a DT, especially for DT::Event modules

I'd suggest we have a purpose built $self->{data} for hanging such stuff and make sure that anything in there is preserved across mutations and clonings.

There was talk ages ago about life span controls however:

  $dt = $easter;

  print $dt->{data}{EventName};
  # Easter Sunday

  $dt->add( days => 4 );

  print $dt->{data}{EventName};
  # Easter Sunday

So now it's incorrect. Four days after Easter isn't Easter any more, so the EventName shouldn't survive any 'add' that takes it past midnight, but it should survive any 'add' that leaves it still on the same 'day'

How do you do that?
* One option is to ignore such things and just leave the data hanging around. If someone changes the date, it's their own damned fault. * Another option would be a callback class method and use $self->{data}{callback} to store an arrayref of packages that want a callback on any mutation:

sub add_duration {
  ...
  ...
  foreach( @{ $self->{data}{callback} } ){
    $_->callback( $self );
  }
}

Then in DateTime::Event::Easter, there's a sub callback that checks the data in the object and adjusts, deletes etc., as appropriate

print Dumper($dt->{data});
# { callback => [ 'DateTime::Event::Easter' ], initial_date => '2007-04-08', event_name => 'Easter Sunday' }

package DateTime::Event::Easter;
sub callback {
        if ($dt->ymd('-') ne $dt->{data}{initial_date}){
                delete $dt->{data}{initial_date};
                delete $dt->{data}{event_name};
                $dt->{data}{callback} = [
                        grep { $_ ne 'DateTime::Format::Easter' }
                                @{$dt->{data}{callback}}
                ];
        }
}

__END__

Just a thought on how we can do such things.

Cheers!
Rick Measham

Reply via email to