[Jifty-commit] r908 - in Jifty-DBI/trunk: . lib/Jifty/DBI
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Apr 23 22:17:51 EDT 2006
Author: jesse
Date: Sun Apr 23 22:17:50 2006
New Revision: 908
Modified:
Jifty-DBI/trunk/ (props changed)
Jifty-DBI/trunk/Changes
Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
Jifty-DBI/trunk/t/testmodels.pl
Log:
r11817 at hualien: jesse | 2006-04-23 21:35:09 -0400
* Switched Jifty::DBI::Record to use load-time method instantiation,
rather than AUTOLOAD
Modified: Jifty-DBI/trunk/Changes
==============================================================================
--- Jifty-DBI/trunk/Changes (original)
+++ Jifty-DBI/trunk/Changes Sun Apr 23 22:17:50 2006
@@ -1,5 +1,8 @@
Revision history for Perl extension Jifty::DBI.
+* Switched Jifty::DBI::Record to autocreate methods on object load rather than
+ use AUTOLOAD.
+
0.20 Fri Apr 21 10:23:14 EDT 2006
* Documentation updates and misc bugfixes from Eric Wilhelm
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm Sun Apr 23 22:17:50 2006
@@ -20,6 +20,7 @@
sort_order
refers_to by
alias_for_column
+ aliased_as
since until
label hints render_as
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm Sun Apr 23 22:17:50 2006
@@ -3,7 +3,6 @@
use strict;
use warnings;
-use vars qw($AUTOLOAD);
use Class::ReturnValue ();
use Lingua::EN::Inflect ();
use Jifty::DBI::Column ();
@@ -88,109 +87,6 @@
return (%hash);
}
-sub DESTROY {
- return 1;
-}
-
-sub AUTOLOAD {
- my $self = $_[0];
-
- $self->_init_columns() unless $self->COLUMNS;
-
- my ( $column_name, $action ) = $self->_parse_autoload_method($AUTOLOAD);
-
- unless ( $action and $column_name ) {
- my ( $package, $filename, $line ) = caller;
- die "$AUTOLOAD Unimplemented in $package. ($filename line $line) \n";
- }
-
- my $column = $self->column($column_name);
-
- unless ($column) {
- my ( $package, $filename, $line ) = caller;
- die "$AUTOLOAD Unimplemented in $package. ($filename line $line) \n";
- }
-
- no strict 'refs'; # We're going to be defining subs
- if ( $action eq 'read' ) {
- return '' unless $column->readable;
-
- if ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) ) {
- *{$AUTOLOAD} = sub {
- $_[0]->_to_record( $column_name,
- $_[0]->__value($column_name) );
- };
- } elsif (
- UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Collection" ) )
- {
- *{$AUTOLOAD} = sub { $_[0]->_collection_value($column_name) };
- } else {
- *{$AUTOLOAD} = sub { return ( $_[0]->_value($column_name) ) };
- }
- goto &$AUTOLOAD;
- } elsif ( $action eq 'write' ) {
- return ( 0, 'Immutable column' ) unless $column->writable;
-
- if ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) ) {
- *{$AUTOLOAD} = sub {
- my $self = shift;
- my $val = shift;
-
- $val = $val->id
- if UNIVERSAL::isa( $val, 'Jifty::DBI::Record' );
- return (
- $self->_set( column => $column_name, value => $val ) );
- };
- } else {
- *{$AUTOLOAD} = sub {
- return (
- $_[0]->_set( column => $column_name, value => $_[1] ) );
- };
- }
- goto &$AUTOLOAD;
- } elsif ( $action eq 'validate' ) {
- *{$AUTOLOAD}
- = sub { return ( $_[0]->_validate( $column_name, $_[1] ) ) };
- goto &$AUTOLOAD;
- }
-
- else {
- my ( $package, $filename, $line ) = caller;
- die "$AUTOLOAD Unimplemented in $package. ($filename line $line) \n";
- }
-
-}
-
-=head2 _parse_autoload_method $AUTOLOAD
-
-Parses autoload methods and attempts to determine if they're
-set, get or validate calls.
-
-Returns a tuple of (COLUMN_NAME, ACTION);
-
-=cut
-
-sub _parse_autoload_method {
- my $self = shift;
- my $method = shift;
-
- my ( $column_name, $action );
-
- if ( $method =~ /^.*::set_(\w+)$/o ) {
- $column_name = $1;
- $action = 'write';
- } elsif ( $method =~ /^.*::validate_(\w+)$/o ) {
- $column_name = $1;
- $action = 'validate';
-
- } elsif ( $method =~ /^.*::(\w+)$/o ) {
- $column_name = $1;
- $action = 'read';
-
- }
- return ( $column_name, $action );
-
-}
=head2 _accessible COLUMN ATTRIBUTE
@@ -255,9 +151,67 @@
$column->readable(1);
$column->type('serial');
$column->mandatory(1);
+ $self->_init_methods_for_column($column);
+ }
+}
+
+sub _init_methods_for_column {
+ my $self = $_[0];
+ my $column = $_[1];
+ my $column_name = ($column->aliased_as ?$column->aliased_as:$column->name);
+ my $package = ref($self)||$self;
+ no strict 'refs'; # We're going to be defining subs
+ if ( not $self->can($column_name)) {
+
+ my $subref;
+ if ($column->readable) {
+ if ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) ) {
+ $subref = sub {
+ $_[0]->_to_record( $column_name, $_[0]->__value($column_name) );
+ };
+ }
+ elsif ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Collection" ) ) {
+ $subref = sub { $_[0]->_collection_value($column_name) };
+ }
+ else {
+ $subref = sub { return ( $_[0]->_value($column_name) ) };
+ }
+ } else {
+ $subref = sub { return '' }
+ }
+ *{$package."::".$column_name} = $subref;
+
+ }
+ if ( not $self->can("set_".$column_name)) {
+ my $subref;
+ if ($column->writable) {
+ if ( UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" ) ) {
+ $subref = sub {
+ my $self = shift;
+ my $val = shift;
+
+ $val = $val->id
+ if UNIVERSAL::isa( $val, 'Jifty::DBI::Record' );
+ return ( $self->_set( column => $column_name, value => $val ) );
+ };
+ }
+ else {
+ $subref = sub {
+ return ( $_[0]->_set( column => $column_name, value => $_[1] ) );
+ };
+ } }
+ else {
+ $subref = sub { return (0, 'Immutable column') } ;
+ }
+ *{$package."::" . "set_" . $column_name } = $subref;
+ }
+ if (not $self->can("validate_".$column_name)) {
+ *{ $package ."::" . "validate_" . $column_name }
+ = sub { return ( $_[0]->_validate( $column_name, $_[1] ) ) };
}
}
+
=head2 _to_record COLUMN VALUE
This B<PRIVATE> method takes a column name and a value for that column.
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm Sun Apr 23 22:17:50 2006
@@ -89,17 +89,19 @@
if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) {
if ( $name =~ /(.*)_id$/ ) {
- my $virtual_column = $from->add_column($1);
+ my $aliased_as = $1;
+ my $virtual_column = $from->add_column($aliased_as);
# XXX FIXME I think the next line is wrong, but things
# explode without it -- mostly because we unique-key
# on name instead of some conbination of name and
# alias_for_column in a couple places
$virtual_column->name( $name );
-
+ $virtual_column->aliased_as($aliased_as);
$_->apply($virtual_column) for @args;
$column->refers_to(undef);
$virtual_column->alias_for_column($name);
+ $from->_init_methods_for_column($virtual_column);
}
$column->by('id') unless $column->by;
$column->type('integer') unless $column->type;
@@ -115,6 +117,7 @@
$from->COLUMNS->{$name} = $column;
+ $from->_init_methods_for_column($column);
}
=head2 type
Modified: Jifty-DBI/trunk/t/testmodels.pl
==============================================================================
--- Jifty-DBI/trunk/t/testmodels.pl (original)
+++ Jifty-DBI/trunk/t/testmodels.pl Sun Apr 23 22:17:50 2006
@@ -25,3 +25,4 @@
column phone =>
type is 'varchar';
+1;
More information about the Jifty-commit
mailing list