From: Brandon L. Black Date: Sun, 23 Jul 2006 19:47:36 +0000 (+0000) Subject: docs + draft untested code for load_namespaces X-Git-Tag: v0.08010~43^2~22^2~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2374c5ff7ea2309a69344fd34be799399f110b66;p=dbsrgits%2FDBIx-Class.git docs + draft untested code for load_namespaces --- diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 47e78cf..9f973a9 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -278,6 +278,89 @@ sub load_classes { } } +=head2 load_namespaces + +=over 4 + +=item Arguments: None + +=back + +This is an alternative to L above which assumes an alternative +layout for automatic class loading. This variant takes no arguments. It +assumes that all ResultSource classes to be loaded are underneath a sub- +namespace of the schema called "ResultSource", and any corresponding +ResultSet classes to be underneath a sub-namespace of the schema called +"ResultSet". + +Any missing ResultSet definitions will be created in memory as basic +inheritors of L. You don't need to explicitly +specify the result_class of the sources unless you wish to override +the naming convention above. + +This method requires L to be installed on the system. + +Example: + + My::Schema->load_namespaces; + # loads My::Schema::ResultSource::CD, My::Schema::ResultSource::Artist, + # My::Schema::ResultSet::CD, etc... + +=cut + +sub load_namespaces { + my ($class) = @_; + + my $source_namespace = $class . '::ResultSource'; + my $resultset_namespace = $class . '::ResultSet'; + + eval "require Module::Find"; + $class->throw_exception("Couldn't load Module::Find ($@)") if $@; + + my %sources = map { (substr($_, length "${source_namespace}::"), $_) } + Module::Find::findallmod($source_namespace); + + my %resultsets = map { (substr($_, length "${resultset_namespace}::"), $_) } + Module::Find::findallmod($resultset_namespace); + + my @to_register; + { + no warnings qw/redefine/; + local *Class::C3::reinitialize = sub { }; + foreach my $source (keys %sources) { + my $source_class = $sources{$source}; + $class->ensure_class_loaded($source_class); + $source_class->source_name($source) unless $source_class->source_name; + unless($source_class->resultset_class) { + if(my $rs_class = delete $resultsets{$source}) { + $class->ensure_class_loaded($rs_class); + $source_class->resultset_class($rs_class); + } + else { + require DBIx::Class::ResultSet; + my $rs_class = "${class}::ResultSet::$source"; + @{"$rs_class::ISA"} = 'DBIx::Class::ResultSet'; + $source_class->resultset_class($rs_class); + } + } + + push(@to_register, [ $source_class->source_name, $source_class ]); + } + } + + foreach (keys %resultsets) { + warn "load_namespaces found ResultSet $_ with no " + . 'corresponding ResultSource'; + } + + Class::C3->reinitialize; + + foreach my $to (@to_register) { + $class->register_class(@$to); + # if $class->can('result_source_instance'); + } +} + =head2 compose_connection =over 4