public
Last active

Tardy attributes. Fashionably late defaults.

  • Download Gist
Late.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
{ package Late;
use Moose ();
use Moose::Exporter;
Moose::Exporter->setup_import_methods(
class_metaroles => {
class => ['Late::MetaRole::Class'],
attribute => ['Late::MetaRole::Attribute'],
},
);
1;}
 
{ package Late::MetaRole::Class;
 
use namespace::autoclean;
use Moose::Role;
 
##INLINED CONSTRUCTOR
around '_inline_new_object' => sub {
my ( $method, $self ) = ( shift, shift );
 
my @ret = $self->$method(@_);
 
splice @ret, -1, 0,
map {'$instance->meta->get_attribute(\''.$_->name.'\')->get_value($instance);'}
grep {$_->does('Late::MetaRole::Attribute') && $_->is_late}
$self->get_all_attributes;
 
return @ret;
};
 
##NOT.. INLINED CONSTRUCTOR
around 'new_object' => sub {
my ( $method, $self ) = ( shift, shift );
 
my $class = $self->$method(@_);
 
$_->get_value($class)
for grep {$_->does('Late::MetaRole::Attribute') && $_->is_late}
$self->get_all_attributes;
return $class;
};
1;}
 
{ package Late::MetaRole::Attribute;
use namespace::autoclean;
use Moose::Role;
 
has 'late' => ( is => 'ro', predicate => 'is_late' );
 
##LATE ITEMS ARE LAZY
before '_process_options' => sub {
my ( $class, $name, $options ) = @_;
$options->{lazy} = 1 if $options->{late};
};
1;}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.