App-JobLog
view release on metacpan or search on metacpan
lib/App/JobLog/Vacation.pm view on Meta::CPAN
package App::JobLog::Vacation;
$App::JobLog::Vacation::VERSION = '1.042';
# ABSTRACT: controller for the vacation model
use Modern::Perl;
use App::JobLog::Vacation::Period;
use App::JobLog::Config qw(
vacation
init_file
);
use Carp qw(carp);
use FileHandle;
sub new {
my $class = shift;
$class = ref $class if ref $class;
my $self = bless { changed => 0 }, $class;
if ( -e vacation ) {
my $fh = FileHandle->new(vacation);
my @data;
while ( my $line = <$fh> ) {
chomp $line;
my $v = App::JobLog::Vacation::Period->parse($line);
push @data, $v;
}
$self->{data} = [ sort { $a->cmp($b) } @data ];
}
return $self;
}
sub periods { @{ $_[0]->{data} || [] } }
sub close {
my ($self) = @_;
if ( $self->{changed} ) {
$self->{changed} = 0;
if ( @{ $self->{data} } ) {
# something to save
init_file(vacation) unless -e vacation;
my $fh = FileHandle->new( vacation, 'w' );
for my $v ( @{ $self->{data} } ) {
print $fh $v, "\n";
}
$fh->close;
}
elsif ( -e vacation ) {
unlink(vacation);
}
}
}
# make sure changes are written to the file
sub DESTROY {
my ($self) = @_;
$self->close if $self->{changed};
}
sub add {
my ( $self, %opts ) = @_;
my ( $end, $type, $repeats ) = @opts{qw(end type repeats)};
( run in 0.684 second using v1.01-cache-2.11-cpan-39bf76dae61 )