File Coverage

blib/lib/Decision/Depends/State.pm
Criterion Covered Total %
statement 71 73 97.2
branch 25 32 78.1
condition 9 15 60.0
subroutine 21 22 95.4
pod 0 17 0.0
total 126 159 79.2


line stmt bran cond sub pod time code
1             # --8<--8<--8<--8<--
2             #
3             # Copyright (C) 2008 Smithsonian Astrophysical Observatory
4             #
5             # This file is part of Decision::Depends
6             #
7             # Decision-Depends is free software: you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation, either version 3 of the License, or (at
10             # your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program. If not, see .
19             #
20             # -->8-->8-->8-->8--
21              
22             package Decision::Depends::State;
23              
24             require 5.005_62;
25 11     11   61 use strict;
  11         127  
  11         442  
26 11     11   63 use warnings;
  11         21  
  11         435  
27              
28             ## no critic ( ProhibitAccessOfPrivateData )
29              
30 11     11   12519 use YAML qw();
  11         171264  
  11         273  
31 11     11   13882 use IO::File;
  11         197604  
  11         1740  
32 11     11   214 use Carp;
  11         35  
  11         17007  
33              
34             our $VERSION = '0.20';
35              
36             sub new
37             {
38 57     57 0 162 my $class = shift;
39 57   33     439 $class = ref($class) || $class;
40              
41 57         702 my $self = {
42             Attr => { Cache => 0,
43             DumpFiles => 0,
44             Pretend => 0,
45             Verbose => 0,
46             Force => 0,
47             File => undef
48             },
49             SLink => {},
50             Files => {},
51             Sig => {},
52             Var => {},
53             };
54              
55 57         253 bless $self, $class;
56              
57 57 50       309 my $attr = 'HASH' eq ref $_[-1] ? pop @_ : {} ;
58              
59 57         120 my ( $file ) = @_;
60              
61 57         348 $self->SetAttr( $attr );
62 57         221 $self->LoadState( );
63              
64 57         377 $self;
65             }
66              
67             sub SetAttr
68             {
69 95     95 0 369 my ( $self, $attr ) = @_;
70              
71 95 50       303 return unless defined $attr;
72              
73 95 50       410 croak( __PACKAGE__, '->SetAttr: attribute not a hash ref')
74             unless 'HASH' eq ref $attr;
75              
76 95         625 my @notok = grep { ! exists $self->{Attr}{$_} } keys %$attr;
  94         683  
77 95 50       330 croak( __PACKAGE__, '->SetAttr: unknown attribute(s): ',
78             join( ', ', @notok) ) if @notok;
79              
80 95         165 my ($key, $val);
81 95         1304 $self->{Attr}{$key} = $val while( ($key, $val) = each %$attr );
82 95 100       493 $self->{Attr}{Cache} = 1 if $self->{Attr}{Pretend};
83              
84 95 100       1001 $self->LoadState() if exists $attr->{File};
85             }
86              
87             sub LoadState
88             {
89 92     92 0 179 my ( $self ) = @_;
90              
91 92         382 my $file = $self->{Attr}{File};
92              
93 270         1996 my $state = defined $file && -f $file
94             ? YAML::LoadFile($file)
95 92 100 100     9251 : { map { $_ => {} } qw( Sig Var Files ) };
96              
97 92         41708 $self->{SLink} = {};
98 92         307 $self->{Sig} = $state->{Sig};
99 92         1523 $self->{Var} = $state->{Var};
100 92         614 $self->{Files} = $state->{Files};
101             }
102              
103             sub SaveState
104             {
105 27     27 0 57 my $self = shift;
106              
107 27 50 33     345 return if $self->{Attr}{Pretend} || !defined $self->{Attr}{File};
108              
109 27 50       449 YAML::DumpFile( $self->{Attr}{File},
    50          
110             {
111             Sig => $self->{Sig},
112             Var => $self->{Var},
113             Files =>
114             ( $self->{Attr}{DumpFiles} ? $self->{Files} : {} )
115             } )
116             or croak( __PACKAGE__,
117             "->SaveState: error writing state to $self->{Attr}{File}" );
118             }
119              
120             sub EraseState
121             {
122 3     3 0 26570 my $self = shift;
123              
124 3         75 $self->{$_} = {} foreach qw( SLink Files Sig Var );
125              
126             }
127              
128             sub DumpAll
129             {
130 0     0 0 0 my $self = shift;
131              
132 0         0 print STDOUT YAML::Store($self);
133             }
134              
135             sub Verbose
136             {
137 174     174 0 1115 $_[0]->{Attr}{Verbose};
138             }
139              
140             sub Force
141             {
142 38     38 0 426 $_[0]->{Attr}{Force};
143             }
144              
145             sub Pretend
146             {
147 15     15 0 172 $_[0]->{Attr}{Pretend};
148             }
149              
150             ######################################################################
151             # Status Files,
152              
153              
154             sub attachSLink
155             {
156 10     10 0 27 my ( $self, $file, $slink ) = @_;
157              
158 10 100       91 $self->{SLink}{$slink} = []
159             unless exists $self->{SLink}{$slink};
160              
161 10         23 push @{$self->{SLink}{$slink}}, $file;
  10         46  
162             }
163              
164             sub getSLinks
165             {
166 9     9 0 28 my ( $self, $file ) = @_;
167              
168 9 100       306 return exists $self->{SLink}{$file} ? $self->{SLink}{$file} : [$file];
169             }
170              
171              
172             ######################################################################
173             # File/Time routines
174              
175             sub getTime
176             {
177 63     63 0 145 my ( $self, $file ) = @_;
178              
179 63 100       2045 exists $self->{Files}{$file} ? $self->{Files}{$file}{time} : undef;
180             }
181              
182             sub setTime
183             {
184 56     56 0 129 my ( $self, $file, $time ) = @_;
185              
186 56 100       360 return unless $self->{Attr}{Cache};
187              
188 1         9 $self->{Files}{$file}{time} = $time;
189             }
190              
191              
192             ######################################################################
193             # Signature routines
194              
195             sub getSig
196             {
197 6     6 0 20 my ( $self, $target, $file ) = @_;
198              
199 6 100 66     95 ( exists $self->{Sig}{$target} && exists $self->{Sig}{$target}{$file} )
200             ? $self->{Sig}{$target}{$file} : undef;
201             }
202              
203             sub setSig
204             {
205 4     4 0 150 my ( $self, $target, $file, $sig ) = @_;
206              
207 4         32 $self->{Sig}{$target}{$file} = $sig;
208 4         98 $self->SaveState;
209             }
210              
211              
212             ######################################################################
213             # Variable routines
214              
215             sub getVar
216             {
217 28     28 0 50 my ( $self, $target, $var ) = @_;
218              
219             # return undef if we have no record of this variable
220 28 100 66     328 exists $self->{Var}{$target} && exists $self->{Var}{$target}{$var} ? $self->{Var}{$target}{$var} : undef;
221             }
222              
223              
224             sub setVar
225             {
226 23     23 0 273 my ( $self, $target, $var, $val ) = @_;
227              
228 23         136 $self->{Var}{$target}{$var} = $val;
229 23         771 $self->SaveState;
230             }
231              
232             1;