DS/Transformer/Projector.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 50 CC 5 SCAR 23.9
CC
5
Cov
0%
new
20.0
process
6.0
BEGIN
2.0
CRAP 10.9
1 #!perl
2
3 # ########################################################################## #
4 # Title: Field projector
5 # Creation date: 2007-03-05
6 # Author: Michael Zedeler
7 # Description: Make a projection of line
8 # Data Stream class
9 # Data transformer
10 # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/Projector.pm,v $
11 # Repository: kronhjorten
12 # State: $State: Exp $
13 # Documentation: inline
14 # Recepient: -
15 # ########################################################################## #
16
17 package DS::Transformer::Projector;
18
19 use base qw{ DS::Transformer };
20
21 use strict;
22 use Carp::Assert;
23
24 our ($VERSION) = $DS::VERSION;
25 our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/;
26
27
28 sub new {
29 #TODO Do not just set $self->{typespec} to $typespec. Do the restriction on a copy of the existing typespec and use that.
30 my( $class, $typespec, $source, $project_fields ) = @_;
31
32 my $project_fields_result;
33 my @new_fields = ();
34 if( ref( $project_fields ) eq 'HASH' ) {
35 $project_fields_result = $project_fields;
36 } else {
37 $project_fields_result = {};
38
39 foreach my $field_line ( split /\n/, $project_fields ) {
40 next if $field_line =~ /^\s*#/;
41 my( $old_field, $new_field ) = $field_line =~ /\s*(\S+)(?:\s+(\S+))?/;
42 $project_fields_result->{$old_field} = $new_field;
43 push @new_fields, $new_field;
44 }
45 }
46
47 my $self;
48
49 if( defined( $project_fields_result ) ) {
50 my $projected_typespec = $typespec->project( 'adlines', $project_fields_result );
51 $self = $class->SUPER::new( $projected_typespec, $source );
52 $self->{project_fields} = $project_fields_result;
53 }
54
55 return $self;
56 }
57
58 sub process {
59 my( $self, $row ) = @_;
60
61 my $new_row = {};
62
63 foreach my $old_field (keys %$row) {
64 if(defined(my $new_field = $self->{project_fields}->{$old_field})) {
65 $new_row->{$new_field} = $row->{$old_field};
66 }
67 }
68
69 $new_row;
70 }
71
72 1;
73
74