diff options
Diffstat (limited to 'lib/IM/Engine/Plugin/Commands/Trait/Attribute/Command.pm')
-rw-r--r-- | lib/IM/Engine/Plugin/Commands/Trait/Attribute/Command.pm | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/IM/Engine/Plugin/Commands/Trait/Attribute/Command.pm b/lib/IM/Engine/Plugin/Commands/Trait/Attribute/Command.pm new file mode 100644 index 0000000..bd345dd --- /dev/null +++ b/lib/IM/Engine/Plugin/Commands/Trait/Attribute/Command.pm @@ -0,0 +1,62 @@ +package IM::Engine::Plugin::Commands::Trait::Attribute::Command; +use Moose::Role; + +has command => ( + is => 'rw', + isa => 'Bool', + default => 0, +); + +has needs_init => ( + is => 'rw', + isa => 'Bool', + default => 1, +); + +has _use_accessor_metatrait => ( + is => 'rw', + isa => 'Bool', + default => 0, +); + +before _process_options => sub { + my $self = shift; + my ($name, $options) = @_; + Carp::cluck("needs_init is useless for attributes without command") + if exists($options->{needs_init}) && !$options->{command}; +}; + +around accessor_metaclass => sub { + my $orig = shift; + my $self = shift; + my $metaclass = $self->$orig(@_); + return $metaclass unless $self->command && $self->_use_accessor_metatrait; + return Moose::Meta::Class->create_anon_class( + superclasses => [$metaclass], + roles => ['IM::Engine::Plugin::Commands::Trait::Method::Command'], + cache => 1, + )->name; +}; + +after install_accessors => sub { + my $self = shift; + return unless $self->command; + my $method_meta = $self->get_read_method_ref; + $method_meta->pass_args(0); + $method_meta->needs_init($self->needs_init); +}; + +around _process_accessors => sub { + my $orig = shift; + my $self = shift; + my ($type) = @_; + $self->_use_accessor_metatrait(1) if $type eq 'accessor' + || $type eq 'reader'; + my @ret = $self->$orig(@_); + $self->_use_accessor_metatrait(0); + return @ret; +}; + +no Moose::Role; + +1; |