Last active
December 18, 2015 12:59
-
-
Save Timbus/5787018 to your computer and use it in GitHub Desktop.
Tardy attributes. Fashionably late defaults.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment