GitLab-API-v4

 view release on metacpan or  search on metacpan

lib/GitLab/API/v4/Mock/Engine.pm  view on Meta::CPAN

    my $next_id = $self->next_ids->{$for} || 1;
    $self->next_ids->{$for} = $next_id + 1;

    return $next_id;
}

=head1 USER METHODS

=head2 user

    my $user = $engine->user( $id );

Returns a user hash reference for the given ID.

If no user is found with the ID then C<undef> is returned.

=cut

sub user {
    my ($self, $id) = @_;

    foreach my $user (@{ $self->users() }) {
        return $user if $user->{id} == $id;
    }

    return undef;
}

=head2 create_user

    my $user = $engine->create_user( $user );
    my $id = $user->{id};

Takes a user hash reference, sets the C<id> field, and stores it in
L</users>.

Returns the updated user hash reference.

=cut

sub create_user {
    my ($self, $user) = @_;

    $user->{id} = $self->next_id_for( 'user' );
    push @{ $self->users() }, $user;

    return $user;
}

=head2 update_user

    my $user = $engine->update_user( $id, $new_user_data );

Takes the ID of the user to update and a hash reference of fields
to update.

Returns the updated user hash reference.

=cut

sub update_user {
    my ($self, $id, $data) = @_;

    my $user = $self->user( $id );
    return undef if !$user;

    %$user = (
        %$user,
        %$data,
    );

    return $user;
}

=head2 delete_user

    my $user = $engine->delete_user( $id );

Deletes the user with the specified ID from L</users>.

If no user is found with the ID then C<undef> is returned.
Otherwise the user hash reference is returned.

=cut

sub delete_user {
    my ($self, $id) = @_;

    my $users = $self->users();

    my @new;
    my $found_user;
    foreach my $user (@$users) {
        if ($user->{id} == $id and !$found_user) {
            $found_user = $user;
            next;
        }
        push @new, $user;
    }

    return undef if !$found_user;

    @$users = @new;

    return $found_user;
}

1;
__END__

=head1 SUPPORT

See L<GitLab::API::v4/SUPPORT>.

=head1 AUTHORS

See L<GitLab::API::v4/AUTHORS>.

=head1 LICENSE

See L<GitLab::API::v4/LICENSE>.



( run in 1.245 second using v1.01-cache-2.11-cpan-483215c6ad5 )