Schedule-Activity
view release on metacpan or search on metacpan
lib/Schedule/Activity/Attribute.pm view on Meta::CPAN
package Schedule::Activity::Attribute;
use strict;
use warnings;
use Ref::Util qw/is_hashref is_ref/;
our $VERSION='0.3.0';
my %types=(
int=>{
change =>\&_changeInt,
average=>\&_avgInt,
changes=>{map {$_=>undef} qw/type value set incr decr tm note/},
},
bool=>{
change =>\&_changeBool,
average=>\&_avgBool,
changes=>{map {$_=>undef} qw/type value set tm note/},
},
);
sub new {
my ($ref,%opt)=@_;
my $class=is_ref($ref)||$ref;
my ($tm,$y)=($opt{tm}//0,$opt{value}//0);
my %self=(
type =>$opt{type}//'int',
value=>$y,
log =>{$tm=>$y},
aog =>{$tm=>$y},
tmmax=>$tm,
avg =>$y,
tmsum=>0,
);
if(!defined($types{$self{type}})) { die "Attribute invalid type: $self{type}" }
return bless(\%self,$class);
}
sub validateConfig {
my ($self,%opt)=@_;
my $C=$types{$$self{type}}{changes};
my @errors=grep {!exists($$C{$_})} keys %opt;
if(@errors) { return "Invalid attribute options/commands: ".join(' ',@errors) }
return;
}
sub log {
my ($self,$tm)=@_;
if(defined($tm)&&($tm>=$$self{tmmax})) { $$self{log}{$tm}=$$self{value}; $$self{aog}{$tm}=$$self{avg}//$$self{value}; $$self{tmmax}=$tm }
# historic entry is not currently supported
return $self;
}
sub change {
my ($self,%opt)=@_;
my $tm=$opt{tm}//$$self{tmmax};
if($tm<$$self{tmmax}) { return $self } # historic entry is not currently supported
#
&{$types{$$self{type}}{change}}($self,%opt);
$self->log($tm); # updates tmmax
if(!defined($$self{avg})) { $self->average() }
return $self;
}
sub report {
( run in 1.820 second using v1.01-cache-2.11-cpan-39bf76dae61 )