From 8cd9e3aa7ae626f9b52e8979197a2611c3d36307 Mon Sep 17 00:00:00 2001 From: Maarten Vanraes Date: Mon, 21 Mar 2016 20:39:35 +0100 Subject: Actions Role uses an Action helper class --- lib/ManaTools/Shared/Action.pm | 111 ++++++++++++++++++++++++++++++++++++ lib/ManaTools/Shared/ActionsRole.pm | 58 +++++++++++++------ 2 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 lib/ManaTools/Shared/Action.pm (limited to 'lib') diff --git a/lib/ManaTools/Shared/Action.pm b/lib/ManaTools/Shared/Action.pm new file mode 100644 index 00000000..34e968cf --- /dev/null +++ b/lib/ManaTools/Shared/Action.pm @@ -0,0 +1,111 @@ +# vim: set et ts=4 sw=4: +package ManaTools::Shared::Action; + +#============================================================= -*-perl-*- + +=head1 NAME + + ManaTools::Shared::Action - an Action helper class + +=head1 SYNOPSIS + + use ManaTools::Shared::Action; + + my $action = ManaTools::Shared::Action->new(name => 'addPartition', label => 'Add a partition', item => $foo, action => sub { + my $self = shift; + my $item = $self->item(); + my @args = @_; + ... + return 'return value'; + }, valid => sub { + my $self = shift; + my $item = $self->item(); + ... + return 0; + }); + my $res = $action->act(@_); + $action->is_valid(); + + +=head1 DESCRIPTION + + This helper class is used to abstract an action + +=head1 SUPPORT + + You can find documentation for this plugin with the perldoc command: + + perldoc ManaTools::Shared::Action + + +=head1 AUTHOR + + Maarten Vanraes + +=head1 COPYRIGHT and LICENSE + +Copyright (c) 2015 Maarten Vanraes + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2, as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +=head1 METHODS + +=cut + +use Moose; + +has 'name' => ( + is => 'ro', + isa => 'Str', + required => 1 +); + +has 'label' => ( + is => 'rw', + isa => 'Str', + required => 1 +); + +has 'item' => ( + is => 'rw', + isa => 'Item', + required => 0, + default => undef +); + +has 'action' => ( + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + required => 1, + handles => { + act => 'execute' + } +); + +has 'valid' => ( + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + required => 0, + default => sub { + return sub { return 1; }; + }, + handles => { + is_valid => 'execute' + } +); + +1; + diff --git a/lib/ManaTools/Shared/ActionsRole.pm b/lib/ManaTools/Shared/ActionsRole.pm index 7b6829d7..888750b3 100644 --- a/lib/ManaTools/Shared/ActionsRole.pm +++ b/lib/ManaTools/Shared/ActionsRole.pm @@ -18,9 +18,9 @@ package ManaTools::Shared::ActionsRole; my $f = Foo->new(); my @actionnames = $f->get_actions(); - $f->add('aname', sub { my @params = @_; ... ; return 'foo'; }); + $f->add_action('aname', 'a label', $item, sub { my $self = shift; my @params = @_; ... ; return 'foo'; }, sub { return 1; }); my $res = $f->act('aname', 'param1', 'param2', @params); - $f->remove('aname'); + $f->remove_action('aname'); =head1 DESCRIPTION @@ -61,13 +61,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use Moose::Role; -## Class DATA -has 'actions' => ( +use MooseX::ClassAttribute; +use ManaTools::Shared::Action; + +class_has 'acts' => ( is => 'ro', init_arg => undef, lazy => 1, - isa => 'HashRef[CodeRef]', - default => sub {return {};} + isa => 'ArrayRef[ManaTools::Shared::Action]', + default => sub {return [];} ); #============================================================= @@ -88,7 +90,7 @@ has 'actions' => ( sub get_actions { my $self = shift; - return keys %{$self->actions}; + return map { return $_->name()} @{$self->acts()}; } #============================================================= @@ -114,17 +116,26 @@ sub get_actions { sub act { my $self = shift; my $key = shift; - return $self->actions->{$key}(@_); + my $acts = $self->acts(); + for my $action (@{$self->acts()}) { + if ($key == $action->name()) { + return $action->act($self, @_); + } + } + + return undef; } #============================================================= -=head2 add +=head2 add_action =head3 INPUT - $key: string - $action: sub + $name: Str + $label: Str + $action: CodeRef + $valid: CodeRef =head3 DESCRIPTION @@ -133,17 +144,22 @@ sub act { =cut #============================================================= -sub add { +sub add_action { my $self = shift; - my $key = shift; + my $name = shift; + my $label = shift; + my $item = shift; my $action = shift; + my $valid = shift; + my $options = {name => $name, label => $label, item => $item, action => $action}; + $options->{valid} = $valid if defined $valid; - $self->actions->{$key} = $action; + push @{$self->acts()}, ManaTools::Shared::Action->new($options); } #============================================================= -=head2 remove +=head2 remove_action =head3 INPUT @@ -156,11 +172,17 @@ sub add { =cut #============================================================= -sub remove { +sub remove_action { my $self = shift; my $key = shift; - - delete $self->actions->{$key}; + my $acts = $self->acts(); + my $index = scalar(@{$acts}); + while ($index > 0) { + $index = $index - 1; + if ($acts->[$index]->name() == $key) { + delete $acts->[$index]; + } + } } 1; -- cgit v1.2.1