File Coverage

blib/lib/DBIx/Changeset/Record.pm
Criterion Covered Total %
statement 46 48 95.8
branch 13 14 92.8
condition 3 3 100.0
subroutine 10 12 83.3
pod 6 6 100.0
total 78 83 93.9


line stmt bran cond sub pod time code
1             package DBIx::Changeset::Record;
2              
3 11     11   417417 use warnings;
  11         24  
  11         450  
4 11     11   57 use strict;
  11         24  
  11         363  
5              
6 11     11   53 use base qw/Class::Factory DBIx::Changeset/;
  11         23  
  11         5204  
7 11     11   10024 use Data::UUID;
  11         36885  
  11         1100  
8              
9 11     11   101 use vars qw{$VERSION};
  11         24  
  11         565  
10             BEGIN {
11 11     11   6917 $VERSION = '1.11';
12             }
13              
14             =head1 NAME
15              
16             DBIx::Changeset::Record - Factory Interface to changeset files
17              
18             =cut
19              
20             =head1 SYNOPSIS
21              
22             Factory Interface to changeset files
23              
24             Perhaps a little code snippet.
25              
26             use DBIx::Changeset::Record;
27              
28             my $foo = DBIx::Changeset::Record->new('type', $opts);
29             ...
30             $foo->read('/moose/moose.sql');
31              
32             =head1 INTERFACE
33              
34             =head2 read
35             This is the read interface implement in your own class
36             =cut
37             sub read {
38 0     0 1 0 die "Define read() in implementation"
39             }
40              
41             =head2 write
42             This is the write interface implement in your own class
43             =cut
44             sub write {
45 0     0 1 0 die "Define write() in implementation"
46             }
47              
48             =head1 METHODS
49              
50             =head2 init
51             Called automatically to intialise the factory objects takes params passed to new and assigns them to
52             accessors if they exist
53             =cut
54              
55             sub init {
56 33     33 1 17809 my ( $self, $params ) = @_;
57              
58 33 100       167 DBIx::Changeset::Exception::ObjectCreateException->throw( error => 'Attempt to create Record Object without a uri.' ) unless defined $params->{'uri'};
59              
60 31         60 foreach my $field ( keys %{$params} ) {
  31         112  
61 56 50       639 $self->{ $field } = $params->{ $field } if ( $self->can($field) );
62             }
63 31         200 return $self;
64             }
65              
66             =head2 validate
67             Validate that the file data is correct
68             =cut
69             sub validate {
70 11     11 1 2829 my ($self) = @_;
71 11         51 my $file = $self->read($self->uri);
72            
73 11 100       210 if ($file =~ m!^/\*.*?tag:\s*(\S+)!mx) {
74 10         80 $self->id($1);
75 10         163 $self->valid(1);
76             } else {
77 1         5 $self->valid(0);
78             }
79            
80 11         117 return;
81             }
82              
83             =head2 generate_uid
84             Generates a uid writing it to the file
85             =cut
86             sub generate_uid {
87 4     4 1 426 my ($self, $data) = @_;
88 4         628 my $ug = new Data::UUID;
89 4 100       29 $data = $self->read() unless defined $data;
90 4         1599 $self->id($ug->to_string($ug->create()));
91             ### check for existing id
92 4 100       146 if ($data =~ m!^/\*.*?tag:\s*(\S+)!mx) {
93             ### replace it
94 2         9 my $id = sprintf('* tag: %s', $self->id);
95 2         35 $data =~ s!\*.*?tag:\s*(\S+)!$id!exm;
  2         9  
96             } else {
97             ### tack it on the end
98 2         103 $data .= sprintf("\n/* tag: %s */\n",$self->id);
99             }
100 4         42 $self->write($data);
101 3         539 return;
102             }
103              
104             =head1 ACCESSORS
105              
106             =head2 valid
107             Has this file been processed as valid default false
108             args:
109             bool
110             returns:
111             bool
112              
113             =head2 skipped
114             Has this file been skipped default false
115             args:
116             bool
117             returns:
118             bool
119              
120             =head2 outstanding
121             Is this file outstanding default false
122             args:
123             bool
124             returns:
125             bool
126              
127             =head2 id
128             The UID of the file
129             args:
130             bool
131             returns:
132             bool
133              
134             =head2 uri
135             The location of the file data * READ ONLY *
136             args:
137             string
138             returns:
139             string
140             =cut
141              
142             my @ACCESSORS = qw/id valid skipped outstanding forced/;
143             __PACKAGE__->mk_accessors(@ACCESSORS);
144              
145             my @RO_ACCESSORS = qw/uri/;
146             __PACKAGE__->mk_ro_accessors(@RO_ACCESSORS);
147              
148             =head1 OVERRIDEN METHODS
149              
150             =head2 get
151              
152             Override the ger accessor for id and valid so that if they are undef
153             validate is called.
154              
155             =cut
156              
157             sub get {
158 128     128 1 23873 my ($self, $key) = @_;
159              
160 128 100 100     686 if ( ($key eq 'id') || ($key eq 'valid') ) {
161 32         138 my $value = $self->SUPER::get($key);
162 32 100       243 unless ( defined $value ) {
163 9         55 $self->validate();
164             }
165             }
166 128         444 return $self->SUPER::get($key);
167             }
168              
169              
170             =head1 TYPES
171             Default types included
172              
173             =head2 disk
174             Simply reads files from disk expects a uri of a filename
175             =cut
176             __PACKAGE__->register_factory_type( disk => 'DBIx::Changeset::Record::Disk' );
177              
178              
179             =head1 COPYRIGHT & LICENSE
180              
181             Copyright 2004-2008 Grox Pty Ltd.
182              
183             This program is free software; you can redistribute it and/or modify it
184             under the same terms as Perl itself.
185              
186             The full text of the license can be found in the LICENSE file included with this module.
187              
188             =cut
189              
190             1; # End of DBIx::Changeset