package Games::WebPaper::Team::Database;

use warnings;
use strict;
use Fcntl;
use MLDBM qw/DB_File Storable/;

=head1 NAME

Games::WebPaper::Team::Database - Takes track of teams

=cut
our $VERSION = '0.01';

=head1 Synopsis

    use Games::WebPaper::Team::Database;

    my $team = Games::WebPaper::Team->new("file.db");

=head1 Functions

=head2 new

Creates a new empty database for teams. You must supply an argument,
the database filename to be used. It will rewrite the file if it
exists.

=cut

sub new {
  my ($class, $filename) = @_;
  return undef unless defined $filename and $filename;

  my $self = bless({filename=>$filename}, $class);
  tie %{$self->{db}}, 'MLDBM', $filename, O_CREAT|O_RDWR, 0640 or die "$! (file is $filename)";
  return $self;
}

=head2 open

Opens a team database given a database name;

=cut

sub open {
  my ($class, $filename) = @_;
  return undef unless defined $filename and $filename;

  my $self = bless({filename=>$filename}, $class);
  tie %{$self->{db}}, 'MLDBM', $filename, O_RDWR, 0640 or die "$! (file is $filename)";
  return $self;
}

=head2 addteam

Adds a Games::WebPaper::Team to the database;

=cut

sub addteam {
  my ($self,$team) = @_;

  return undef unless defined $team and ref($team) eq "Games::WebPaper::Team";

  return undef if exists ($self->{db}{lc($team->teamname)});

  $self->{db}{lc($team->teamname)} = $team;
  return $team->teamname;
}

=head2 close

Saves and unties the database.

=cut

sub close {
  my $self = shift;
  $self->DESTROY
}

# Destructor.
sub DESTROY {
  my $self = shift;
  untie %{$self->{db}};
}

=head1 Author

Alberto Simoes, C<< <ambs@cpan.org> >>

=head1 Bugs

Please report any bugs or feature requests to
C<bug-games-webpaper@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.  I will be notified, and then you'll automatically
be notified of progress on your bug as I make changes.

=head1 Copyright & License

Copyright 2004 Alberto Simoes, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of Games::WebPaper::Team
