X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSourceProxy%2FTable.pm;h=fe72d4d9b3014d0cc348a9a7962dec34abda68b2;hb=d71502bd0ca6c125b14e7a9d4786f7deae3e6fb2;hp=e8a2da008852f1d499f456ea11f898a658bb4921;hpb=80c90f5d6c31d3464041a6e927495ed205c7a32d;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSourceProxy/Table.pm b/lib/DBIx/Class/ResultSourceProxy/Table.pm index e8a2da0..fe72d4d 100644 --- a/lib/DBIx/Class/ResultSourceProxy/Table.pm +++ b/lib/DBIx/Class/ResultSourceProxy/Table.pm @@ -4,27 +4,66 @@ use strict; use warnings; use base qw/DBIx::Class::ResultSourceProxy/; + use DBIx::Class::ResultSource::Table; +use Scalar::Util 'blessed'; +use namespace::clean; + +__PACKAGE__->mk_classdata(table_class => 'DBIx::Class::ResultSource::Table'); + +__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do + # anything yet! + +sub _init_result_source_instance { + my $class = shift; + + $class->mk_classdata('result_source_instance') + unless $class->can('result_source_instance'); + + my $table = $class->result_source_instance; + my $class_has_table_instance = ($table and $table->result_class eq $class); + return $table if $class_has_table_instance; + + my $table_class = $class->table_class; + $class->ensure_class_loaded($table_class); + + if( $table ) { + $table = $table_class->new({ + %$table, + result_class => $class, + source_name => undef, + schema => undef + }); + } + else { + $table = $table_class->new({ + name => undef, + result_class => $class, + source_name => undef, + }); + } -__PACKAGE__->mk_classdata('table_alias'); # FIXME: Doesn't actually do anything yet! + $class->result_source_instance($table); -__PACKAGE__->mk_classdata('table_class' => 'DBIx::Class::ResultSource::Table'); + return $table; +} -=head1 NAME +=head1 NAME -DBIx::Class::ResultSourceProxy::Table - provides a classdata table object and method proxies +DBIx::Class::ResultSourceProxy::Table - provides a classdata table +object and method proxies =head1 SYNOPSIS - __PACKAGE__->table('foo'); - __PACKAGE__->add_columns(qw/id bar baz/); - __PACKAGE__->set_primary_key('id'); + __PACKAGE__->table('cd'); + __PACKAGE__->add_columns(qw/cdid artist title year/); + __PACKAGE__->set_primary_key('cdid'); =head1 METHODS =head2 add_columns - __PACKAGE__->add_columns(qw/col1 col2 col3/); + __PACKAGE__->add_columns(qw/cdid artist title year/); Adds columns to the current class and creates accessors for them. @@ -33,7 +72,7 @@ Adds columns to the current class and creates accessors for them. =head2 table __PACKAGE__->table('tbl_name'); - + Gets or sets the table name. =cut @@ -41,52 +80,67 @@ Gets or sets the table name. sub table { my ($class, $table) = @_; return $class->result_source_instance->name unless $table; - unless (ref $table) { - $table = $class->table_class->new( - { + + unless (blessed $table && $table->isa($class->table_class)) { + + my $table_class = $class->table_class; + $class->ensure_class_loaded($table_class); + + $table = $table_class->new({ + $class->can('result_source_instance') + ? %{$class->result_source_instance||{}} + : () + , name => $table, result_class => $class, - }); - if ($class->can('result_source_instance')) { - $table->{_columns} = { %{$class->result_source_instance->{_columns}||{}} }; - $table->{_ordered_columns} = - [ @{$class->result_source_instance->{_ordered_columns}||[]} ]; - } - } - $class->mk_classdata('result_source_instance' => $table); - if ($class->can('schema_instance')) { - $class =~ m/([^:]+)$/; - $class->schema_instance->register_class($class, $class); + }); } + + $class->mk_classdata('result_source_instance') + unless $class->can('result_source_instance'); + + $class->result_source_instance($table); + + return $class->result_source_instance->name; } -=head2 has_column - - if ($obj->has_column($col)) { ... } - -Returns 1 if the class has a column of this name, 0 otherwise. - -=cut - -=head2 column_info - - my $info = $obj->column_info($col); - -Returns the column metadata hashref for a column. - -=cut +=head2 table_class + + __PACKAGE__->table_class('DBIx::Class::ResultSource::Table'); + +Gets or sets the table class used for construction and validation. + +=cut + +=head2 has_column + + if ($obj->has_column($col)) { ... } + +Returns 1 if the class has a column of this name, 0 otherwise. + +=cut + +=head2 column_info + + my $info = $obj->column_info($col); + +Returns the column metadata hashref for a column. For a description of +the various types of column data in this hashref, see +L + +=cut =head2 columns - my @column_names = $obj->columns; - -=cut + my @column_names = $obj->columns; + +=cut 1; -=head1 AUTHORS +=head1 AUTHOR AND CONTRIBUTORS -Matt S. Trout +See L and L in DBIx::Class =head1 LICENSE