File Coverage

blib/lib/Schedule/Activity/Attributes.pm
Criterion Covered Total %
statement 69 74 93.2
branch 7 12 58.3
condition 2 6 33.3
subroutine 12 12 100.0
pod 0 8 0.0
total 90 112 80.3


line stmt bran cond sub pod time code
1             package Schedule::Activity::Attributes;
2              
3 3     3   12177 use strict;
  3         7  
  3         142  
4 3     3   19 use warnings;
  3         6  
  3         251  
5 3     3   1260 use Ref::Util qw/is_ref/;
  3         6097  
  3         327  
6 3     3   1884 use Schedule::Activity::Attribute;
  3         12  
  3         2785  
7              
8             our $VERSION='0.3.0';
9              
10             sub new {
11 82     82 0 783691 my ($ref,%opt)=@_;
12 82   33     348 my $class=is_ref($ref)||$ref;
13 82         355 my %self=(
14             attr =>{},
15             stack=>[],
16             );
17 82         411 return bless(\%self,$class);
18             }
19              
20             sub register {
21 242     242 0 676 my ($self,$attribute,%opt)=@_;
22 242         369 my @errors;
23 242 100       855 if(defined($$self{attr}{$attribute})) {
24 130 50 33     420 if($opt{type}&&($$self{attr}{$attribute}{type} ne $opt{type})) {
25 0         0 push @errors,"Attribute conflicting types: $attribute" }
26 130         420 return @errors;
27             }
28 112         277 eval {
29 112         602 $$self{attr}{$attribute}=Schedule::Activity::Attribute->new(%opt);
30 112         472 push @errors,$$self{attr}{$attribute}->validateConfig(%opt);
31             };
32 112 50       319 if($@) { push @errors,$@ }
  0         0  
33 112         358 return @errors;
34             }
35              
36             # sub get { my ($self,$attribute)=@_; ... } # not yet needed
37              
38             sub log {
39 184093     184093 0 381574 my ($self,$tm)=@_;
40 184093 50       417726 if(!defined($tm)) { return $self }
  0         0  
41 184093         277411 foreach my $A (values %{$$self{attr}}) { $A->change(tm=>$tm,_log=>1) }
  184093         507943  
  311715         862704  
42 184093         448078 return $self;
43             }
44              
45             sub change {
46 408780     408780 0 1091443 my ($self,$attribute,%opt)=@_;
47 408780         838393 my $A=$$self{attr}{$attribute};
48 408780 50       883892 if(!$A) { return }
  0         0  
49 408780         1393623 $A->change(%opt);
50 408780         1330035 return $self;
51             }
52              
53             sub report {
54 19795     19795 0 53999 my ($self)=@_;
55 19795         38618 my %res;
56 19795         37921 while(my ($k,$v)=each %{$$self{attr}}) { %{$res{$k}}=$v->report() }
  35768         99517  
  35768         179388  
  55563         191020  
57 19795         78469 return %res;
58             }
59              
60             sub reset {
61 2839     2839 0 5895 my ($self)=@_;
62 2839         4809 foreach my $attr (values %{$$self{attr}}) { $attr->reset() }
  2839         7623  
  2839         8729  
63 2839         6336 return $self;
64             }
65              
66             sub push {
67 222760     222760 0 426512 my ($self)=@_;
68 222760         343590 my %state;
69 222760         356642 while(my ($k,$v)=each %{$$self{attr}}) { %{$state{$k}}=$v->dump() }
  385336         1089664  
  385336         1944177  
  608096         1936299  
70 222760         390428 push @{$$self{stack}},\%state;
  222760         578234  
71 222760         530639 return $self;
72             }
73              
74             sub pop {
75 205796     205796 0 401819 my ($self)=@_;
76 205796 50       303608 if(!@{$$self{stack}}) { return $self }
  205796         566313  
  0         0  
77 205796         315917 my %state=%{pop @{$$self{stack}}};
  205796         311126  
  205796         741036  
78 205796         577311 %{$$self{attr}}=();
  205796         1559946  
79 205796         675973 while(my ($k,$v)=each %state) { $$self{attr}{$k}=Schedule::Activity::Attribute->restore(%$v) }
  352444         1457009  
80 205796         1538618 return $self;
81             }
82              
83             1;
84              
85             __END__