File Coverage

blib/lib/Class/DBI/UUID.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             package Class::DBI::UUID;
2             # $Id: UUID.pm,v 1.1 2005/01/31 18:51:22 cwest Exp $
3 1     1   871 use strict;
  1         3  
  1         52  
4              
5             =head1 NAME
6              
7             Class::DBI::UUID - Provide Globally Unique Column Values
8              
9             =head1 SYNOPSIS
10              
11             package MyApp::User;
12             use base qw[Class::DBI];
13              
14             __PACKAGE__->connection('dbi:SQLite:dbfile', '', '');
15             __PACKAGE__->table(q[users]);
16             __PACKAGE__->columns(Primary => 'id');
17             __PACKAGE__->columns(Essential => qw[username password]);
18              
19             use Class::DBI::UUID;
20             __PACKAGE__->uuid_columns('id');
21              
22             # Elsewhere..
23             my $user = MyApp::User->create({
24             username => 'user',
25             password => 'pass',
26             });
27            
28             print $user->id; # A UUID string.
29              
30             =head1 DESCRIPTION
31              
32             This module implements globally unique columns values. When an object
33             is created, the columns specified are given unique IDs. This is particularly
34             helpful when running in an environment where auto incremented primary
35             keys won't work, such as multi-master replication.
36              
37             =cut
38              
39 1     1   5 use base qw[Exporter Class::Data::Inheritable];
  1         2  
  1         145  
40 1     1   17 use vars qw[@EXPORT $VERSION];
  1         2  
  1         115  
41             $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1];
42             @EXPORT = qw[uuid_columns uuid_columns_type];
43              
44 1     1   942 use Data::UUID;
  1         1018  
  1         252  
45              
46             =head2 uuid_columns
47              
48             MyApp::User->uuid_columns(MyApp::User->columns('Primary'));
49              
50             A C trigger will be set up to set the values of each column
51             listed as input to a C string. Change the type of string output
52             using the C class method.
53              
54             =cut
55              
56             sub uuid_columns {
57 2     2 1 7304 my ($class, @cols) = @_;
58 2         13 my $type = 'create_' . __PACKAGE__->_uuid_type;
59             $class->add_trigger(before_create => sub {
60 3     3   1955 my ($self) = @_;
61 3         6 foreach ( @cols ) {
62 6         2333 $self->{$_} = Data::UUID->new->$type;
63             }
64 2         53 });
65             }
66              
67             =head2 uuid_columns_type
68              
69             MyApp::User->uuid_columns_type('bin'); # keep it small
70              
71             By default the type will be C. It's the largest, but its also the
72             safest for general use. Possible values are C, C, C, and
73             C. Basically, anything that you can append to C and still
74             get a valid method name from C. Also returns the type to be
75             used.
76              
77             Do not change this value on a whim. If you do change it, change it before
78             your call to C, or, call C again after it is
79             changed (therefore calling it before C, but also adding extra
80             triggers without need).
81              
82             =cut
83              
84             __PACKAGE__->mk_classdata('_uuid_type');
85             __PACKAGE__->_uuid_type('str');
86             sub uuid_columns_type {
87 2     2 1 3884 my $class = shift;
88 2 100       12 return __PACKAGE__->_uuid_type(shift) if @_;
89 1         8 return __PACKAGE__->_uuid_type;
90             }
91              
92             1;
93              
94             __END__