| 1 | package App::mkfeyorm; | ||
| 2 | { | ||
| 3 | $App::mkfeyorm::VERSION = '0.010'; | ||
| 4 | } | ||
| 5 | # ABSTRACT: Make skeleton code with Fey::ORM | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use Moose::Util::TypeConstraints; | ||
| 9 | use MooseX::SemiAffordanceAccessor; | ||
| 10 | use MooseX::StrictConstructor; | ||
| 11 | use autodie; | ||
| 12 | |||
| 13 | use Data::Section -setup; | ||
| 14 | use File::Basename; | ||
| 15 | use File::Spec::Functions; | ||
| 16 | use Template; | ||
| 17 | |||
| 18 | has 'schema' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Str', | ||
| 21 | required => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | subtype 'TableRef', | ||
| 25 | as 'HashRef'; | ||
| 26 | |||
| 27 | sub _db_table_name { | ||
| 28 | my $table = shift; | ||
| 29 | |||
| 30 | $table =~ s/([A-Z]+)::/"_\L$1_"/ge; | ||
| 31 | $table =~ s/([A-Z]+)([A-Z])/"_\L$1_$2"/ge; | ||
| 32 | $table =~ s/([A-Z])/"_\L$1"/ge; | ||
| 33 | $table =~ s/::/_/g; | ||
| 34 | $table =~ s/_+/_/g; | ||
| 35 | $table =~ s/^_//; | ||
| 36 | |||
| 37 | return $table; | ||
| 38 | } | ||
| 39 | |||
| 40 | coerce 'TableRef', | ||
| 41 | from 'ArrayRef', | ||
| 42 | via { | ||
| 43 | my %result = map { $_ => _db_table_name($_) } @$_; | ||
| 44 | |||
| 45 | \%result; | ||
| 46 | }; | ||
| 47 | |||
| 48 | has 'tables' => ( | ||
| 49 | is => 'rw', | ||
| 50 | isa => 'TableRef', | ||
| 51 | required => 1, | ||
| 52 | coerce => 1, | ||
| 53 | ); | ||
| 54 | |||
| 55 | has '_db_tables' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'ArrayRef', | ||
| 58 | ); | ||
| 59 | |||
| 60 | has 'output_path' => ( | ||
| 61 | is => 'rw', | ||
| 62 | isa => 'Str', | ||
| 63 | default => 'lib', | ||
| 64 | ); | ||
| 65 | |||
| 66 | after 'set_output_path' => sub { | ||
| 67 | my ( $self, $path ) = @_; | ||
| 68 | |||
| 69 | my $tt = Template->new({ | ||
| 70 | OUTPUT_PATH => $self->output_path, | ||
| 71 | DEFAULT_ENCODING => 'utf-8', | ||
| 72 | }) || die "$Template::ERROR\n"; | ||
| 73 | |||
| 74 | $self->_set_template($tt); | ||
| 75 | }; | ||
| 76 | |||
| 77 | has 'namespace' => ( | ||
| 78 | is => 'rw', | ||
| 79 | isa => 'Str', | ||
| 80 | ); | ||
| 81 | |||
| 82 | has 'table_namespace' => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'Str', | ||
| 85 | ); | ||
| 86 | |||
| 87 | has 'schema_namespace' => ( | ||
| 88 | is => 'rw', | ||
| 89 | isa => 'Str', | ||
| 90 | ); | ||
| 91 | |||
| 92 | has 'schema_template' => ( | ||
| 93 | is => 'rw', | ||
| 94 | isa => 'Str', | ||
| 95 | default => ${ __PACKAGE__->section_data('schema.tt') }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | has 'table_template' => ( | ||
| 99 | is => 'rw', | ||
| 100 | isa => 'Str', | ||
| 101 | default => ${ __PACKAGE__->section_data('table.tt') }, | ||
| 102 | ); | ||
| 103 | |||
| 104 | has 'cache' => ( | ||
| 105 | is => 'rw', | ||
| 106 | isa => 'Bool', | ||
| 107 | default => 0, | ||
| 108 | ); | ||
| 109 | |||
| 110 | has 'template_params' => ( | ||
| 111 | is => 'rw', | ||
| 112 | isa => 'HashRef', | ||
| 113 | default => sub { {} }, | ||
| 114 | ); | ||
| 115 | |||
| 116 | has '_template' => ( | ||
| 117 | is => 'rw', | ||
| 118 | isa => 'Template', | ||
| 119 | lazy_build => 1, | ||
| 120 | ); | ||
| 121 | |||
| 122 | sub _build__template { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | my $tt = Template->new({ | ||
| 126 | OUTPUT_PATH => $self->output_path, | ||
| 127 | DEFAULT_ENCODING => 'utf-8', | ||
| 128 | }) || die "$Template::ERROR\n"; | ||
| 129 | |||
| 130 | return $tt; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub schema_module { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $full_name = join( | ||
| 137 | '::', | ||
| 138 | grep { $_ } ( | ||
| 139 | $self->namespace, | ||
| 140 | $self->schema_namespace, | ||
| 141 | $self->schema | ||
| 142 | ) | ||
| 143 | ); | ||
| 144 | |||
| 145 | return $full_name; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub table_modules { | ||
| 149 | my ( $self, @tables ) = @_; | ||
| 150 | |||
| 151 | my @full_names; | ||
| 152 | if (@tables) { | ||
| 153 | for my $table (@tables) { | ||
| 154 | my $full_name = join( | ||
| 155 | '::', | ||
| 156 | grep { $_ } ( | ||
| 157 | $self->namespace, | ||
| 158 | $self->table_namespace, | ||
| 159 | $table, | ||
| 160 | ) | ||
| 161 | ); | ||
| 162 | push @full_names, $full_name; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | for my $table ( sort keys %{ $self->tables } ) { | ||
| 167 | my $full_name = join( | ||
| 168 | '::', | ||
| 169 | grep { $_ } ( | ||
| 170 | $self->namespace, | ||
| 171 | $self->table_namespace, | ||
| 172 | $table, | ||
| 173 | ) | ||
| 174 | ); | ||
| 175 | push @full_names, $full_name; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | return @full_names; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub process { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | $self->process_schema; | ||
| 186 | $self->process_table($_, $self->tables->{$_}) for keys %{ $self->tables }; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub process_tables { | ||
| 190 | my ( $self, @tables ) = @_; | ||
| 191 | |||
| 192 | if (@tables) { | ||
| 193 | $self->process_table($_, $self->tables->{$_}) for @tables; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $self->process_table($_, $self->tables->{$_}) for keys %{ $self->tables }; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | sub process_schema { | ||
| 201 | my ( $self, $content ) = @_; | ||
| 202 | |||
| 203 | my $schema = $self->schema_module; | ||
| 204 | my @tables = $self->table_modules; | ||
| 205 | |||
| 206 | my $vars = { | ||
| 207 | SCHEMA => $schema, | ||
| 208 | TABLES => \@tables, | ||
| 209 | CACHE => $self->cache, | ||
| 210 | PARAMS => $self->template_params, | ||
| 211 | }; | ||
| 212 | |||
| 213 | $self->_template->process( | ||
| 214 | \$self->schema_template, | ||
| 215 | $vars, | ||
| 216 | ref $content eq 'SCALAR' ? $content : $self->module_path($schema), | ||
| 217 | ) or die $self->_template->error, "\n"; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub process_table { | ||
| 221 | my ( $self, $orig_table, $db_table, $content ) = @_; | ||
| 222 | |||
| 223 | $db_table = _db_table_name($orig_table) unless $db_table; | ||
| 224 | |||
| 225 | my $schema = $self->schema_module; | ||
| 226 | my ( $table ) = $self->table_modules($orig_table); | ||
| 227 | |||
| 228 | my $vars = { | ||
| 229 | SCHEMA => $schema, | ||
| 230 | TABLE => $table, | ||
| 231 | CACHE => $self->cache, | ||
| 232 | DB_TABLE => $db_table, | ||
| 233 | PARAMS => $self->template_params, | ||
| 234 | }; | ||
| 235 | |||
| 236 | $self->_template->process( | ||
| 237 | \$self->table_template, | ||
| 238 | $vars, | ||
| 239 | ref $content eq 'SCALAR' ? $content : $self->module_path($table), | ||
| 240 | ) or die $self->_template->error, "\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub module_path { | ||
| 244 | my ( $self, $module ) = @_; | ||
| 245 | |||
| 246 | return catfile( split(/::/, $module) ) . '.pm'; | ||
| 247 | } | ||
| 248 | |||
| 249 | __PACKAGE__->meta->make_immutable; | ||
| 250 | 1; | ||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | =pod | ||
| 256 | |||
| 257 | =encoding utf-8 | ||
| 258 | |||
| 259 | =head1 NAME | ||
| 260 | |||
| 261 | App::mkfeyorm - Make skeleton code with Fey::ORM | ||
| 262 | |||
| 263 | =head1 VERSION | ||
| 264 | |||
| 265 | version 0.010 | ||
| 266 | |||
| 267 | =head1 SYNOPSIS | ||
| 268 | |||
| 269 | use App::mkfeyorm; | ||
| 270 | |||
| 271 | my $app = App::mkfeyorm->new( | ||
| 272 | output_path => 'somewhere/lib', | ||
| 273 | schema => 'Schema', | ||
| 274 | tables => [qw( | ||
| 275 | MC::User | ||
| 276 | MC::Role | ||
| 277 | MC::UserRole | ||
| 278 | AE::Source | ||
| 279 | AE::Task | ||
| 280 | CM::Source | ||
| 281 | CM::Task | ||
| 282 | )], | ||
| 283 | namespace => 'MedicalCoding', | ||
| 284 | table_namespace => 'Model', | ||
| 285 | ); | ||
| 286 | |||
| 287 | $app->process; | ||
| 288 | |||
| 289 | =head1 DESCRIPTION | ||
| 290 | |||
| 291 | This module generates L<Fey::ORM> based module on the fly. | ||
| 292 | At least C<schema> and C<tables> attributes are needed. | ||
| 293 | |||
| 294 | =head1 ATTRIBUTES | ||
| 295 | |||
| 296 | =head2 schema | ||
| 297 | |||
| 298 | Schema module name (required) | ||
| 299 | |||
| 300 | my $schema_module_name = $self->schema; | ||
| 301 | $self->set_schema($schema_module_name); | ||
| 302 | |||
| 303 | =head2 tables | ||
| 304 | |||
| 305 | Table module name list (required) | ||
| 306 | |||
| 307 | my $table_module_names_ref = $self->tables; | ||
| 308 | $self->set_tables(\@table_module_names); | ||
| 309 | $self->set_tables(\%table_module_names); | ||
| 310 | |||
| 311 | =head2 output_path | ||
| 312 | |||
| 313 | Output path for generated modules. | ||
| 314 | Default output directory is C<lib>. | ||
| 315 | |||
| 316 | my $output_path = $self->output_path; | ||
| 317 | $self->set_output_path($output_path); | ||
| 318 | |||
| 319 | =head2 namespace | ||
| 320 | |||
| 321 | Namespace for schema and table module | ||
| 322 | |||
| 323 | =head2 table_namespace | ||
| 324 | |||
| 325 | Namespace for table module | ||
| 326 | |||
| 327 | =head2 schema_namespace | ||
| 328 | |||
| 329 | Namespace for schema module | ||
| 330 | |||
| 331 | =head2 schema_template | ||
| 332 | |||
| 333 | Schema template string. | ||
| 334 | If you want to use your own template file then use this attribute. | ||
| 335 | |||
| 336 | =head2 table_template | ||
| 337 | |||
| 338 | Table template string. | ||
| 339 | If you want to use your own template file then use this attribute. | ||
| 340 | |||
| 341 | =head2 cache | ||
| 342 | |||
| 343 | Use cache feature or not. Default is false. | ||
| 344 | It uses L<Storable> to save and load cache file. | ||
| 345 | |||
| 346 | =head2 template_params | ||
| 347 | |||
| 348 | Hash reference for templating. | ||
| 349 | Set this attribute if you want to use additional parameters | ||
| 350 | in your own templates. | ||
| 351 | |||
| 352 | =head1 METHODS | ||
| 353 | |||
| 354 | =head2 process | ||
| 355 | |||
| 356 | Generate the schema module & table module | ||
| 357 | |||
| 358 | my $app = App::mkfeyorm->new( | ||
| 359 | schema => 'Schema', | ||
| 360 | tables => { | ||
| 361 | User => 'user', | ||
| 362 | Role => 'role', | ||
| 363 | UserRole => 'user_role', | ||
| 364 | }, | ||
| 365 | namespace => 'Web::Blog', | ||
| 366 | table_namespace => 'Model', | ||
| 367 | ); | ||
| 368 | $app->process; | ||
| 369 | |||
| 370 | =head2 process_schema | ||
| 371 | |||
| 372 | Generate the schema module. | ||
| 373 | |||
| 374 | $app->process_schema; | ||
| 375 | |||
| 376 | =head2 process_table | ||
| 377 | |||
| 378 | Generate the talbe module. | ||
| 379 | |||
| 380 | $app->process_table; | ||
| 381 | |||
| 382 | =head2 process_tables | ||
| 383 | |||
| 384 | Generate the table module. | ||
| 385 | |||
| 386 | $app->process_tables; # generates all tables | ||
| 387 | $app->process_tables(qw/ User Role /); # generates User and Role tables | ||
| 388 | |||
| 389 | =head2 schema_module | ||
| 390 | |||
| 391 | Get full name of schema module | ||
| 392 | |||
| 393 | =head2 table_modules | ||
| 394 | |||
| 395 | Get full names of table modules | ||
| 396 | |||
| 397 | =head2 module_path | ||
| 398 | |||
| 399 | Get module path from module names | ||
| 400 | |||
| 401 | =head1 SEE ALSO | ||
| 402 | |||
| 403 | L<Fey::ORM> | ||
| 404 | |||
| 405 | =head1 AUTHOR | ||
| 406 | |||
| 407 | Keedi Kim - κΉλν <keedi@cpan.org> | ||
| 408 | |||
| 409 | =head1 COPYRIGHT AND LICENSE | ||
| 410 | |||
| 411 | This software is copyright (c) 2011 by Keedi Kim. | ||
| 412 | |||
| 413 | This is free software; you can redistribute it and/or modify it under | ||
| 414 | the same terms as the Perl 5 programming language system itself. | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | |||
| 419 | __DATA__ |