[Jifty-commit] r712 - in Jifty-DBI/trunk: lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Mar 20 21:15:37 EST 2006


Author: alexmv
Date: Mon Mar 20 21:15:37 2006
New Revision: 712

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm

Log:
 r11728 at zoq-fot-pik:  chmrr | 2006-03-20 21:12:28 -0500
  * Use objects instead of prototypes to know how much of @_ to eat
  * This also allows things like 'is ! immutable' if you really want it


Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	Mon Mar 20 21:15:37 2006
@@ -1471,11 +1471,9 @@
 
 =cut
 
-sub refers_to (@) {
+sub refers_to {
     my $class = shift;
-    my (%args) = @_;
-
-    return ( refers_to => $class, %args );
+    return ( Jifty::DBI::Schema::Trait->new(refers_to => $class), @_ );
 }
 
 =head2 Clone

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	Mon Mar 20 21:15:37 2006
@@ -1028,11 +1028,9 @@
 
 =cut
 
-sub refers_to (@) {
+sub refers_to {
     my $class = shift;
-    my (%args) = @_;
-
-    return ( refers_to => $class, %args );
+    return ( Jifty::DBI::Schema::Trait->new(refers_to => $class), @_ );
 }
 
 sub _filters {

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	Mon Mar 20 21:15:37 2006
@@ -40,7 +40,7 @@
 use Carp qw/carp/;
 use Exporter::Lite;
 our @EXPORT
-    = qw(column type default validator immutable unreadable length distinct mandatory not_null valid_values label hints render_as since input_filters output_filters filters is by are on virtual);
+    = qw(column type default validator immutable unreadable length distinct mandatory not_null valid_values label hints render_as since input_filters output_filters filters virtual is by are on);
 
 our $SCHEMA;
 
@@ -59,23 +59,25 @@
 
     my $from = (caller)[0];
     $from =~ s/::Schema//;
+
+    croak "Base of schema class $from is not a Jifty::DBI::Record"
+      unless UNIVERSAL::isa($from, "Jifty::DBI::Record");
+
+    carp "Illegal column definition for column $name in $from"
+      if grep {not UNIVERSAL::isa($_, "Jifty::DBI::Schema::Trait")} @_;
+
     $from->_init_columns;
 
     my @args = (
-        name     => $name,
-        readable => 1,
-        writable => 1,
-        virtual  => 0,
-        type     => '',
-        @_,
+        ! unreadable(),
+        ! immutable(),
+        ! virtual(),
+        type(''),
+        @_
     );
-    my @original = @args;
 
-    my $column = Jifty::DBI::Column->new();
-    while (@args) {
-        my ( $method, $arguments ) = splice @args, 0, 2;
-        $column->$method($arguments);
-    }
+    my $column = Jifty::DBI::Column->new( { name => $name } );
+    $_->apply($column) for @args;
 
     if ( my $refclass = $column->refers_to ) {
         $refclass->require();
@@ -84,10 +86,14 @@
         if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) {
             if ( $name =~ /(.*)_id$/ ) {
                 my $virtual_column = $from->add_column($1);
-                while (@original) {
-                    my ( $method, $arguments ) = splice @original, 0, 2;
-                    $virtual_column->$method($arguments);
-                }
+
+                # 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 );
+
+                $_->apply($virtual_column) for @args;
                 $column->refers_to(undef);
                 $virtual_column->alias_for_column($name);
             }
@@ -114,9 +120,8 @@
 
 =cut
 
-sub type ($) {
-    _list( type => shift );
-
+sub type {
+    _list( type => @_ );
 }
 
 =head2 default
@@ -126,8 +131,8 @@
 
 =cut
 
-sub default ($) {
-    _list( default => shift );
+sub default {
+    _list( default => @_ );
 }
 
 =head2 validator
@@ -137,8 +142,8 @@
 
 =cut
 
-sub validator ($) {
-    _list( validator => shift );
+sub validator {
+    _list( validator => @_ );
 }
 
 =head2 immutable
@@ -149,8 +154,8 @@
 
 =cut
 
-sub immutable () {
-    _item( [ writable => 0 ] );
+sub immutable {
+    _item( writable => 0, @_ );
 }
 
 =head2 unreadable
@@ -163,7 +168,7 @@
 =cut
 
 sub unreadable {
-    _item( [ readable => 0 ] );
+    _item( readable => 0, @_ );
 }
 
 =head2 length
@@ -175,8 +180,8 @@
 
 =cut
 
-sub length ($) {
-    _list( length => shift );
+sub length {
+    _list( length => @_ );
 }
 
 =head2 mandatory
@@ -186,8 +191,8 @@
 
 =cut
 
-sub mandatory () {
-    _item( [ mandatory => 1 ] );
+sub mandatory {
+    _item( mandatory => 1, @_ );
 }
 
 =head2 not_null
@@ -197,9 +202,9 @@
 
 =cut
 
-sub not_null () {
+sub not_null {
     carp "'is not_null' is deprecated in favor of 'is mandatory'";
-    _item( [ mandatory => 1 ] );
+    _item( mandatory => 1, @_ );
 }
 
 =head2 distinct
@@ -210,20 +215,19 @@
 
 =cut
 
-sub distinct () {
-    _item( [ distinct => 1 ] );
+sub distinct {
+    _item( distinct => 1, @_ );
 }
 
-=head2 filters
+=head2 virtual
 
-Sets a list of filters on the data.  These are applied when reading
-B<and> writing to the database.  Correct usage is C<input_filters are
-'Jifty::DBI::Filter::DateTime'>.  See L<Jifty::DBI::Filter>.
+Declares that a column is not backed by an actual column in the
+database, but is instead computed on-the-fly.
 
 =cut
 
-sub filters ($) {
-    _list( input_filters => shift );
+sub virtual {
+    _item( virtual => 1, @_ );
 }
 
 =head2 input_filters
@@ -234,8 +238,8 @@
 
 =cut
 
-sub input_filters ($) {
-    _list( input_filters => shift );
+sub input_filters {
+    _list( input_filters => @_ );
 }
 
 =head2 output_filters
@@ -246,8 +250,20 @@
 
 =cut
 
-sub output_filters ($) {
-    _list( output_filters => shift );
+sub output_filters {
+    _list( output_filters => @_ );
+}
+
+=head2 filters
+
+Sets a list of filters on the data.  These are applied when reading
+B<and> writing to the database.  Correct usage is C<input_filters are
+'Jifty::DBI::Filter::DateTime'>.  See L<Jifty::DBI::Filter>.
+
+=cut
+
+sub filters {
+    _list( input_filters => @_ );
 }
 
 =head2 since
@@ -257,8 +273,8 @@
 
 =cut
 
-sub since ($) {
-    _list( since => shift );
+sub since {
+    _list( since => @_ );
 }
 
 =head2 valid_values
@@ -270,8 +286,8 @@
 
 =cut
 
-sub valid_values ($) {
-    _list( valid_values => shift );
+sub valid_values {
+    _list( valid_values => @_ );
 }
 
 =head2 label
@@ -281,8 +297,8 @@
 
 =cut
 
-sub label ($) {
-    _list( label => shift );
+sub label {
+    _list( label => @_ );
 }
 
 =head2 hints
@@ -293,8 +309,8 @@
 
 =cut
 
-sub hints ($) {
-    _list( hints => shift );
+sub hints {
+    _list( hints => @_ );
 }
 
 =head2 render_as
@@ -302,7 +318,7 @@
 Used in user interface generation to know how to render the column.
 
 The values for this attribute are the same as the names of the modules under
-Jifty::Web::Form::Field, i.e. 
+L<Jifty::Web::Form::Field>, i.e. 
 
 =over 
 
@@ -338,33 +354,33 @@
 internal fields that should not actually be displayed.
 
 If these don't meet your needs, you can write your own subclass of
-Jifty::Web::Form::Field. See the documentation for that module.
+L<Jifty::Web::Form::Field>. See the documentation for that module.
 
 =cut
 
-sub render_as ($) {
-    _list( render_as => shift );
+sub render_as {
+    _list( render_as => @_ );
 }
 
-=head2 is
+=head2 by
 
 Helper method to improve readability.
 
 =cut
 
-sub is ($) {
-    my $thing = shift;
-    ref $thing eq "ARRAY" ? _list( @{$thing} ) : _item($thing);
+sub by {
+    _list( by => @_ );
 }
 
-=head2 by
+=head2 is
 
 Helper method to improve readability.
 
 =cut
 
-sub by ($) {
-    _list( by => shift );
+sub is {
+    my $thing = shift;
+    ref $thing eq "ARRAY" ? ( @{$thing}, @_ ) : ($thing, @_);
 }
 
 =head2 are
@@ -373,8 +389,10 @@
 
 =cut
 
-sub are (@) {
-    _item( [@_] );
+sub are {
+    my $ref = [];
+    push @{$ref}, shift @_ while @_ and not UNIVERSAL::isa($_[0], "Jifty::DBI::Schema::Trait");
+    return( $ref, @_ );
 }
 
 =head2 on
@@ -383,7 +401,7 @@
 
 =cut
 
-sub on ($) {
+sub on {
     _list( self => shift );
 }
 
@@ -394,14 +412,20 @@
     wantarray
         or die
         "Cannot call list traits in scalar context -- check for unneccessary 'is'";
-    @_;
+    _trait(@_);
 }
 
 sub _item {
     defined wantarray
         or die
         "Cannot add traits in void context -- check for misspelled preceding comma as a semicolon";
-    $_[0];
+    _trait(@_);
+}
+
+sub _trait {
+    my @trait;
+    push @trait, shift @_ while @_ and not UNIVERSAL::isa($_[0], "Jifty::DBI::Schema::Trait");
+    return wantarray ? (Jifty::DBI::Schema::Trait->new(@trait), @_) : Jifty::DBI::Schema::Trait->new(@trait);
 }
 
 =head1 EXAMPLE
@@ -419,4 +443,31 @@
 
 =cut
 
+package Jifty::DBI::Schema::Trait;
+
+use overload "!" => \&negation;
+
+sub new {
+    my $class = shift;
+    return bless [@_], $class;
+}
+
+sub apply {
+    my $self = shift;
+    my ($column) = @_;
+
+    my ($method, $argument) = @{$self};
+
+    die "Illegal Jifty::DBI::Schema property '$method'"
+      unless $column->can($method);
+
+    $column->$method($argument);
+}
+
+sub negation {
+    my $self = shift;
+    my ($trait, @rest) = @{$self};
+    return (ref $self)->new($trait, map {not $_} @rest);
+}
+
 1;


More information about the Jifty-commit mailing list