| 1 | package Rose::DBx::Garden; | ||
| 2 | |||
| 3 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use base qw( Rose::DB::Object::Loader ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use Carp; | ||
| 7 | use Data::Dump qw( dump ); | ||
| 8 | use Path::Class; | ||
| 9 | use File::Slurp::Tiny; | ||
| 10 | use File::Basename; | ||
| 11 | |||
| 12 | my $MAX_FIELD_SIZE = 64; | ||
| 13 | |||
| 14 | use Rose::Object::MakeMethods::Generic ( | ||
| 15 | boolean => [ | ||
| 16 | 'find_schemas' => { default => 0 }, | ||
| 17 | 'force_install' => { default => 0 }, | ||
| 18 | 'debug' => { default => 0 }, | ||
| 19 | 'skip_map_class_forms' => { default => 1 }, | ||
| 20 | 'include_autoinc_form_fields' => { default => 1 }, | ||
| 21 | ], | ||
| 22 | 'scalar --get_set_init' => 'column_field_map', | ||
| 23 | 'scalar --get_set_init' => 'column_to_label', | ||
| 24 | 'scalar --get_set_init' => 'garden_prefix', | ||
| 25 | 'scalar --get_set_init' => 'perltidy_opts', | ||
| 26 | 'scalar --get_set_init' => 'base_code', | ||
| 27 | 'scalar --get_set_init' => 'base_form_class_code', | ||
| 28 | 'scalar --get_set_init' => 'text_field_size', | ||
| 29 | 'scalar --get_set_init' => 'limit_to_schemas', | ||
| 30 | 'scalar' => 'use_db_name', | ||
| 31 | ); | ||
| 32 | |||
| 33 | our $VERSION = '0.193'; | ||
| 34 | |||
| 35 | =head1 NAME | ||
| 36 | |||
| 37 | Rose::DBx::Garden - bootstrap Rose::DB::Object and Rose::HTML::Form classes | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Rose::DBx::Garden; | ||
| 42 | |||
| 43 | my $garden = Rose::DBx::Garden->new( | ||
| 44 | |||
| 45 | garden_prefix => 'MyRoseGarden', # instead of class_prefix | ||
| 46 | perltidy_opts => '-pbp -nst -nse', # Perl Best Practices | ||
| 47 | db => My::DB->new, # Rose::DB object | ||
| 48 | find_schemas => 0, # set true if your db has schemas | ||
| 49 | force_install => 0, # do not overwrite existing files | ||
| 50 | debug => 0, # print actions on stderr | ||
| 51 | skip_map_class_forms => 1, # no Form classes for many2many map classes | ||
| 52 | include_autoinc_form_fields => 1, | ||
| 53 | # other Rose::DB::Object::Loader params here | ||
| 54 | |||
| 55 | ); | ||
| 56 | |||
| 57 | # $garden ISA Rose::DB::Object::Loader | ||
| 58 | |||
| 59 | $garden->plant('path/to/where/i/want/files'); | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | Rose::DBx::Garden bootstraps L<Rose::DB::Object> and L<Rose::HTML::Form> based projects. | ||
| 64 | The idea is that you can point the module at a database and end up with work-able | ||
| 65 | RDBO and Form classes with a single method call. | ||
| 66 | |||
| 67 | Rose::DBx::Garden creates scaffolding only. | ||
| 68 | It creates Rose::DB::Object-based and Rose::HTML::Object-based classes, which | ||
| 69 | assume 1 table == 1 form. There is no generation of code to handle | ||
| 70 | subforms, though it's relatively easy to add those later. | ||
| 71 | |||
| 72 | Rose::DBx::Garden inherits from L<Rose::DB::Object::Loader>, so all the magic there | ||
| 73 | is also available here. | ||
| 74 | |||
| 75 | =head1 METHODS | ||
| 76 | |||
| 77 | B<NOTE:> All the init_* methods are intended for when you subclass the Garden | ||
| 78 | class. You can pass in values to the new() constructor for normal use. | ||
| 79 | See L<Rose::Object::MakeMethods::Generic>. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | =head2 include_autoinc_form_fields | ||
| 84 | |||
| 85 | The default behaviour is to include db columns flagged as | ||
| 86 | auto_increment from the generated Form class and to map | ||
| 87 | them to the 'serial' field type. Set this value | ||
| 88 | to a false value to exclude auto_increment columns as form fields. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | =head2 init_column_field_map | ||
| 93 | |||
| 94 | Sets the default RDBO column type to RHTMLO field type mapping. | ||
| 95 | Should be a hash ref of 'rdbo' => 'rhtmlo' format. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | # TODO better detection of the serial type on per-db basis | ||
| 100 | sub init_column_field_map { | ||
| 101 | return { | ||
| 102 | 'varchar' => 'text', | ||
| 103 | 'text' => 'textarea', | ||
| 104 | 'character' => 'text', | ||
| 105 | 'date' => 'date', | ||
| 106 | 'datetime' => 'datetime', | ||
| 107 | 'epoch' => 'datetime', | ||
| 108 | 'integer' => 'integer', | ||
| 109 | 'bigint' => 'integer', | ||
| 110 | 'serial' => 'serial', | ||
| 111 | 'time' => 'time', | ||
| 112 | 'timestamp' => 'datetime', | ||
| 113 | 'float' => 'numeric', # TODO nice to have ::Field::Float | ||
| 114 | 'numeric' => 'numeric', | ||
| 115 | 'decimal' => 'numeric', | ||
| 116 | 'double precision' => 'numeric', | ||
| 117 | 'boolean' => 'boolean', | ||
| 118 | 'enum' => 'menu', | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 init_column_to_label | ||
| 123 | |||
| 124 | Returns a CODE ref for filtering a column name to its corresponding | ||
| 125 | form field label. The CODE ref should expect two arguments: | ||
| 126 | the Garden object and the column name. | ||
| 127 | |||
| 128 | The default is just to return the column name. If you wanted to return, | ||
| 129 | for example, a prettier version aligned with the naming conventions used | ||
| 130 | in Rose::DB::Object::ConventionManager, you might do something like: | ||
| 131 | |||
| 132 | my $garden = Rose::DBx::Garden->new( | ||
| 133 | column_to_label => sub { | ||
| 134 | my ($garden_obj, $col_name) = @_; | ||
| 135 | return join(' ', | ||
| 136 | map { ucfirst($_) } | ||
| 137 | split(m/_/, $col_name) | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | ); | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub init_column_to_label { | ||
| 145 | sub { return $_[1] } | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 init_garden_prefix | ||
| 149 | |||
| 150 | The default base class name is C<MyRoseGarden>. This value | ||
| 151 | overrides C<class_prefix> and C<base_class> in the base Loader class. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub init_garden_prefix {'MyRoseGarden'} | ||
| 156 | |||
| 157 | =head2 init_perltidy_opts | ||
| 158 | |||
| 159 | If set, Perl::Tidy will be called to format all generated code. The | ||
| 160 | value of perltidy_opts should be the same as the command-line options | ||
| 161 | to perltidy. | ||
| 162 | |||
| 163 | The default is 0 (no run through Perl::Tidy). | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub init_perltidy_opts {0} | ||
| 168 | |||
| 169 | =head2 init_text_field_size | ||
| 170 | |||
| 171 | Tie the size and maxlength of text input fields to the allowed length | ||
| 172 | of text columns. Should be set to an integer corresponding to the max | ||
| 173 | size of a text field. The default is 64. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub init_text_field_size {$MAX_FIELD_SIZE} | ||
| 178 | |||
| 179 | =head2 init_base_code | ||
| 180 | |||
| 181 | The return value is inserted into the base RDBO class created. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub init_base_code {''} | ||
| 186 | |||
| 187 | =head2 init_base_form_class_code | ||
| 188 | |||
| 189 | The return value is inserted into the base RHTMLO class created. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub init_base_form_class_code { | ||
| 194 | return <<EOF | ||
| 195 | use base qw( Rose::HTML::Form ); | ||
| 196 | |||
| 197 | EOF | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 init_limit_to_schemas | ||
| 201 | |||
| 202 | The default return value is an empty arrayref, which is interpreted | ||
| 203 | as "all schemas" if the B<find_schemas> flag is true. | ||
| 204 | |||
| 205 | Otherwise, you may explicitly name an array of schema names to limit | ||
| 206 | the code generated to only those schemas you want. B<Must> be used | ||
| 207 | with B<find_schemas> set to true. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub init_limit_to_schemas { [] } | ||
| 212 | |||
| 213 | =head2 use_db_name( I<name> ) | ||
| 214 | |||
| 215 | Define an explicit database name to use when generating class names. | ||
| 216 | The default is taken from the Rose::DB connection information. | ||
| 217 | B<NOTE:>This does not affect the db connection, only the string used | ||
| 218 | in constructing class names. | ||
| 219 | |||
| 220 | B<NOTE:>This option is ignored if find_schemas() is true. | ||
| 221 | |||
| 222 | =head2 plant( I<path> ) | ||
| 223 | |||
| 224 | I<path> will override module_dir() if set in new(). | ||
| 225 | |||
| 226 | Returns a hash ref of all the class names created, in the format: | ||
| 227 | |||
| 228 | RDBO::Class => RHTMLO::Class | ||
| 229 | |||
| 230 | If no RHTMLO class was created the hash value will be '1'. | ||
| 231 | |||
| 232 | =head2 make_garden | ||
| 233 | |||
| 234 | An alias for plant(). | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | *make_garden = \&plant; | ||
| 239 | |||
| 240 | sub plant { | ||
| 241 | my $self = shift; | ||
| 242 | my $path = shift or croak "path required"; | ||
| 243 | |||
| 244 | #carp "path = $path"; | ||
| 245 | |||
| 246 | my $path_obj = dir($path); | ||
| 247 | |||
| 248 | $path_obj->mkpath( $self->debug ); | ||
| 249 | |||
| 250 | if ( !-w "$path_obj" or !-d "$path_obj" ) { | ||
| 251 | croak("$path_obj is not a write-able directory: $!"); | ||
| 252 | } | ||
| 253 | |||
| 254 | # make sure we can 'require' files we generate | ||
| 255 | unshift( @INC, $path ); | ||
| 256 | |||
| 257 | # set in loader just in case | ||
| 258 | $self->module_dir($path); | ||
| 259 | |||
| 260 | my $garden_prefix = $self->garden_prefix; | ||
| 261 | |||
| 262 | # setup the base RDBO class | ||
| 263 | my $base_code = $self->base_code; | ||
| 264 | my $db = $self->db or croak "db required"; | ||
| 265 | my $db_class = $db->class; | ||
| 266 | my $new_method = $db->can('new_or_cached') ? 'new_or_cached' : 'new'; | ||
| 267 | my $db_type = $db->type; | ||
| 268 | my $db_domain = $db->domain; | ||
| 269 | |||
| 270 | # make the base class unless it already exists | ||
| 271 | my $base_template = <<EOF; | ||
| 272 | package $garden_prefix; | ||
| 273 | use strict; | ||
| 274 | use base qw( Rose::DB::Object ); | ||
| 275 | use $db_class; | ||
| 276 | |||
| 277 | sub init_db { | ||
| 278 | ${db_class}->$new_method( type => '$db_type', domain => '$db_domain' ) | ||
| 279 | } | ||
| 280 | |||
| 281 | =head2 garden_prefix | ||
| 282 | |||
| 283 | Returns the garden_prefix() value with which this class was created. | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | sub garden_prefix { '${garden_prefix}' } | ||
| 288 | |||
| 289 | $base_code | ||
| 290 | |||
| 291 | EOF | ||
| 292 | |||
| 293 | # append metadata if we are using schemas | ||
| 294 | if ( $self->find_schemas ) { | ||
| 295 | |||
| 296 | $base_template .= <<EOF; | ||
| 297 | |||
| 298 | use ${garden_prefix}::Metadata; | ||
| 299 | sub meta_class { '${garden_prefix}::Metadata' } | ||
| 300 | |||
| 301 | EOF | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 | # need a 1 no matter what | ||
| 306 | $base_template .= "\n1;\n"; | ||
| 307 | |||
| 308 | $self->_make_file( $garden_prefix, $base_template ) | ||
| 309 | unless ( defined $base_code && $base_code eq '0' ); | ||
| 310 | |||
| 311 | # find all schemas if this db supports them | ||
| 312 | my %schemas; | ||
| 313 | if ( $self->find_schemas and !scalar @{ $self->limit_to_schemas } ) { | ||
| 314 | my %native = ( information_schema => 1, pg_catalog => 1 ); | ||
| 315 | my $info = $db->dbh->table_info( undef, '%', undef, 'TABLE' ) | ||
| 316 | ->fetchall_arrayref; | ||
| 317 | |||
| 318 | #carp dump $info; | ||
| 319 | |||
| 320 | for my $row (@$info) { | ||
| 321 | next if exists $native{ $row->[1] }; | ||
| 322 | $schemas{ $row->[1] }++; | ||
| 323 | } | ||
| 324 | |||
| 325 | # only need custom metadata if we are using schemas | ||
| 326 | $self->_make_file( join( '::', $garden_prefix, 'Metadata' ), | ||
| 327 | $self->_metadata_template ); | ||
| 328 | |||
| 329 | } | ||
| 330 | |||
| 331 | # if we are using schemas and have explicitly named them already, | ||
| 332 | # then use what was specified. | ||
| 333 | elsif ( $self->find_schemas ) { | ||
| 334 | |||
| 335 | $schemas{$_}++ for @{ $self->limit_to_schemas }; | ||
| 336 | |||
| 337 | $self->_make_file( join( '::', $garden_prefix, 'Metadata' ), | ||
| 338 | $self->_metadata_template ); | ||
| 339 | |||
| 340 | } | ||
| 341 | elsif ( $self->use_db_name ) { | ||
| 342 | %schemas = ( $self->use_db_name => '' ); | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | |||
| 346 | my $dbname = $db->database; | ||
| 347 | $dbname =~ s!.*/!!; | ||
| 348 | $dbname =~ s/\W/_/g; | ||
| 349 | %schemas = ( $dbname => '' ); | ||
| 350 | } | ||
| 351 | |||
| 352 | my (%created_classes); | ||
| 353 | |||
| 354 | my $preamble = $self->module_preamble; | ||
| 355 | my $postamble = $self->module_postamble; | ||
| 356 | |||
| 357 | $Rose::DB::Object::Loader::Debug = $self->debug || $ENV{PERL_DEBUG} || 0; | ||
| 358 | |||
| 359 | my @classes; | ||
| 360 | |||
| 361 | for my $schema ( keys %schemas ) { | ||
| 362 | |||
| 363 | #carp "working on schema $schema"; | ||
| 364 | |||
| 365 | my $schema_class | ||
| 366 | = $schema | ||
| 367 | ? join( '::', $garden_prefix, ucfirst($schema) ) | ||
| 368 | : $garden_prefix; | ||
| 369 | |||
| 370 | if ($schema) { | ||
| 371 | my $schema_tmpl | ||
| 372 | = $self->_schema_template( $garden_prefix, $schema_class, | ||
| 373 | $schema ); | ||
| 374 | |||
| 375 | $self->_make_file( $schema_class, $schema_tmpl ); | ||
| 376 | $self->db_schema($schema) if $self->find_schemas; | ||
| 377 | } | ||
| 378 | |||
| 379 | #carp "schema_class: $schema_class"; | ||
| 380 | |||
| 381 | $self->class_prefix($schema_class); | ||
| 382 | $self->base_class($schema_class); # already wrote it, so can require | ||
| 383 | |||
| 384 | push @classes, $self->make_classes; | ||
| 385 | } | ||
| 386 | |||
| 387 | #carp dump \@classes; | ||
| 388 | |||
| 389 | for my $class (@classes) { | ||
| 390 | |||
| 391 | #carp "class: $class"; | ||
| 392 | |||
| 393 | my $template = ''; | ||
| 394 | my $this_preamble = ''; | ||
| 395 | my $this_postamble = ''; | ||
| 396 | |||
| 397 | if ( $class->isa('Rose::DB::Object') ) { | ||
| 398 | |||
| 399 | $template | ||
| 400 | = $class->meta->perl_class_definition( indent => 4 ) . "\n"; | ||
| 401 | |||
| 402 | if ($preamble) { | ||
| 403 | $this_preamble | ||
| 404 | = ref $preamble eq 'CODE' | ||
| 405 | ? $preamble->( $class->meta ) | ||
| 406 | : $preamble; | ||
| 407 | } | ||
| 408 | |||
| 409 | if ($postamble) { | ||
| 410 | $this_postamble | ||
| 411 | = ref $postamble eq 'CODE' | ||
| 412 | ? $postamble->( $class->meta ) | ||
| 413 | : $postamble; | ||
| 414 | } | ||
| 415 | |||
| 416 | $created_classes{$class} = 1; | ||
| 417 | } | ||
| 418 | elsif ( $class->isa('Rose::DB::Object::Manager') ) { | ||
| 419 | $template = $class->perl_class_definition( indent => 4 ) . "\n"; | ||
| 420 | |||
| 421 | if ($preamble) { | ||
| 422 | $this_preamble | ||
| 423 | = ref $preamble eq 'CODE' | ||
| 424 | ? $preamble->( $class->object_class->meta ) | ||
| 425 | : $preamble; | ||
| 426 | } | ||
| 427 | |||
| 428 | if ($postamble) { | ||
| 429 | $this_postamble | ||
| 430 | = ref $postamble eq 'CODE' | ||
| 431 | ? $postamble->( $class->object_class->meta ) | ||
| 432 | : $postamble; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | else { | ||
| 436 | croak "class $class not supported"; | ||
| 437 | } | ||
| 438 | |||
| 439 | $self->_make_file( $class, | ||
| 440 | $this_preamble . $template . $this_postamble ); | ||
| 441 | } | ||
| 442 | |||
| 443 | # RDBO classes all done. That was the easy part. | ||
| 444 | # now create a RHTMLO::Form tree using the same model. | ||
| 445 | |||
| 446 | # first create the base ::Form class. | ||
| 447 | my $base_form_class = join( '::', $garden_prefix, 'Form' ); | ||
| 448 | my $base_form_class_code = $self->base_form_class_code; | ||
| 449 | my $base_form_template = <<EOF; | ||
| 450 | package $base_form_class; | ||
| 451 | use strict; | ||
| 452 | |||
| 453 | $base_form_class_code | ||
| 454 | |||
| 455 | 1; | ||
| 456 | |||
| 457 | # generated by Rose::DBx::Garden | ||
| 458 | |||
| 459 | EOF | ||
| 460 | |||
| 461 | $self->_make_file( $base_form_class, $base_form_template ); | ||
| 462 | |||
| 463 | # second create a subclass of base ::Form for each RDBO class. | ||
| 464 | for my $rdbo_class ( keys %created_classes ) { | ||
| 465 | |||
| 466 | if ( $self->convention_manager->is_map_class($rdbo_class) | ||
| 467 | and $self->skip_map_class_forms ) | ||
| 468 | { | ||
| 469 | print " ... skipping map_class $rdbo_class\n"; | ||
| 470 | next; | ||
| 471 | } | ||
| 472 | my $form_class = join( '::', $rdbo_class, 'Form' ); | ||
| 473 | my $form_template = $self->_form_template( $rdbo_class, $form_class, | ||
| 474 | $base_form_class ); | ||
| 475 | |||
| 476 | $created_classes{$rdbo_class} = $form_class; | ||
| 477 | |||
| 478 | $self->_make_file( $form_class, $form_template ); | ||
| 479 | } | ||
| 480 | |||
| 481 | return \%created_classes; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub _metadata_template { | ||
| 485 | my $self = shift; | ||
| 486 | my $base_rdbo_class = $self->garden_prefix; | ||
| 487 | |||
| 488 | return <<EOF; | ||
| 489 | package ${base_rdbo_class}::Metadata; | ||
| 490 | |||
| 491 | use strict; | ||
| 492 | use warnings; | ||
| 493 | |||
| 494 | use base qw( Rose::DB::Object::Metadata ); | ||
| 495 | |||
| 496 | sub setup { | ||
| 497 | my \$self = shift; | ||
| 498 | my \$schema = \$self->class->schema; | ||
| 499 | \$self->SUPER::setup( \@_, schema => \$schema ); | ||
| 500 | } | ||
| 501 | |||
| 502 | 1; | ||
| 503 | |||
| 504 | EOF | ||
| 505 | } | ||
| 506 | |||
| 507 | sub _form_template { | ||
| 508 | my ( $self, $rdbo_class, $form_class, $base_form_class ) = @_; | ||
| 509 | |||
| 510 | # load the rdbo class and examine its metadata. | ||
| 511 | |||
| 512 | # make sure rdbo_class is loaded | ||
| 513 | eval "require $rdbo_class"; | ||
| 514 | croak "can't load $rdbo_class: $@" if $@; | ||
| 515 | |||
| 516 | my $object_name | ||
| 517 | = $self->convention_manager->class_to_table_singular($rdbo_class); | ||
| 518 | |||
| 519 | # create a form template using the column definitions | ||
| 520 | # as seed for the form field definitions | ||
| 521 | # use the convention manager to assign default field labels | ||
| 522 | |||
| 523 | my $form = <<EOF; | ||
| 524 | package $form_class; | ||
| 525 | use strict; | ||
| 526 | use base qw( $base_form_class ); | ||
| 527 | |||
| 528 | sub object_class { '$rdbo_class' } | ||
| 529 | |||
| 530 | sub init_with_${object_name} { | ||
| 531 | my \$self = shift; | ||
| 532 | \$self->init_with_object(\@_); | ||
| 533 | } | ||
| 534 | |||
| 535 | sub ${object_name}_from_form { | ||
| 536 | my \$self = shift; | ||
| 537 | \$self->object_from_form(\@_); | ||
| 538 | } | ||
| 539 | |||
| 540 | sub build_form { | ||
| 541 | my \$self = shift; | ||
| 542 | |||
| 543 | \$self->add_fields( | ||
| 544 | |||
| 545 | EOF | ||
| 546 | |||
| 547 | my @fields; | ||
| 548 | my $count = 0; | ||
| 549 | for my $column ( sort __by_position $rdbo_class->meta->columns ) { | ||
| 550 | push( @fields, $self->_column_to_field( $column, ++$count ) ); | ||
| 551 | } | ||
| 552 | |||
| 553 | $form .= join( "\n", @fields ); | ||
| 554 | $form .= <<EOF; | ||
| 555 | ); | ||
| 556 | |||
| 557 | return \$self->SUPER::build_form(\@_); | ||
| 558 | } | ||
| 559 | |||
| 560 | 1; | ||
| 561 | |||
| 562 | EOF | ||
| 563 | |||
| 564 | return $form; | ||
| 565 | } | ||
| 566 | |||
| 567 | # keep columns in same order they appear in db | ||
| 568 | sub __by_position { | ||
| 569 | my $pos1 = $a->ordinal_position; | ||
| 570 | my $pos2 = $b->ordinal_position; | ||
| 571 | |||
| 572 | if ( defined $pos1 && defined $pos2 ) { | ||
| 573 | return $pos1 <=> $pos2 || lc( $a->name ) cmp lc( $b->name ); | ||
| 574 | } | ||
| 575 | |||
| 576 | return lc( $a->name ) cmp lc( $b->name ); | ||
| 577 | } | ||
| 578 | |||
| 579 | sub _column_to_field { | ||
| 580 | my ( $self, $column, $tabindex ) = @_; | ||
| 581 | my $col_type = $column->type; | ||
| 582 | my $type = $self->column_field_map->{$col_type} || 'text'; | ||
| 583 | my $field_maker = 'garden_' . $type . '_field'; | ||
| 584 | my $label_maker = $self->column_to_label; | ||
| 585 | my $label = $label_maker->( $self, $column->name ); | ||
| 586 | |||
| 587 | unless ( $self->can($field_maker) ) { | ||
| 588 | $field_maker = 'garden_default_field'; | ||
| 589 | } | ||
| 590 | |||
| 591 | if ( $col_type eq 'serial' and !$self->include_autoinc_form_fields ) { | ||
| 592 | return ''; | ||
| 593 | } | ||
| 594 | |||
| 595 | return $self->$field_maker( $column, $label, $tabindex ); | ||
| 596 | } | ||
| 597 | |||
| 598 | =head2 garden_default_field( I<column>, I<label>, I<tabindex> ) | ||
| 599 | |||
| 600 | Returns the Perl code text for creating a generic Form field. | ||
| 601 | |||
| 602 | =cut | ||
| 603 | |||
| 604 | sub garden_default_field { | ||
| 605 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 606 | my $col_type = $column->type; | ||
| 607 | my $type = $self->column_field_map->{$col_type} || 'text'; | ||
| 608 | my $name = $column->name; | ||
| 609 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 610 | my $length = $column->can('length') ? $column->length() : 0; | ||
| 611 | my $maxlen = $self->text_field_size; | ||
| 612 | if ( defined $length ) { | ||
| 613 | $maxlen = $length; | ||
| 614 | } | ||
| 615 | $length = 24 unless defined $length; # 24 holds a timestamp | ||
| 616 | |||
| 617 | if ( $length > $MAX_FIELD_SIZE ) { | ||
| 618 | $length = $MAX_FIELD_SIZE; | ||
| 619 | } | ||
| 620 | |||
| 621 | return <<EOF; | ||
| 622 | $name => { | ||
| 623 | id => '$name', | ||
| 624 | type => '$type', | ||
| 625 | class => '$col_type', | ||
| 626 | label => '$label', | ||
| 627 | tabindex => $tabindex, | ||
| 628 | rank => $tabindex, | ||
| 629 | size => $length, | ||
| 630 | maxlength => $maxlen, | ||
| 631 | description => q{$desc}, | ||
| 632 | }, | ||
| 633 | EOF | ||
| 634 | } | ||
| 635 | |||
| 636 | =head2 garden_numeric_field( I<column>, I<label>, I<tabindex> ) | ||
| 637 | |||
| 638 | Returns the Perl code text for creating a numeric Form field. | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | sub garden_numeric_field { | ||
| 643 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 644 | my $col_type = $column->type; | ||
| 645 | my $type = $self->column_field_map->{$col_type} || 'text'; | ||
| 646 | my $name = $column->name; | ||
| 647 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 648 | |||
| 649 | return <<EOF; | ||
| 650 | $name => { | ||
| 651 | id => '$name', | ||
| 652 | type => '$type', | ||
| 653 | class => '$col_type', | ||
| 654 | label => '$label', | ||
| 655 | tabindex => $tabindex, | ||
| 656 | rank => $tabindex, | ||
| 657 | size => 16, | ||
| 658 | maxlength => 32, | ||
| 659 | description => q{$desc}, | ||
| 660 | }, | ||
| 661 | EOF | ||
| 662 | } | ||
| 663 | |||
| 664 | =head2 garden_boolean_field( I<column>, I<label>, I<tabindex> ) | ||
| 665 | |||
| 666 | Returns the Perl code text for creating a boolean Form field. | ||
| 667 | |||
| 668 | =cut | ||
| 669 | |||
| 670 | sub garden_boolean_field { | ||
| 671 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 672 | my $col_type = $column->type; | ||
| 673 | my $name = $column->name; | ||
| 674 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 675 | |||
| 676 | return <<EOF; | ||
| 677 | $name => { | ||
| 678 | id => '$name', | ||
| 679 | type => 'boolean', | ||
| 680 | label => '$label', | ||
| 681 | tabindex => $tabindex, | ||
| 682 | rank => $tabindex, | ||
| 683 | class => '$col_type', | ||
| 684 | description => q{$desc}, | ||
| 685 | }, | ||
| 686 | EOF | ||
| 687 | } | ||
| 688 | |||
| 689 | =head2 garden_text_field( I<column>, I<label>, I<tabindex> ) | ||
| 690 | |||
| 691 | Returns the Perl code text for creating a text Form field. | ||
| 692 | |||
| 693 | =cut | ||
| 694 | |||
| 695 | sub garden_text_field { | ||
| 696 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 697 | my $col_type = $column->type; | ||
| 698 | my $name = $column->name; | ||
| 699 | my $length = $column->can('length') ? $column->length() : 0; | ||
| 700 | $length = 0 unless defined $length; | ||
| 701 | my $maxlen = $self->text_field_size; | ||
| 702 | if ( defined $length ) { | ||
| 703 | $maxlen = $length; | ||
| 704 | } | ||
| 705 | if ( $length > $MAX_FIELD_SIZE ) { | ||
| 706 | $length = $MAX_FIELD_SIZE; | ||
| 707 | } | ||
| 708 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 709 | |||
| 710 | return <<EOF; | ||
| 711 | $name => { | ||
| 712 | id => '$name', | ||
| 713 | type => 'text', | ||
| 714 | class => '$col_type', | ||
| 715 | label => '$label', | ||
| 716 | tabindex => $tabindex, | ||
| 717 | rank => $tabindex, | ||
| 718 | size => $length, | ||
| 719 | maxlength => $maxlen, | ||
| 720 | description => q{$desc}, | ||
| 721 | }, | ||
| 722 | EOF | ||
| 723 | } | ||
| 724 | |||
| 725 | =head2 garden_menu_field( I<column>, I<label>, I<tabindex> ) | ||
| 726 | |||
| 727 | Returns the Perl code text for creating a menu Form field. | ||
| 728 | |||
| 729 | =cut | ||
| 730 | |||
| 731 | sub garden_menu_field { | ||
| 732 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 733 | my $col_type = $column->type; | ||
| 734 | my $name = $column->name; | ||
| 735 | my $options = dump $column->values; | ||
| 736 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 737 | |||
| 738 | #dump $column; | ||
| 739 | |||
| 740 | return <<EOF; | ||
| 741 | $name => { | ||
| 742 | id => '$name', | ||
| 743 | type => 'menu', | ||
| 744 | class => '$col_type', | ||
| 745 | label => '$label', | ||
| 746 | tabindex => $tabindex, | ||
| 747 | rank => $tabindex, | ||
| 748 | options => $options, | ||
| 749 | description => q{$desc}, | ||
| 750 | }, | ||
| 751 | EOF | ||
| 752 | } | ||
| 753 | |||
| 754 | =head2 garden_textarea_field( I<column>, I<label>, I<tabindex> ) | ||
| 755 | |||
| 756 | Returns Perl code for textarea field. | ||
| 757 | |||
| 758 | =cut | ||
| 759 | |||
| 760 | sub garden_textarea_field { | ||
| 761 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 762 | my $col_type = $column->type; | ||
| 763 | my $name = $column->name; | ||
| 764 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 765 | |||
| 766 | return <<EOF; | ||
| 767 | $name => { | ||
| 768 | id => '$name', | ||
| 769 | type => 'textarea', | ||
| 770 | class => '$col_type', | ||
| 771 | label => '$label', | ||
| 772 | tabindex => $tabindex, | ||
| 773 | rank => $tabindex, | ||
| 774 | size => $MAX_FIELD_SIZE . 'x8', | ||
| 775 | description => q{$desc}, | ||
| 776 | }, | ||
| 777 | EOF | ||
| 778 | } | ||
| 779 | |||
| 780 | =head2 garden_hidden_field( I<column>, I<label>, I<tabindex> ) | ||
| 781 | |||
| 782 | Returns the Perl code text for creating a hidden Form field. | ||
| 783 | |||
| 784 | =cut | ||
| 785 | |||
| 786 | sub garden_hidden_field { | ||
| 787 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 788 | my $col_type = $column->type; | ||
| 789 | my $name = $column->name; | ||
| 790 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 791 | return <<EOF; | ||
| 792 | $name => { | ||
| 793 | id => '$name', | ||
| 794 | type => 'hidden', | ||
| 795 | class => '$col_type', | ||
| 796 | label => '$label', | ||
| 797 | rank => $tabindex, | ||
| 798 | description => q{$desc}, | ||
| 799 | }, | ||
| 800 | EOF | ||
| 801 | } | ||
| 802 | |||
| 803 | =head2 garden_serial_field( I<column>, I<label>, I<tabindex> ) | ||
| 804 | |||
| 805 | Returns the Perl code text for creating a serial Form field. | ||
| 806 | |||
| 807 | =cut | ||
| 808 | |||
| 809 | sub garden_serial_field { | ||
| 810 | my ( $self, $column, $label, $tabindex ) = @_; | ||
| 811 | my $col_type = $column->type; | ||
| 812 | my $name = $column->name; | ||
| 813 | my $desc = $column->can('remarks') ? ( $column->remarks || '' ) : ''; | ||
| 814 | |||
| 815 | return <<EOF; | ||
| 816 | $name => { | ||
| 817 | id => '$name', | ||
| 818 | type => 'serial', | ||
| 819 | class => '$col_type', | ||
| 820 | label => '$label', | ||
| 821 | rank => $tabindex, | ||
| 822 | description => q{$desc}, | ||
| 823 | }, | ||
| 824 | EOF | ||
| 825 | } | ||
| 826 | |||
| 827 | sub _schema_template { | ||
| 828 | my ( $self, $base, $package, $schema ) = @_; | ||
| 829 | |||
| 830 | my @other_base = grep { !m/LoaderGenerated/ } @{ $self->base_classes }; | ||
| 831 | if (@other_base) { | ||
| 832 | $base .= ' ' . join( ' ', @other_base ); | ||
| 833 | } | ||
| 834 | |||
| 835 | return <<EOF; | ||
| 836 | package $package; | ||
| 837 | use strict; | ||
| 838 | use base qw( $base ); | ||
| 839 | |||
| 840 | sub schema { '$schema' } | ||
| 841 | |||
| 842 | 1; | ||
| 843 | EOF | ||
| 844 | } | ||
| 845 | |||
| 846 | sub _make_file { | ||
| 847 | my ( $self, $class, $buffer ) = @_; | ||
| 848 | ( my $file = $class ) =~ s,::,/,g; | ||
| 849 | $file .= '.pm'; | ||
| 850 | |||
| 851 | my ( $name, $path, $suffix ) = fileparse( $file, qr{\.pm} ); | ||
| 852 | |||
| 853 | my $fullpath = dir( $self->module_dir, $path ); | ||
| 854 | |||
| 855 | unless ( $self->force_install ) { | ||
| 856 | if ( -s $file ) { | ||
| 857 | print " ... skipping $class ($file)\n"; | ||
| 858 | return; | ||
| 859 | } | ||
| 860 | } | ||
| 861 | |||
| 862 | $fullpath->mkpath( $self->debug ) if $path; | ||
| 863 | |||
| 864 | if ( $self->perltidy_opts ) { | ||
| 865 | require Perl::Tidy; | ||
| 866 | my $newbuf; | ||
| 867 | Perl::Tidy::perltidy( | ||
| 868 | source => \$buffer, | ||
| 869 | destination => \$newbuf, | ||
| 870 | argv => $self->perltidy_opts | ||
| 871 | ); | ||
| 872 | $buffer = $newbuf; | ||
| 873 | } | ||
| 874 | |||
| 875 | my $file_to_write = file( $self->module_dir, $file )->stringify; | ||
| 876 | |||
| 877 | File::Slurp::Tiny::write_file( $file_to_write, $buffer ); | ||
| 878 | |||
| 879 | print "$class written to $file\n"; | ||
| 880 | } | ||
| 881 | |||
| 882 | =head1 AUTHORS | ||
| 883 | |||
| 884 | Peter Karman, C<< <karman at cpan.org> >> | ||
| 885 | |||
| 886 | =head1 BUGS | ||
| 887 | |||
| 888 | Please report any bugs or feature requests to | ||
| 889 | C<bug-rose-dbx-garden at rt.cpan.org>, or through the web interface at | ||
| 890 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rose-DBx-Garden>. | ||
| 891 | I will be notified, and then you'll automatically be notified of progress on | ||
| 892 | your bug as I make changes. | ||
| 893 | |||
| 894 | =head1 SUPPORT | ||
| 895 | |||
| 896 | You can find documentation for this module with the perldoc command. | ||
| 897 | |||
| 898 | perldoc Rose::DBx::Garden | ||
| 899 | |||
| 900 | You can also look for information at: | ||
| 901 | |||
| 902 | =over 4 | ||
| 903 | |||
| 904 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 905 | |||
| 906 | L<http://annocpan.org/dist/Rose-DBx-Garden> | ||
| 907 | |||
| 908 | =item * CPAN Ratings | ||
| 909 | |||
| 910 | L<http://cpanratings.perl.org/d/Rose-DBx-Garden> | ||
| 911 | |||
| 912 | =item * RT: CPAN's request tracker | ||
| 913 | |||
| 914 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-DBx-Garden> | ||
| 915 | |||
| 916 | =item * Search CPAN | ||
| 917 | |||
| 918 | L<http://search.cpan.org/dist/Rose-DBx-Garden> | ||
| 919 | |||
| 920 | =back | ||
| 921 | |||
| 922 | =head1 ACKNOWLEDGEMENTS | ||
| 923 | |||
| 924 | Thanks to Adam Prime, C<< adam.prime at utoronto.ca >> | ||
| 925 | for patches and feedback on the design. | ||
| 926 | |||
| 927 | The Minnesota Supercomputing Institute C<< http://www.msi.umn.edu/ >> | ||
| 928 | sponsored the development of this software. | ||
| 929 | |||
| 930 | =head1 COPYRIGHT & LICENSE | ||
| 931 | |||
| 932 | Copyright 2007 by the Regents of the University of Minnesota. | ||
| 933 | |||
| 934 | This program is free software; you can redistribute it and/or modify it | ||
| 935 | under the same terms as Perl itself. | ||
| 936 | |||
| 937 | =cut | ||
| 938 | |||
| 939 | 1; # End of Rose::DBx::Garden | ||