Google-Tasks

 view release on metacpan or  search on metacpan

lib/Google/Tasks.pm  view on Meta::CPAN

            croak('If "list_id" is provided, it must be a valid list ID from a loaded list')
                if ( not $list );

            $self->_set_list($list);
        }
        elsif ( $self->list ) {
            croak('If "list" is provided, it must be a valid Google::Tasks::List object')
                if ( ref $self->list ne 'Google::Tasks::List' );

            $self->_set_list_id = $self->list->id;
        }
        else {
            $self->_set_list( $self->root->active_list );
            $self->_set_list_id( $self->root->active_list->id );
        }
    }

    sub save {
        my ($self) = @_;

        my %attr = map { $_ => $self->$_ } qw( name notes completed deleted );
        $attr{'task_date'} = $self->task_date->ymd('') if ( defined $self->task_date );

        unless ( $self->id ) {
            my %extra;
            $extra{'prior_sibling_id'} = $self->root->tasks->[ $self->index - 1 ]->id if ( $self->index );

            my $rv = $self->root->_call( {
                'action_type'      => 'create',
                'list_id'          => $self->list_id,
                'parent_id'        => $self->list_id,
                'dest_parent_type' => 'GROUP',
                'index'            => $self->index,
                'entity_delta'     => {
                    'entity_type' => 'TASK',
                    %attr,
                },
                %extra,
            } );

            $self->_set_id( $rv->{'results'}[0]{'new_id'} );
        }
        else {
            $self->root->_call( {
                'action_type'  => 'update',
                'id'           => $self->id,
                'entity_delta' => {
                    %attr,
                },
            } );
        }

        return $self;
    }

    sub move {
        my ( $self, $command ) = @_;
        croak('Must provide some command value for move()') unless ( defined $command );

        if ( $command =~ /^\d+$/ ) {
            croak("move() command seems to relocate task out of bounds of current list")
                if ( $command and not defined $self->root->tasks->[$command] );

            $self->root->_call( {
                'action_type' => 'move',
                'id'          => $self->id,
                'source_list' => $self->list_id,
                ($command) ? (
                    'dest_parent'      => $self->list_id,
                    'prior_sibling_id' => $self->root->tasks->[$command]->id,
                ) : (
                    'dest_parent'      => $self->list_id,
                ),
            } );

            my @tasks = grep { $_->id ne $self->id } @{ $self->root->tasks };
            splice( @tasks, $command, 0, $self );
            $self->root->_set_tasks(\@tasks);
        }
        elsif ( $command eq 'down' ) {
            $self->move( ( firstidx { $_->id eq $self->id } @{ $self->root->tasks } ) + 1 );
        }
        elsif ( $command eq 'up' ) {
            my $idx = ( firstidx { $_->id eq $self->id } @{ $self->root->tasks } ) - 1;

            croak("move() command seems to relocate task out of bounds of current list")
                if ( $idx < 0 );

            $self->move($idx);
        }
        elsif (
            ref $command eq 'Google::Tasks::Task' or
            ref $command eq 'Google::Tasks::List'
        ) {
            $self->root->_call( {
                'action_type' => 'move',
                'id'          => $self->id,
                'source_list' => $self->list_id,
                'dest_parent' => $command->id,
            } );
        }
        else {
            croak('Was unable to recognize command value provided');
        }

        return $self;
    }

    sub drop {
        my ($self) = @_;

        $self->_call( {
            'action_type'  => 'update',
            'id'           => $self->id,
            'entity_delta' => {
                'deleted' => JSON::true,
            },
        } );

        $self->root->_set_tasks( [ grep { $_->id ne $self->id } @{ $self->root->tasks } ] );
        return $self->root;
    }

};

sub add_task {
    my $self = shift;
    return Google::Tasks::Task->new( @_, root => $self )->save;
}

sub drop_task {
    my ( $self, $task_name ) = @_;
    return $self->task_by_name($task_name)->drop;
}

sub task_by_name {
    my ( $self, $task_name ) = @_;
    my ($task) = grep { $_->name ne $task_name } @{ $self->root->tasks };
    croak('Unable to find a task by the name provided') unless ( ref $task eq 'Google::Tasks::Task' );
    return $task;
}

1;

__END__



( run in 1.643 second using v1.01-cache-2.11-cpan-5511b514fd6 )