Skip to content

Instantly share code, notes, and snippets.

@Timbus
Last active December 18, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Timbus/5787018 to your computer and use it in GitHub Desktop.
Save Timbus/5787018 to your computer and use it in GitHub Desktop.
Tardy attributes. Fashionably late defaults.
{ 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