aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaarten Vanraes <alien@mageia.org>2016-03-21 20:39:35 +0100
committerMaarten Vanraes <alien@mageia.org>2016-03-21 20:59:27 +0100
commit8cd9e3aa7ae626f9b52e8979197a2611c3d36307 (patch)
tree9d8a7be17f9182d9c417c9dbb9f19002b1bbff93
parent65f5c20529cc20119c6a9e576c745f205e245328 (diff)
downloadmanatools-8cd9e3aa7ae626f9b52e8979197a2611c3d36307.tar
manatools-8cd9e3aa7ae626f9b52e8979197a2611c3d36307.tar.gz
manatools-8cd9e3aa7ae626f9b52e8979197a2611c3d36307.tar.bz2
manatools-8cd9e3aa7ae626f9b52e8979197a2611c3d36307.tar.xz
manatools-8cd9e3aa7ae626f9b52e8979197a2611c3d36307.zip
Actions Role uses an Action helper class
-rw-r--r--Changes1
-rw-r--r--MANIFEST1
-rw-r--r--lib/ManaTools/Shared/Action.pm111
-rw-r--r--lib/ManaTools/Shared/ActionsRole.pm58
4 files changed, 153 insertions, 18 deletions
diff --git a/Changes b/Changes
index 81cbbadb..8497f453 100644
--- a/Changes
+++ b/Changes
@@ -21,6 +21,7 @@
- Add a Properties GUI helper class
- Add an Actions Role
- Add a Properties Role
+ - Actions Role uses an Action helper class
1.1.1 Wed Feb 3 20:05:51 CET 2016
- manaclock - Added local NTP server setup mga#17569
1.1.0 Sun Jan 24 11:19:27 CET 2016
diff --git a/MANIFEST b/MANIFEST
index eacb4fee..471b2d07 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -69,6 +69,7 @@ lib/ManaTools/Rpmdragora/pkg.pm
lib/ManaTools/Rpmdragora/rpmnew.pm
lib/ManaTools/SettingsReader.pm
lib/ManaTools/Shared.pm
+lib/ManaTools/Shared/Action.pm
lib/ManaTools/Shared/ActionsRole.pm
lib/ManaTools/Shared/CommandLine.pm
lib/ManaTools/Shared/Firewall.pm
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 <alien@rmail.be>
+
+=head1 COPYRIGHT and LICENSE
+
+Copyright (c) 2015 Maarten Vanraes <alien@rmail.be>
+
+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;