###################################################################

package KoLBot::Item;
 
# Copyright (c) 2004 Alan W. Mahoney. All rights reserved. 
# This program is free software.  You may modify and/or 
# distribute it under the same terms as Perl itself.  
# This copyright notice must remain attached to the file.  
#

# 21 July 2004

use warnings;
use strict;

use POSIX qw( floor );

BEGIN 
{
  use Exporter ();
  our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS,$ID);

  $VERSION     = 0.5;
  $ID          = '$Id: Item.pm 159 2006-09-18 23:08:57Z alan $';

  @ISA         = qw(Exporter);
  @EXPORT      = qw();
  %EXPORT_TAGS = ( );

  @EXPORT_OK   = qw();
}

our @EXPORT_OK;

##########################################
## element access
##########################################

for my $field (qw( name
		   plural
		   id
		   desc
		   number
		   )) {
  no strict "refs";
  *$field = sub {
    my $self = shift;
    $self->{$field} = shift if @_;
    return $self->{$field};
  }
}

##########################################
## Overloads
##########################################

use overload q("") => \&as_string;

##########################################
## Constructor:
##########################################

# Needs to be called with a full config hashref.  See examples.

sub new {
  my $invocant = shift;
  my $class = ref($invocant) || $invocant;
  my $item = { number => 0, @_ };
  bless $item, $class;
}

##########################################
## The rest
##########################################

sub as_string {
  my $item = shift;
  my $str = '';

  my $id = $item->{id} || 0;
  my $space = $id < 10 ? "  " : $id < 100 ? " " : "";
  $str .= $space . $id . ' '
    . $item->{desc} . " " . $item->{name};
  if(exists($item->{plural}) 
     && defined($item->{plural})
     && $item->{plural} ne '') {
    $str .= " : " . $item->{plural};
  }
  return $str;
}

END {} ;

1;

