OpenInteract2/Conversion/IniConfig.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 196 CC 18 SCAR 37.7
CC
18
Cov
0%
_handle_value
110.0
_convert_check
30.0
new
12.0
CRAP 43.3
1 package OpenInteract2::Conversion::IniConfig;
2  
3 use strict;
4 use base qw( Class::Accessor::Fast );
5 use OpenInteract2::Exception qw( oi_error );
6  
7 my @FIELDS = qw( old_content old_config transforms );
8 __PACKAGE__->mk_accessors( @FIELDS );
9  
10 $OpenInteract2::Conversion::IniConfig::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
11  
12 sub new {
13 my ( $class, $old_something ) = @_;
14 my $self = bless( {}, $class );
15 if ( $old_something ) {
16 if ( ref( $old_something ) ) {
17 $self->old_config( $old_something )
18 }
19 else {
20 $self->old_content( $old_something );
21 }
22 }
23 $self->init;
24 return $self;
25 }
26  
27 sub new_content {
28 return $_[0]->{_new_content};
29 }
30  
31 sub raw_content {
32 return $_[0]->{_raw_content};
33 }
34  
35 sub convert {
36 my ( $self ) = @_;
37 my $old_config = $self->_convert_check;
38 $self->old_config( $old_config );
39 my $fields_in_order = $self->get_field_order;
40 my %field_order = map { $fields_in_order->[ $_ ] => $_ }
41 ( 0 .. scalar( @{ $fields_in_order } ) - 1 );
42 my @keys = keys %{ $old_config };
43 my @out = ();
44 foreach my $object_key ( @keys ) {
45 my %later_hash = ();
46 my @this_set = ( "$object_key" );
47 my $config = $old_config->{ $object_key };
48  
49 $self->pre_transform_check( $config );
50  
51 # This should order all known fields and put all unknown
52 # fields at the back of the list in alpha-order
53  
54 my @ordered_keys = sort { $field_order{ $a } <=> $field_order{ $b } || $a cmp $b }
55 keys %{ $config };
56 KEY:
57 foreach my $cf ( @ordered_keys ) {
58 $self->_handle_value( $cf, $config->{ $cf }, \@this_set, \%later_hash );
59 }
60 push @out, \@this_set;
61  
62 foreach my $cf ( keys %later_hash ) {
63 my @later_set = ( "$object_key $cf" );
64 my $value = $config->{ $cf };
65 foreach my $sub_cf ( keys %{ $value } ) {
66 $self->_handle_value( $sub_cf, $value->{ $sub_cf }, \@later_set );
67 }
68 push @out, \@later_set;
69 }
70 }
71 $self->{_raw_content} = \@out ;
72 return $self->serialize;
73 }
74  
75 sub serialize {
76 my ( $self, $entries ) = @_;
77 $entries ||= $self->raw_content;
78 unless ( ref $entries eq 'ARRAY' ) {
79 oi_error "No raw content generated yet -- first call 'convert()'";
80 }
81  
82 my @out = ();
83 foreach my $entry ( @{ $entries } ) {
84  
85 # First item is the label...
86 push @out, "[" . shift( @{ $entry } ) . "]";
87  
88 # Remainder are actual items. First find the longest name...
89  
90 my $length = 0;
91 foreach my $pair ( @{ $entry } ) {
92 if ( length( $pair->[0] ) > $length ) {
93 $length = length( $pair->[0] );
94 }
95 }
96  
97 # Now output entries, padding each name to be $length size
98  
99 foreach my $pair ( @{ $entry } ) {
100 my $spacing = join( '', map { ' ' } ( 1 .. ( $length - length( $pair->[0] ) ) ) );
101 push @out, "$pair->[0]$spacing = $pair->[1]";
102 }
103 push @out, '';
104 }
105 return $self->{_new_content} = join( "\n", @out );
106 }
107  
108 sub _handle_value {
109 my ( $self, $name, $value, $out, $later ) = @_;
110 my $transforms = $self->transforms;
111 if ( $transforms->{ $name } ) {
112 my ( $new_name, $new_value ) =
113 $transforms->{ $name }->( $name, $value );
114 $name = $new_name if ( $new_name );
115 $value = $new_value if ( $new_value );
116 }
117  
118 my $typeof = ref $value;
119 if ( ! defined $value ) {
120 push @{ $out }, [ $name, '' ];
121 }
122 elsif ( $typeof eq 'ARRAY' ) {
123 my $num_items = scalar @{ $value };
124 if ( $num_items == 0 ) {
125 push @{ $out }, [ $name, '' ];
126 }
127 else {
128 push @{ $out }, map { [ $name, $_ ] } @{ $value };
129 }
130 }
131 elsif ( $typeof eq 'HASH' ) {
132 if ( $later ) {
133 $later->{ $name } = $value;
134 }
135 else {
136 push @{ $out }, [ $name, "CANNOT STRINGIFY TWO-LEVEL HASHREF" ];
137 }
138 }
139 elsif ( $typeof ) {
140 push @{ $out }, [ $name, "CANNOT STRINGIFY $typeof REFERENCE" ];
141 }
142 else {
143 push @{ $out }, [ $name, $value ];
144 }
145 }
146  
147 sub _convert_check {
148 my ( $self ) = @_;
149 my ( $old_config );
150 if ( $self->old_config ) {
151 $old_config = $self->old_config;
152 }
153 else {
154 my $old_content = $self->old_content;
155 unless ( $old_content ) {
156 oi_error "You must first set content in constructor or via ",
157 "the 'old_content' method.";
158 }
159 {
160 no strict;
161 $old_config = eval "$old_content";
162 }
163 if ( $@ ) {
164 oi_error "Your original perl configuration is invalid:$@\n\n",
165 "Please fix and try again.";
166 }
167 }
168 unless ( ref $old_config eq 'HASH' ) {
169 oi_error "Are you sure your perl configuration is correct?\n",
170 "It should be a hashref but I'm getting that it's a ",
171 ref( $old_config ), "\nCannot continue.";
172 }
173 return $old_config;
174 }
175  
176 ########################################
177 # INTERFACE
178  
179 sub init {
180 return $_[0];
181 }
182  
183 sub get_field_order {
184 die "get_field_order() Must be defined in subclass\n";
185 }
186  
187 sub pre_transform_check {
188 return $_[1];
189 }
190  
191 1;
192  
193 __END__