| 1 | package Draft::Protozoa::Yml; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Draft::Protozoa::Yml - CAD drawing-object base class | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | A CAD drawing object that consists of a single file with an internal | ||
| 10 | L<YAML> format. | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use YAML; | ||
| 20 | use File::Atomism::utils qw /TempFilename Journal/; | ||
| 21 | |||
| 22 | =pod | ||
| 23 | |||
| 24 | =head1 USAGE | ||
| 25 | |||
| 26 | Create an object by supplying the 'new' method with a file-path: | ||
| 27 | |||
| 28 | use Draft::Drawing::Yml; | ||
| 29 | my $foo = Draft::Drawing::Yml->new ('/path/to/file.yml'); | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my $class = shift; | ||
| 36 | $class = ref $class || $class; | ||
| 37 | my $self = bless {}, $class; | ||
| 38 | $self->{_path} = shift; | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | =pod | ||
| 43 | |||
| 44 | Read/Parse an existing file with the Read method: | ||
| 45 | |||
| 46 | $foo->Read; | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub Read | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $data = YAML::LoadFile ($self->{_path}); | ||
| 55 | |||
| 56 | my $newclass = "Draft::Protozoa::Yml::". $self->Type ($data) ."::". $self->Version ($data); | ||
| 57 | |||
| 58 | eval "use $newclass"; | ||
| 59 | $@ and return 0; | ||
| 60 | |||
| 61 | bless $self, $newclass; | ||
| 62 | |||
| 63 | $self->_parse ($data); | ||
| 64 | } | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | Objects can be moved in the world-space: | ||
| 69 | |||
| 70 | $foo->Move ([10, 5, 0]); | ||
| 71 | |||
| 72 | This edits the file on the filesystem in one atomic operation. This | ||
| 73 | is a permanent operation - If you find that you didn't want to move | ||
| 74 | the object, you might want to investigate using cvs for your data. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub Move | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | my $vec = shift; | ||
| 82 | |||
| 83 | my $data = YAML::LoadFile ($self->{_path}); | ||
| 84 | |||
| 85 | # element could have any number of points | ||
| 86 | for my $point (0 .. @{$data->{points}} - 1) | ||
| 87 | { | ||
| 88 | # vector could have any number of dimensions | ||
| 89 | for my $axis (0 .. @{$vec} -1) | ||
| 90 | { | ||
| 91 | $data->{points}->[$point]->[$axis] += $vec->[$axis]; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | my $temp = TempFilename ($self->{_path}); | ||
| 96 | YAML::DumpFile ($temp, $data); | ||
| 97 | |||
| 98 | Journal ([[$self->{_path}, $temp]]); | ||
| 99 | rename $temp, $self->{_path}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _capitalise | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | my $word = shift; | ||
| 106 | my $first = substr ($word, 0, 1, ''); | ||
| 107 | return uc ($first) . lc ($word); | ||
| 108 | } | ||
| 109 | |||
| 110 | =pod | ||
| 111 | |||
| 112 | Query the type and version of the object like so: | ||
| 113 | |||
| 114 | my $type = $self->Type ($foo); | ||
| 115 | my $version = $self->Version ($foo); | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub Type | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $data = shift; | ||
| 123 | my $type = $data->{type} || 'unknown'; | ||
| 124 | $type =~ s/ .*//; | ||
| 125 | $type =~ s/[^a-z0-9_]//gi; | ||
| 126 | $self->_capitalise ($type); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub Version | ||
| 130 | { | ||
| 131 | my $self = shift; | ||
| 132 | my $data = shift; | ||
| 133 | my $version = $data->{version} || 'draft1'; | ||
| 134 | $version =~ s/ .*//; | ||
| 135 | $version =~ s/[^a-z0-9_]//gi; | ||
| 136 | $self->_capitalise ($version); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub Process {} | ||
| 140 | |||
| 141 | sub Draw {} | ||
| 142 | |||
| 143 | 1; |