File Coverage

lib/Draft/Protozoa/Eml.pm
Criterion Covered Total %
statement 53 72 73.6
branch 2 12 16.6
condition 3 7 42.8
subroutine 10 11 90.9
pod 0 6 0.0
total 68 108 62.9


line stmt bran cond sub pod time code
1             package Draft::Protozoa::Eml;
2              
3             =head1 NAME
4              
5             Draft::Protozoa::Eml - CAD drawing-object base class
6              
7             =head1 SYNOPSIS
8              
9             This class is DEPRECATED, use YAML formatted file instead.
10              
11             A CAD drawing object that consists of a single file that resembles
12             an rfc822 email message.
13              
14             =head1 DESCRIPTION
15              
16             If you find yourself using this base-class directly, then you are
17             probably doing something wrong.
18              
19             =cut
20              
21 3     3   8791 use strict;
  3         8  
  3         129  
22 2     2   10 use warnings;
  2         3  
  2         71  
23              
24 2     2   509 use File::Atomism::utils qw /TempFilename Journal/;
  2         4  
  2         1918  
25              
26             =pod
27              
28             =head1 USAGE
29              
30             Create an object by supplying the 'new' method with a file-path:
31              
32             use Draft::Drawing::Eml;
33             my $foo = Draft::Drawing::Eml->new ('/path/to/file.eml');
34              
35             =cut
36              
37             sub new
38             {
39 3     3 0 139 my $class = shift;
40 3   33     22 $class = ref $class || $class;
41 3         30 my $self = bless {}, $class;
42 3         20 $self->{_path} = shift;
43 3         12 return $self;
44             }
45              
46             =pod
47              
48             Read/Parse an existing file with the Read method:
49              
50             $foo->Read;
51              
52             Currently the file-format is a simple series of key->value
53             declarations, one per line:
54              
55             Pi: 3.1418
56             Author: Plato
57             Author: Descartes
58              
59             This might become a perl structure like this:
60              
61             '/path/to/file.eml' => bless( {
62             'pi' => [ '3.1418' ],
63             'author' => [ 'Plato', 'Descartes' ],
64             '_path' => '/path/to/file.eml'
65             }, 'Draft::Drawing::Eml' )
66              
67             =cut
68              
69             sub Read
70             {
71 3     3 0 26 my $self = shift;
72              
73 3         10 my $data = LoadFile ($self->{_path});
74              
75 3         14 my $newclass = "Draft::Protozoa::Eml::". $self->Type ($data) ."::". $self->Version ($data);
76              
77 2     2   1193 eval "use $newclass";
  2         8  
  2         40  
  3         206  
78 3 50       14 $@ and return 0;
79              
80 3         10 bless $self, $newclass;
81              
82 3         47 $self->_parse ($data);
83             }
84              
85             sub LoadFile
86             {
87 3     3 0 5 my $path = shift;
88 3 50       135 open FILE, "<". $path or warn $path ." not found.\n";
89 3         92 my @lines = ;
90 3         28 close FILE;
91              
92 3         5 my $data;
93              
94             # FIXME: should stop processing on first blank line
95             # FIXME: should join lines prefixed with whitespace
96              
97 3         9 for my $line (@lines)
98             {
99 29         30 chomp $line;
100 29         67 my ($key, $value) = split (':', $line);
101 29         36 $key = lc ($key);
102 29         77 $value =~ s/^ +//;
103 29         32 push @{$data->{$key}}, $value;
  29         78  
104             }
105              
106 3         10 return $data;
107             }
108              
109             =pod
110              
111             Objects can be moved in the world-space:
112              
113             $foo->Move ([10, 5, 0]);
114              
115             This edits the file on the filesystem in one atomic operation. This
116             is a permanent operation - If you find that you didn't want to move
117             the object, you might want to investigate using cvs for your data.
118              
119             =cut
120              
121             sub Move
122             {
123 0     0 0 0 my $self = shift;
124 0         0 my $vec = shift;
125              
126 0 0       0 open FILE, "<". $self->{_path} or warn $self->{_path} ." not found.\n";
127 0         0 my @lines = ;
128 0         0 close FILE;
129              
130 0         0 my $temp = TempFilename ($self->{_path});
131 0         0 open FILE, ">". $temp;
132              
133 0         0 my $done;
134              
135 0         0 for my $line (@lines)
136             {
137 0         0 my ($key, $value) = split ': ', $line;
138 0 0       0 $done->{$key} = 0 unless $done->{$key};
139              
140 0 0       0 if ($key =~ /^[0-9]+$/)
141             {
142 0         0 $value += $vec->[$done->{$key}];
143 0         0 $line = "$key: $value\n";
144             }
145              
146 0         0 $done->{$key}++;
147 0         0 print FILE $line;
148             }
149              
150 0 0       0 close FILE || return;
151              
152 0         0 Journal ([[$self->{_path}, $temp]]);
153 0         0 rename $temp, $self->{_path};
154             }
155              
156             sub _capitalise
157             {
158 6     6   9 my $self = shift;
159 6         10 my $word = shift;
160 6         13 my $first = substr ($word, 0, 1, '');
161 6         32 return uc ($first) . lc ($word);
162             }
163              
164             =pod
165              
166             Query the type and version of the object like so:
167              
168             my $type = $self->Type ($foo);
169             my $version = $self->Version ($foo);
170              
171             =cut
172              
173             sub Type
174             {
175 3     3 0 5 my $self = shift;
176 3         5 my $data = shift;
177 3   50     12 my $type = $data->{type}->[0] || 'unknown';
178 3         8 $type =~ s/ .*//;
179 3         7 $type =~ s/[^a-z0-9_]//gi;
180 3         24 $self->_capitalise ($type);
181             }
182              
183             sub Version
184             {
185 3     3 0 5 my $self = shift;
186 3         5 my $data = shift;
187 3   50     539 my $version = $data->{version}->[0] || 'draft1';
188 3         6 $version =~ s/ .*//;
189 3         6 $version =~ s/[^a-z0-9_]//gi;
190 3         8 $self->_capitalise ($version);
191             }
192              
193             #sub Process {}
194              
195             #sub Draw {}
196              
197             1;