From: Brandon L. Black Date: Sun, 23 Jul 2006 21:25:42 +0000 (+0000) Subject: overridable namespaces and overridable base resultset class X-Git-Tag: v0.08010~43^2~22^2~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=85bd0538ea366ab9048a0b53a87c539aaa27a382;p=dbsrgits%2FDBIx-Class.git overridable namespaces and overridable base resultset class --- diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 9f973a9..105064b 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -282,21 +282,25 @@ sub load_classes { =over 4 -=item Arguments: None +=item Arguments: %options? =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". +layout for automatic class loading. 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. +You can change the namespaces checked for ResultSources and ResultSets via +the C and C options, respectively. + +Any source which does not have an explicitly-defined corresponding ResultSet +will have one created in the appropriate namespace for it, based on +L. If you wish to change this default ResultSet +base class, you can do so via the C option. (Your +custom default should, of course, be based on L +itself). This method requires L to be installed on the system. @@ -306,19 +310,33 @@ Example: # loads My::Schema::ResultSource::CD, My::Schema::ResultSource::Artist, # My::Schema::ResultSet::CD, etc... + My::Schema->load_namespaces( + resultsource_namespace => 'My::Schema::RSources', + resultset_namespace => 'My::Schema::RSets', + default_resultset_base => 'My::Schema::RSetBase', + ); + # loads My::Schema::RSources::CD, My::Schema::RSources::Artist, + # My::Schema::RSets::CD, and if no such class exists on disk, + # creates My::Schema::RSets::Artist in memory based on the + # class My::Schema::RSetBase + =cut sub load_namespaces { - my ($class) = @_; + my ($class, %args) = @_; - my $source_namespace = $class . '::ResultSource'; - my $resultset_namespace = $class . '::ResultSet'; + my $resultsource_namespace = $args{resultsource_namespace} + || ($class . '::ResultSource'); + my $resultset_namespace = $args{resultset_namespace} + || ($class . '::ResultSet'); + my $default_resultset_base = $args{default_resultset_base} + || 'DBIx::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 %sources = map { (substr($_, length "${resultsource_namespace}::"), $_) } + Module::Find::findallmod($resultsource_namespace); my %resultsets = map { (substr($_, length "${resultset_namespace}::"), $_) } Module::Find::findallmod($resultset_namespace); @@ -326,20 +344,21 @@ sub load_namespaces { my @to_register; { no warnings qw/redefine/; + no strict qw/refs/; 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(!$source_class->resultset_class + || $source_class->resultset_class eq 'DBIx::Class::ResultSet') { 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'; + my $rs_class = "$resultset_namespace\::$source"; + @{"$rs_class\::ISA"} = ($default_resultset_base); $source_class->resultset_class($rs_class); } }