NAME

Class::PINT::DataTypes - Specifying Accessors for complex Class::PINT attributes

DESCRIPTION

This package provides accessors and mutators for Class::PINT attributes with compound or complex datatypes.

It provides the built-in datatypes of array, hash and boolean. It also allows you to add additional datatypes and methods.

SYNOPSIS

# Class

package Nautical::Course;

use base qw(Class::PINT);

__PACKAGE__->add_datatype('coordinate',{ get=>\sub { .. } }, .. );

# ...

__PACKAGE__->column_types(coordinate => qw/Point Position Destination/);

CLASS METHODS

add_datatype

add_datatype is a class method that allows you to add a new datatype for attributes.

__PACKAGE__->add_datatype('coordinate',{ get=>\sub { .. } }, .. );

default actions are :

write => \sub { return $_[1]; },

read => \sub { return $_[1]; },

get => \sub {

 	     my ($self,$column,@args) = @_;

  	     die "read-only get accessor called on $column with new value(s)\n" if (@args);

	     return $self->{$column};

	    },

set => \sub {

            my ($self,$column,@args) = @_;

	    die "write-only set mutator called on $column without new value(s)\n" unless (@args);

	    $self->{$column} = $args[0];

	    return;

	    },

getset => \sub {

	my ($self,$column,@args) = @_;

	if (@args) {

	    $self->{$column} = $args[0];

	}

	return $self->{$column};

       },

The read/write subs munge the attributes value on the way in/out of the database.

The set method is a write-only mutator that sets the attributes internal value.

The get method is a read-only accessor that gets the attributes value.

The getset method is an impure accessor that gets/sets the attributes value depending on how it is called.

The get,set and getset methods can get/set values that make up a compound attribute such as a list or dictionary.

Additional actions can be added but read,write,get,set and getset are required, and defaults are provided - see the example below.

You can use an alias instead of a subref except for read,write and getset which must be specified, this is done by setting the value of the action key to the action it is an alias of, for example : another_name_for_get => 'get'.

You can specify the full name of a method as long as you include 'Attribute_' in the name, which will be replaced by the name of the Attribute, otherwise all methods are named as action_Attribute, i.e. get_Foo.

The read and write subs take 2 arguments : the column and then the value.

All other subs take at least 3 arguments: a reference to the class or object, the column and then any values

Any mutator sub should call _set_complex_attribute once it has made its changes, and only if it has made changes ( as doing so, may incur a database update, and refetching values from the database). See below for more details

OBJECT METHODS

_set_complex_attribute

_set_complex_attribute ensures that persistance works for an attribute as PINT bypasses parts of Class::DBI's persistance code. Calling this function ensures that everything works as users and developers expects. It calls triggers, updates CDBI internal information, and ensures the database is kept up to date. See Class::DBI documentation for update and autoupdate for more details on how CDBI manages persistence.

This method takes the name of the column as its only argument :

$self->_set_complex_attribute($column);

This method is seen as an internal or private method and is only required when adding your own datatypes and their accessors, hence the underscore at the start of the name.

SEE ALSO

perl

Class::PINT

Class::DBI

Class::Tangram

AUTHOR

Aaron J. Trevena, <aaron@droogs.org>

COPYRIGHT

Licensed for use, modification and distribution under the Artistic and GNU GPL licenses.

Copyright (C) 2004 by Aaron J Trevena <aaron@droogs.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.