From 4c780314dace2294af648228a544fdf50c5b6afb Mon Sep 17 00:00:00 2001 From: Maarten Vanraes Date: Tue, 10 May 2016 20:07:15 +0200 Subject: actions also have a level --- lib/ManaTools/Shared/Action.pm | 41 +++++++++++++++++++++++++++++++++++++ lib/ManaTools/Shared/ActionsRole.pm | 13 +++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/ManaTools/Shared/Action.pm b/lib/ManaTools/Shared/Action.pm index 34e968cf..1c211296 100644 --- a/lib/ManaTools/Shared/Action.pm +++ b/lib/ManaTools/Shared/Action.pm @@ -65,6 +65,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use Moose; +use Moose::Util::TypeConstraints; +use MooseX::ClassAttribute; + has 'name' => ( is => 'ro', isa => 'Str', @@ -107,5 +110,43 @@ has 'valid' => ( } ); +subtype 'LevelType' + => as Int + => where {($_ > 0 && $_ <= 3)}; + +has 'level' => ( + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + required => 0, + default => sub { + return sub { return 1; }; + }, + handles => { + is_level => 'execute' + } +); + +class_has 'beginnerLevel' => ( + is => 'ro', + isa => 'LevelType', + init_arg => undef, + default => sub {return 1;}, +); + +class_has 'advancedLevel' => ( + is => 'ro', + isa => 'LevelType', + init_arg => undef, + default => sub {return 2;}, +); + +class_has 'expertLevel' => ( + is => 'ro', + isa => 'LevelType', + init_arg => undef, + default => sub {return 3;}, +); + 1; diff --git a/lib/ManaTools/Shared/ActionsRole.pm b/lib/ManaTools/Shared/ActionsRole.pm index ba4ed8f1..30f3789d 100644 --- a/lib/ManaTools/Shared/ActionsRole.pm +++ b/lib/ManaTools/Shared/ActionsRole.pm @@ -72,6 +72,14 @@ class_has 'acts' => ( default => sub {return [];} ); +has 'level' => ( + is => 'rw', + init_arg => undef, + lazy => 1, + isa => 'LevelType', + default => sub {return ManaTools::Shared::Action->beginnerLevel;} +); + #============================================================= =head2 get_actions @@ -89,8 +97,9 @@ class_has 'acts' => ( #============================================================= sub get_actions { my $self = shift; + my $level = $self->level(); - return map { return $_->name()} @{$self->acts()}; + return map { return $_->name()} grep { $_->is_valid($_) && ($_->is_level($_) <= $level) } @{$self->acts()}; } #============================================================= @@ -151,8 +160,10 @@ sub add_action { my $item = shift; my $action = shift; my $valid = shift; + my $level = shift; my $options = {name => $name, label => $label, item => $item, action => $action}; $options->{valid} = $valid if defined $valid; + $options->{level} = $level if defined $level; push @{$self->acts()}, ManaTools::Shared::Action->new($options); } -- cgit v1.2.1