Gossamer Forum
Quote Reply
GT::Base clase ?
Alex,

Any insight on using this?

I want to create an object, based on the Table object, such that "File" represents an uploaded/uploading file, or an in-process edited/editing file. Everything seems built on the GT:Base, so understanding and potentially using that seems to make sense.

Table is a good object to use as a guide, since it will fundamentally be a database record with a few extra flag fields, and the methods to operate on them. It's a subset of the Table object, really, that operates on a database record, not a table, but has to grab the table 'def' to start out.


It should ideally load the '.def' file for the table it's using, and add them to the "control fields" that are created when the object is initialized. For instance, an in-progress upload object would need to know where it's temporary files are -- temp_dir, temp_file, etc, while the database object only needs to know the final resting place.

The reason for this, is because a file lives as a real-world object on a disk, it has attributes that can be changed, and by creating a software object that represents it, properties such as name, location, or even existence can be much more easily, and properly modeled.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Plugins:http://LinkSQL.com/plugin
Quote Reply
Re: GT::Base clase ? In reply to
Hi Pugdog,

Sorry about the late reply, must have missed this one.

GT::Base is our base class for all objects. It provides a consistent way of error handling, accessor methods, debuging and object creation.

To inherit, all you need to do is:

Code:
package My::Files;
use GT::Base;
use strict;
use vars qw/@ISA/;
@ISA = qw/GT::Base/; # Inherit from GT::Base
And that's it. You can now do:

my $file = new My::Files;

but that's about it so far. To provide attributes you could do:

Code:
package My::Files;
use GT::Base;
use strict;
use vars qw/@ISA $ATTRIBS/;
@ISA = qw/GT::Base/; # Inherit from GT::Base
$ATTRIBS = {
filename => undef,
filesize => undef,
filetype => undef
};
Now you can do:

my $file = new My::File ( filename => 'alex.txt', filesize => 123, filetype => 'text/plain' );
print "The name is: ", $file->filename, "\n";
$file->filename ('newname.txt');
etc.

The other important thing is error handling. With your file object, you can now return:

return $file->error ('ERRCODE', 'FATAL', any args);

where ERRCODE is an error code, FATAL is whether it should die (otherwise use WARN), and args is any arguments to pass to the error string. So a more complete object might look like:

Code:
package My::Files;
use GT::Base;
use strict;
use vars qw/@ISA $ATTRIBS $ERRORS/;
@ISA = qw/GT::Base/; # Inherit from GT::Base
$ATTRIBS = {
filename => undef,
filesize => undef,
filetype => undef
};
$ERRORS = {
'TOOBIG' => "The file you uploaded is too large.",
'BADNAME' => "The filename %s is invalid."
};

sub filename {
# accessor method for filename attribute, but check to make
# sure it is a proper name.
my $self = shift;
if ($_[0]) {
if (length $_[0] !~ /^\w+$/) {
return $self->error ('BADNAME', 'WARN', $_[0]);
}
$self->{filename} = $_[0];
}
return $self->{filename};
}
and then you would do:

my $file = new My::File;
$file->filename ('not valid') or die "Error: $My::File::error";

Now, this may all be not quite what you are looking for, but I hope it will be of use to someone. =)

Let me know anything else you may have questions on!

Cheers,

Alex

--
Gossamer Threads Inc.