line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# the contents of this file are Copyright (c) 2009-2011 Daniel Norman |
2
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
3
|
|
|
|
|
|
|
# modify it under the terms of the GNU General Public License as |
4
|
|
|
|
|
|
|
# published by the Free Software Foundation. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package DBR::Config::Trans; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
18
|
|
|
18
|
|
106
|
use strict; |
|
18
|
|
|
|
|
35
|
|
|
18
|
|
|
|
|
1098
|
|
10
|
18
|
|
|
18
|
|
109
|
use base 'DBR::Common'; |
|
18
|
|
|
|
|
88
|
|
|
18
|
|
|
|
|
1892
|
|
11
|
|
|
|
|
|
|
|
12
|
18
|
|
|
18
|
|
14509
|
use DBR::Config::Trans::Enum; |
|
18
|
|
|
|
|
55
|
|
|
18
|
|
|
|
|
619
|
|
13
|
18
|
|
|
18
|
|
29046
|
use DBR::Config::Trans::Dollars; |
|
18
|
|
|
|
|
55
|
|
|
18
|
|
|
|
|
773
|
|
14
|
18
|
|
|
18
|
|
19609
|
use DBR::Config::Trans::UnixTime; |
|
18
|
|
|
|
|
132
|
|
|
18
|
|
|
|
|
1230
|
|
15
|
18
|
|
|
18
|
|
12385
|
use DBR::Config::Trans::DateTime; |
|
18
|
|
|
|
|
61
|
|
|
18
|
|
|
|
|
653
|
|
16
|
18
|
|
|
18
|
|
10703
|
use DBR::Config::Trans::Percent; |
|
18
|
|
|
|
|
56
|
|
|
18
|
|
|
|
|
23600
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %MODULES = ( |
21
|
|
|
|
|
|
|
1 => 'Enum', |
22
|
|
|
|
|
|
|
2 => 'Dollars', |
23
|
|
|
|
|
|
|
3 => 'UnixTime', |
24
|
|
|
|
|
|
|
4 => 'Percent', |
25
|
|
|
|
|
|
|
5 => 'DateTime', |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub list_translators{ |
29
|
55
|
|
|
55
|
0
|
338
|
return [ map { {id => $_, name => $MODULES{$_} } } keys %MODULES ]; |
|
275
|
|
|
|
|
1519
|
|
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub load{ |
33
|
24
|
|
|
24
|
0
|
78
|
my( $package ) = shift; |
34
|
24
|
|
|
|
|
117
|
my %params = @_; |
35
|
|
|
|
|
|
|
|
36
|
24
|
|
|
|
|
262
|
my $self = { session => $params{session} }; |
37
|
24
|
|
|
|
|
138
|
bless( $self, $package ); # Dummy object |
38
|
|
|
|
|
|
|
|
39
|
24
|
|
50
|
|
|
136
|
my $instance = $params{instance} || return $self->_error('instance is required'); |
40
|
|
|
|
|
|
|
|
41
|
24
|
|
50
|
|
|
108
|
my $field_ids = $params{field_id} || return $self->_error('field_id is required'); |
42
|
24
|
50
|
|
|
|
116
|
$field_ids = [$field_ids] unless ref($field_ids) eq 'ARRAY'; |
43
|
|
|
|
|
|
|
|
44
|
24
|
|
|
|
|
121
|
foreach my $trans_id (keys %MODULES){ |
45
|
|
|
|
|
|
|
|
46
|
120
|
50
|
33
|
|
|
642
|
my $module = $MODULES{$trans_id} or $self->_error('invalid module') or next; |
47
|
|
|
|
|
|
|
|
48
|
120
|
|
|
|
|
239
|
my $pkg = 'DBR::Config::Trans::' . $module; |
49
|
|
|
|
|
|
|
#eval "require $pkg" or $self->_error('failed to load package ' . $pkg) or next; |
50
|
|
|
|
|
|
|
|
51
|
120
|
50
|
|
|
|
1487
|
$pkg->moduleload( |
52
|
|
|
|
|
|
|
session => $self->{session}, |
53
|
|
|
|
|
|
|
instance => $instance, |
54
|
|
|
|
|
|
|
field_id => $field_ids, |
55
|
|
|
|
|
|
|
) or return $self->_error('failed to load translator' . $module); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
24
|
|
|
|
|
502
|
return 1; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
96
|
|
|
96
|
0
|
337
|
sub moduleload{ 1 } # Stub |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
0
|
0
|
0
|
sub module { $_[0]->{module} } |
66
|
|
|
|
|
|
|
sub new { |
67
|
68
|
|
|
68
|
0
|
122
|
my $package = shift; |
68
|
68
|
|
|
|
|
487
|
my %params = @_; |
69
|
68
|
|
|
|
|
447
|
my $self = { |
70
|
|
|
|
|
|
|
session => $params{session}, |
71
|
|
|
|
|
|
|
trans_id => $params{trans_id}, |
72
|
|
|
|
|
|
|
field_id => $params{field_id}, |
73
|
|
|
|
|
|
|
}; |
74
|
|
|
|
|
|
|
|
75
|
68
|
50
|
|
|
|
699
|
return $package->_error('trans_id must be specified') unless $self->{trans_id}; |
76
|
68
|
50
|
|
|
|
224
|
return $self->_error('field_id is required') unless $self->{field_id}; |
77
|
|
|
|
|
|
|
|
78
|
68
|
50
|
|
|
|
1256
|
my $module = $MODULES{ $self->{trans_id} } or $self->_error('invalid module'); |
79
|
68
|
|
|
|
|
192
|
$self->{module} = $module; |
80
|
|
|
|
|
|
|
|
81
|
68
|
|
|
|
|
419
|
bless( $self, $package . '::' . $module ); |
82
|
|
|
|
|
|
|
|
83
|
68
|
50
|
|
|
|
441
|
return $self->_error('Failed to init ' . $module) unless $self->init; |
84
|
|
|
|
|
|
|
|
85
|
68
|
|
|
|
|
394
|
return( $self ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
#Stub |
89
|
36
|
|
|
36
|
0
|
117
|
sub init { 1 }; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
0
|
0
|
0
|
sub trans_id { return $_[0]->{trans_id} } |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub field{ |
94
|
2
|
|
|
2
|
0
|
5
|
my $self = shift; |
95
|
|
|
|
|
|
|
|
96
|
2
|
50
|
|
|
|
13
|
my $field = DBR::Config::Field->new( |
97
|
|
|
|
|
|
|
session => $self->{session}, |
98
|
|
|
|
|
|
|
field_id => $self->{field_id}, |
99
|
|
|
|
|
|
|
) or return $self->_error('failed to create field object'); |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
5
|
return $field; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub parse{ |
105
|
4
|
|
|
4
|
0
|
6
|
my $self = shift; |
106
|
4
|
|
|
|
|
8
|
my $raw = shift; |
107
|
|
|
|
|
|
|
|
108
|
4
|
|
|
|
|
18
|
my @tv = $self->backward($raw); |
109
|
|
|
|
|
|
|
|
110
|
4
|
100
|
|
|
|
16
|
return undef unless scalar(@tv) > 0; # $self->_error('failed to parse value') |
111
|
2
|
50
|
|
|
|
9
|
return $self->_error('parse does not allow multi-valued translations') if scalar(@tv) > 1; |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
107
|
my $field = $self->field; |
114
|
2
|
50
|
|
|
|
11
|
my $testsub = $field->testsub or return $self->_error('failed to retrieve testsub'); |
115
|
2
|
|
|
|
|
5
|
my $value = $tv[0]; |
116
|
|
|
|
|
|
|
|
117
|
2
|
0
|
|
|
|
619
|
$testsub->($value) or return $self->_error( |
|
|
50
|
|
|
|
|
|
118
|
|
|
|
|
|
|
"Invalid internal value " . |
119
|
|
|
|
|
|
|
( defined $value ? "'$value'" : '(undef)' ) . |
120
|
|
|
|
|
|
|
" for " . $field->name |
121
|
|
|
|
|
|
|
); |
122
|
2
|
|
|
|
|
12
|
return $self->forward($value); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |