Schedule-Activity
view release on metacpan or search on metacpan
lib/Schedule/Activity/NodeFilter.pm view on Meta::CPAN
package Schedule::Activity::NodeFilter;
use strict;
use warnings;
use Ref::Util qw/is_plain_hashref is_ref/;
our $VERSION='0.3.0';
my %property=map {$_=>undef} qw/f attr op value boolean filters mod/;
my %matcher=(
boolean=>\&matchBoolean,
elapsed=>\&matchElapsed,
value =>\&matchValue,
avg =>\&matchValue,
);
sub new {
my ($ref,%opt)=@_;
my $class=is_ref($ref)||$ref;
my %self=map {$_=>$opt{$_}} grep {exists($opt{$_})} keys(%property);
if($self{attr}) { $self{f}//='value' }
if($self{boolean}) { $self{f}='boolean'; $self{boolean}=lc($self{boolean}) }
if(!defined($matcher{$self{f}})) { die "Invalid filter function $self{f}" }
return bless(\%self,$class);
}
sub matches {
my ($self,$tm,%attributes)=@_;
return &{$matcher{$$self{f}}}($self,$tm,%attributes);
}
sub matchBoolean {
my ($self,$tm,%attributes)=@_;
if($$self{boolean} eq 'and') {
my $res=1;
foreach my $filter (@{$$self{filters}}) {
if(is_plain_hashref($filter)) { $res&&=__PACKAGE__->new(%$filter)->matches($tm,%attributes) }
else { $res&&=$filter->matches($tm,%attributes) }
if(!$res) { return 0 }
}
return $res;
}
if($$self{boolean} eq 'or') {
my $res=0;
foreach my $filter (@{$$self{filters}}) {
if(is_plain_hashref($filter)) { $res||=__PACKAGE__->new(%$filter)->matches($tm,%attributes) }
else { $res||=$filter->matches($tm,%attributes) }
if($res) { return 1 }
}
return $res;
}
if($$self{boolean} eq 'nand') {
my $res=0;
foreach my $filter (@{$$self{filters}}) {
if(is_plain_hashref($filter)) { $res||=!__PACKAGE__->new(%$filter)->matches($tm,%attributes) }
else { $res||=!$filter->matches($tm,%attributes) }
if($res) { return 1 }
}
return $res;
}
return 0;
}
sub matchElapsed {
my ($self,$tm,%attributes)=@_;
( run in 0.648 second using v1.01-cache-2.11-cpan-39bf76dae61 )