Collection - CRUD framework
package MyCollection; use Collection; @MyCollection::ISA = qw(Collection);
A collection - sometimes called a container - is simply an object that groups multiple elements into a single unit. Collection are used to store, retrieve, manipulate, and communicate aggregate data.
The primary advantages of a Collection framework are that it reduces programming effort by providing useful data structures and algorithms so you don't have to write them yourself.
The Collection framework consists of:
Wrapper Implementations - Add functionality, such as mirroring and lazy load, to other implementations.
Algorithms - methods that perform useful functions, such as caching.
This module has a task - to be a base class for ather Collections. You can inherit the methods _create, _delete, _fetch, _store and may be _prepare_record for new source of data. As you see this is similar to CRUD (Create - Read - Update- Delete).
Sample:
my $col = new MyCollection:: <some params>; #fetch objects or data by keys my $data = $col->fetch(1,2,3,4,5); #do something foreach my $item ( values %$data) { $_->attr->{inc} ++ } #You can use "lazy" functionality my $not_actualy_fetch = $col->get_lazy(6,7,8,9); #store changed data or objects $col->store; #free memory $col->release;
Sample from Collection::AutoSQL:
my $beers = new Collection::AutoSQL:: dbh => $dbh, #database connect table => 'beers', #table name field => 'bid', #key field (IDs), usually primary,autoincrement cut_key => 1; #delete field 'bid' from readed records, my $heineken = $beers->fetch_one(1); #SELECT * FROM beers WHERE bid in (1)
Sample from Collection::Memcached:
use Collection::Memcached; use Cache::Memcached; $memd = new Cache::Memcached { 'servers' => [ "127.0.0.1:11211" ], 'debug' => 0, 'compress_threshold' => 10_000, }; my $collection = new Collection::Memcached:: $memd; my $collection_prefix = new Collection::Memcached:: $memd, 'prefix';
Method for store changed objects. Called with ref to hash :
{ ID1 => <reference to object1> [,ID2 => <reference to object2>,...] }
Read data for given IDs. Must return reference to hash, where keys is IDs, values is readed data. For example:
return {1=>[1..3],2=>[5..6]}
Create recods in data storage.
Parametrs:
user defined format
Result: Must return reference to hash, where keys is IDs, values is create records of data
Delete records in data storage for given IDs.
Parametrs: array id IDs
ID1, ID2, ...
or array of refs to HASHes
{ id=>ID1 }, {id => ID2 }, ...
Format of parametrs depend method delete
Called before insert readed objects into collection. Must return ref to data or object, which will insert to callection.
Public method for create objects.
Public methods. Fetch object from collection for given ID. Return ref to objects or undef unless exists.
Public methods. Fetch objects from collection for given IDs. Return ref to HASH, where where keys is IDs, values is objects refs.
Release from collection objects with IDs. Only delete given keys from collection or all if empty
Call _store for changed objects. Store all loaded objects without parameters:
$simple_collection->store(); #store all changed
or (for 1,2,6 IDs )
$simple_collection->store(1,2,6);
Release from collections and delete from storage (by calling _delete) objects ID1,ID2...
$simple_collection->delete(1,5,84);
Method for base support lazy load objects from data storage. Not really return lazy object.
Collection::Memcached, Collection::Mem, Collection::AutoSQL, README
Zahatski Aliaksandr, <zag@cpan.org>
Copyright (C) 2005-2008 by Zahatski Aliaksandr
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.8 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
=pod directives shouldn't be over one line long! Ignoring all 7 lines of content
To install Collection, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Collection
CPAN shell
perl -MCPAN -e shell install Collection
For more information on module installation, please visit the detailed CPAN module installation guide.