App::HWD::Work - Work completed on HWD projects


hwd documentation Contained in the hwd distribution.

Index


Code Index:

NAME

Top

App::HWD::Work - Work completed on HWD projects

SYNOPSIS

Top

Used only by the hwd application.

Note that these functions are pretty fragile, and do almost no data checking.

FUNCTIONS

Top

App::HWD::Work->parse()

Returns an App::HWD::Work object from an input line

App::HWD::Work->new( { args } )

Creates a new task from the args passed in. They should include at least level, name and id, even if id is undef.

$work->set( $key => $value )

Sets the $key field to $value.

$work->who()

Returns who did the work

$work->when()

Returns the when of the work as a string.

$work->when_obj()

Returns the when of the work as a DateTime object.

$work->task()

Returns the ID of the work that was worked on.

$work->hours()

Returns the hours spent.

$work->completed()

Returns a boolean that says whether the work was completed or not.

$work->comment()

Returns the comment from the file, if any.

AUTHOR

Top

Andy Lester, <andy at petdance.com>

COPYRIGHT & LICENSE

Top


hwd documentation Contained in the hwd distribution.
package App::HWD::Work;

use warnings;
use strict;
use DateTime::Format::Strptime;

sub parse {
    my $class = shift;
    my $line = shift;

    my @cols = split " ", $line, 5;
    die "Invalid work line: $line" unless @cols >= 4;

    my ($who, $when, $task, $hours, $comment) = @cols;
    my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
    $when = $parser->parse_datetime( $when );
    my $completed;
    if ( defined $comment ) {
        if ( $comment =~ s/\s*X\s*//i ) {
            $completed = 1;
        }
        $comment =~ s/^#\s*//;
        $comment =~ s/\s+$//;
    }
    else {
        $comment = '';
    }
    if ( $hours =~ s/h$// ) {
        # nothing
    }
    elsif ( $hours =~ /^(\d+)m$/ ) {
        $hours = $1/60;
    }
    elsif ( $hours != $hours+0 ) {
        die "Invalid hours: $hours\n";
    }

    die "Invalid task: $task\n" unless ($task =~ /^\d+$/ || $task eq "^");

    my $self =
        $class->new( {
            who => $who,
            when => $when,
            task => $task,
            hours => $hours,
            comment => $comment,
            completed => $completed,
        } );

    return $self;
}

sub new {
    my $class = shift;
    my $args = shift;

    my $self = bless { %$args }, $class;
}


sub set {
    my $self = shift;
    my $key = shift;
    my $value = shift;

    die "Dupe key $key" if exists $self->{$key};
    $self->{$key} = $value;
}

sub who         { return shift->{who} }
sub task        { return shift->{task} }
sub hours       { return shift->{hours} }
sub completed   { return shift->{completed} || 0 }
sub comment     { return shift->{comment} }
sub when_obj    { return shift->{when} }
sub when {
    my $self = shift;

    my $obj = $self->{when} or return '';

    return $obj->strftime( "%F" );
}

1; # End of App::HWD::Task