lib/MOSES/MOBY/Generators/GenTypes.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 186 CC 19 SCAR 42.8
CC
19
Cov
0%
generate
182.0
load
20.0
_attr_prop
12.0
CRAP 72.0
1 #-----------------------------------------------------------------
2 # MOSES::MOBY::Generators::GenTypes
3 # Author: Martin Senger <martin.senger@gmail.com>,
4 # Edward Kawas <edward.kawas@gmail.com>
5 #
6 # For copyright and disclaimer see below.
7 #
8 # $Id: GenTypes.pm,v 1.4 2008/04/29 19:42:56 kawas Exp $
9 #-----------------------------------------------------------------
10  
11 package MOSES::MOBY::Generators::GenTypes;
12 use MOSES::MOBY::Base;
13 use base qw( MOSES::MOBY::Base );
14 use Template;
15 use FindBin qw( $Bin );
16 use lib $Bin;
17 use File::Spec;
18 use MOSES::MOBY::Cache::Central;
19 use MOSES::MOBY::Generators::Utils;
20 use vars qw( %PRIMITIVE_TYPES );
21 use strict;
22  
23 # add versioning to this module
24 use vars qw /$VERSION/;
25 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
26  
27 #-----------------------------------------------------------------
28 # A list of allowed attribute names. See MOSES::MOBY::Base for details.
29 #-----------------------------------------------------------------
30 {
31 my %_allowed =
32 (
33 outdir => undef,
34 cachedir => undef,
35 registry => undef,
36 );
37  
38 sub _accessible {
39 my ($self, $attr) = @_;
40 exists $_allowed{$attr} or $self->SUPER::_accessible ($attr);
41 }
42 sub _attr_prop {
43 my ($self, $attr_name, $prop_name) = @_;
44 my $attr = $_allowed {$attr_name};
45 return ref ($attr) ? $attr->{$prop_name} : $attr if $attr;
46 return $self->SUPER::_attr_prop ($attr_name, $prop_name);
47 }
48 }
49  
50 #-----------------------------------------------------------------
51 # init
52 #-----------------------------------------------------------------
53 sub init {
54 my ($self) = shift;
55 $self->SUPER::init();
56 $self->cachedir ($MOBYCFG::CACHEDIR);
57 $self->registry ($MOBYCFG::REGISTRY);
58 $self->outdir ( $MOBYCFG::GENERATORS_OUTDIR ||
59 MOSES::MOBY::Generators::Utils->find_file ($Bin, 'generated') );
60 }
61  
62 #-----------------------------------------------------------------
63 # Check boolean value of a string.
64 #-----------------------------------------------------------------
65 sub _is {
66 my $value = shift;
67 return ($value =~ /true|\+|1|yes|ano/ ? '1' : '');
68 }
69  
70 #-----------------------------------------------------------------
71 # I do not generate primitive types
72 #-----------------------------------------------------------------
73 %PRIMITIVE_TYPES = ( Object => 1, Integer => 1, Boolean => 1,
74 String => 1, Float => 1, DateTime => 1 );
75  
76 #-----------------------------------------------------------------
77 # generate
78 #-----------------------------------------------------------------
79 sub generate {
80 my ($self, @args) = @_;
81 my %args =
82 ( # some default values
83 outdir => $self->outdir,
84 cachedir => $self->cachedir,
85 registry => $self->registry,
86 with_docs => 0,
87  
88 # potential args with no default values
89 # obtain_related => 1 | 0,
90 # datatype_names => ['datatype1', 'datatype2', ...]
91 # outcode => ref SCALAR
92  
93 # and the real parameters
94 @args );
95  
96 $args{obtain_related} = $args{datatype_names} ? 1 : 0
97 unless defined $args{obtain_related};
98  
99 $self->throw ("Parameter 'outcode' should be a reference to a SCALAR.")
100 if $args{outcode} and ref ($args{outcode}) ne 'SCALAR';
101  
102 # get objects from a local cache
103 my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $args{cachedir},
104 registry => $args{registry});
105 my @wanted_objs = ();
106  
107 # which 'top-level' objects we want
108 $args{datatype_names} = [ $cache->get_datatype_names ]
109 unless $args{datatype_names};
110 foreach my $name (@{ $args{datatype_names} }) {
111 push (@wanted_objs, $cache->get_datatype ($name));
112 }
113  
114 # also we want all related objects
115 @wanted_objs = @{ $cache->get_related_types (@wanted_objs)}
116 if $args{obtain_related};
117  
118 # generate from template
119 my $tt = Template->new ( ABSOLUTE => 1 );
120 my $input = File::Spec->rel2abs ( MOSES::MOBY::Generators::Utils->find_file
121 ($Bin,
122 'MOSES', 'MOBY', 'Generators', 'templates',
123 'datatype.tt') );
124 # where to generate
125 my $outdir = File::Spec->rel2abs
126 ( ( $args{outdir} ?
127 $args{outdir} :
128 MOSES::MOBY::Generators::Utils->find_file ($Bin, 'generated') ) );
129 $LOG->info ("Data types will be generated into: '$outdir'")
130 unless $args{outcode};
131  
132 # generate even when it exists?
133 my $ignore_existing = $MOBYCFG::GENERATORS_IGNORE_EXISTING_TYPES || 0;
134 $ignore_existing = _is ($ignore_existing);
135 $LOG->debug ("Ignoring pre-generated data types")
136 if $ignore_existing;
137  
138 foreach my $obj (@wanted_objs) {
139 next if exists $PRIMITIVE_TYPES{$obj->name};
140 my $module_name = $obj->module_name;
141 if ($args{outcode}) {
142 # check if the same data type is already loaded
143 next if eval '%' . $module_name . '::';
144 # check if the same data type is available pre-generated
145 eval "require $module_name" and next
146 unless $ignore_existing;
147 $LOG->debug ("$module_name will be loaded");
148 $tt->process ( $input, { obj => $obj,
149 full_source => $args{with_docs},
150 },
151 $args{outcode} ) || $LOG->logdie ($tt->error());
152 } else {
153 $LOG->debug ("$module_name will be generated");
154 # we cannot easily check whether the same file was already
155 # generated - so we don't
156 my $outfile = (File::Spec->catfile ($outdir, 'MOSES', 'MOBY', 'Data',
157 $self->module_name_escape ($obj->name) . '.pm'));
158 $tt->process ( $input, { obj => $obj,
159 full_source => 1,
160 },
161 $outfile ) || $LOG->logdie ($tt->error());
162 }
163 }
164 }
165  
166 #-----------------------------------------------------------------
167 # load
168 # load (datatype-name)
169 # load ([@datatype_names])
170 # load (cachedir => dir, datatype_names => [..], ... )
171 #-----------------------------------------------------------------
172 sub load {
173 my ($self, @args) = @_;
174 @args = (datatype_names => $args[0])
175 if ref ($args[0]) eq 'ARRAY';
176 @args = (datatype_names => [ $args[0] ])
177 if @args == 1 and !ref ($args[0]);
178  
179 my $code = '';
180 $self->generate (@args, outcode => \$code, obtain_related => 1);
181 eval $code;
182 $LOG->logdie ("$@") if $@;
183 }
184  
185 1;
186 __END__