]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI/DFV.pm
C3, inheritence changes and adding skeleton DFV model
[maypole.git] / lib / Maypole / Model / CDBI / DFV.pm
diff --git a/lib/Maypole/Model/CDBI/DFV.pm b/lib/Maypole/Model/CDBI/DFV.pm
new file mode 100644 (file)
index 0000000..1a90bce
--- /dev/null
@@ -0,0 +1,116 @@
+package Maypole::Model::CDBI::DFV;
+use Class::C3;
+use Maypole::Config;
+use base qw(Maypole::Model::Base);
+use strict;
+
+Maypole::Config->mk_accessors(qw(table_to_class));
+
+=head1 NAME
+
+Maypole::Model::CDBI::Plain - Class::DBI model without ::Loader
+
+=head1 SYNOPSIS
+
+    package Foo;
+    use 'Maypole::Application';
+
+    Foo->config->model("Maypole::Model::CDBI::DFV");
+    Foo->setup([qw/ Foo::SomeTable Foo::Other::Table /]);
+
+    # Look ma, no untainting
+
+    sub Foo::SomeTable::SomeAction : Exported {
+
+        . . .
+
+    }
+
+=head1 DESCRIPTION
+
+This module allows you to use Maypole with previously set-up
+L<Class::DBI> classes that use Class::DBI::DFV;
+
+Simply call C<setup> with a list reference of the classes you're going to use,
+and Maypole will work out the tables and set up the inheritance relationships
+as normal.
+
+Better still, it will also set use your DFV profile to validate input instead
+of CGI::Untaint. For teh win!!
+
+=head1 METHODS
+
+=head2 setup
+
+  This method is inherited from Maypole::Model::Base and calls setup_database,
+  which uses Class::DBI::Loader to create and load Class::DBI classes from
+  the given database schema.
+
+=head2 setup_database
+
+  This method loads the model classes for the application
+
+=cut
+
+sub setup_database {
+    my ( $self, $config, $namespace, $classes ) = @_;
+    $config->{classes}        = $classes;
+    foreach my $class (@$classes) { $namespace->load_model_subclass($class); }
+    $namespace->model_classes_loaded(1);
+    $config->{table_to_class} = { map { $_->table => $_ } @$classes };
+    $config->{tables}         = [ keys %{ $config->{table_to_class} } ];
+}
+
+=head2 class_of
+
+  returns class for given table
+
+=cut
+
+sub class_of {
+    my ( $self, $r, $table ) = @_;
+    return $r->config->{table_to_class}->{$table};
+}
+
+=head2 add_model_superclass
+
+Adds model as superclass to model classes
+
+=cut
+
+sub add_model_superclass {
+  my ($class,$config) = @_;
+  foreach my $subclass ( @{ $config->classes } ) {
+    next if $subclass->isa("Maypole::Model::Base");
+    no strict 'refs';
+    push @{ $subclass . "::ISA" }, $config->model;
+  }
+  return;
+}
+
+=head2 adopt
+
+This class method is passed the name of a model class that represensts a table
+and allows the master model class to do any set-up required.
+
+=cut
+
+sub adopt {
+    my ( $self, $child ) = @_;
+    if ( my $col = $child->stringify_column ) {
+        $child->columns( Stringify => $col );
+    }
+}
+
+=head1 SEE ALSO
+
+L<Maypole::Model::Base>
+
+L<Maypole::Model::CDBI>
+
+=cut
+
+
+1;
+
+