[Jifty-commit] r4145 - in Jifty-DBI/trunk: . lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Sep 24 17:57:46 EDT 2007


Author: clkao
Date: Mon Sep 24 17:57:41 2007
New Revision: 4145

Added:
   Jifty-DBI/trunk/t/metadata.t
Modified:
   Jifty-DBI/trunk/MANIFEST
   Jifty-DBI/trunk/lib/Jifty/DBI/Column.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
   Jifty-DBI/trunk/t/17virtualtypes.t

Log:
Refactor Jifty::DBI::Column to split attributes into two groups.
Actual column-related (in jifty::dbi layer) ones, such as type,
remains as accesssors.  The rest goes into the attributes hash.
This allows us to declare higher level meta data without having to
change Jifty::DBI::Column in the future.

Note that if you are accessing the column object's ->{label} directly
this will now fail.  Use ->label instead.


Modified: Jifty-DBI/trunk/MANIFEST
==============================================================================
--- Jifty-DBI/trunk/MANIFEST	(original)
+++ Jifty-DBI/trunk/MANIFEST	Mon Sep 24 17:57:41 2007
@@ -81,6 +81,7 @@
 t/15types.t
 t/16inheritance.t
 t/17virtualtypes.t
+t/metadata.t
 t/pod-coverage.t
 t/pod.t
 t/testmodels.pl

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	Mon Sep 24 17:57:41 2007
@@ -8,35 +8,50 @@
 use UNIVERSAL::require;
 use version;
 
-
-
-our @ATTRS = qw/
-name
+my @attrs = qw/
+    name
     type
     default
     readable writable
     max_length
     mandatory
     virtual
-    container
     distinct
     sort_order
     refers_to by
     alias_for_column
     aliased_as
     since till
+    indexed
+    _validator
+    _checked_for_validate_sub
+    record_class
+    attributes
+    /;
 
+# these actually live in the attributes hash
+my @handy_attrs = qw/
+    container
     label hints render_as
     valid_values
     available_values
-    indexed
     autocompleted
-    _validator
-    _checked_for_validate_sub
-    record_class
     /;
 
-__PACKAGE__->mk_accessors(@ATTRS);
+# compat: this should probably never exist and be deprecated
+our @ATTRS = (@attrs, @handy_attrs);
+
+__PACKAGE__->mk_accessors(@attrs);
+
+for my $attr (@handy_attrs) {
+    no strict 'refs';
+    *$attr = sub {
+        my $self = shift;
+	$self->attributes({}) unless $self->attributes;
+        return $self->attributes->{$attr} unless @_;
+        $self->attributes->{$attr} = (@_ == 1 ? $_[0] : [@_]);
+    }
+}
 
 =head1 NAME
 
@@ -54,6 +69,26 @@
 
 =cut
 
+=head2 new
+
+=cut
+
+sub new {
+    my ($class, $args) = @_;
+    my $self = $class->SUPER::new({});
+
+    # run through accessors, push unknown keys into the attributes hash
+
+    # XXX: we might want to construct the proper hash (lifting things
+    # not in @attrs into attributes and just pass the whole hash
+    $self->attributes({});
+    for (keys %$args) {
+	$self->can($_) ? $self->$_($args->{$_}) : $self->attributes->{$_} = $args->{$_};
+    }
+
+    return $self;
+}
+
 =head2 is_numeric
 
 Returns true if the column is of some numeric type, otherwise returns false.
@@ -89,17 +124,46 @@
 =head2 serialize_metadata
 
 Returns a hash describing this column object with enough detail to
-fully describe it in the database.  Intentionally skips C<record_class>,
-all column attributes starting with C<_>, and all column attributes
-which are undefined.
+fully describe it in the database.  Intentionally skips
+C<record_class>, all column attributes starting with C<_>, and all
+column attributes which are undefined.  The "known" attributes in the
+C<attributes> hash are flattened and returned as well.  The list of
+known attributes are:
+
+=over
+
+=item     container
+
+=item     label hints render_as
+
+=item     valid_values
+
+=item     available_values
+
+=item     autocompleted
+
+=back
 
 =cut
 
 sub serialize_metadata {
     my $self = shift;
-    return {map { $_ => $self->$_() } grep { $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_}  @ATTRS};
+    return {map { $_ => $self->$_() } grep { $_ ne 'attributes' && $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_}
+            @ATTRS};
+}
 
+=head2 serialize_metadata2
 
+Returns a hash describing this column object with enough detail to
+fully describe it in the database.  Intentionally skips C<record_class>,
+all column attributes starting with C<_>, and all column attributes
+which are undefined.
+
+=cut
+
+sub serialize_metadata2 {
+    my $self = shift;
+    return {map { $_ => $self->$_() } grep { $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_} @attrs};
 }
 
 =head2 validator

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 Sep 24 17:57:41 2007
@@ -373,7 +373,7 @@
         } else {
             warn "Error in $from: $refclass neither Record nor Collection";
         }
-    } elsif (my $handler = $column->{_init_handler}) {
+    } elsif (my $handler = $column->attributes->{_init_handler}) {
         $handler->($column, $from);
     } else {
         $column->type('varchar(255)') unless $column->type;

Modified: Jifty-DBI/trunk/t/17virtualtypes.t
==============================================================================
--- Jifty-DBI/trunk/t/17virtualtypes.t	(original)
+++ Jifty-DBI/trunk/t/17virtualtypes.t	Mon Sep 24 17:57:41 2007
@@ -87,7 +87,6 @@
     my ($column, $from) = @_;
     my $name = $column->name;
     $column->virtual(1);
-    use Data::Dumper;
     for (qw(x y)) {
         Jifty::DBI::Schema::_init_column_for(
             Jifty::DBI::Column->new({ type => 'double',

Added: Jifty-DBI/trunk/t/metadata.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/t/metadata.t	Mon Sep 24 17:57:41 2007
@@ -0,0 +1,94 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 6;
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+use DateTime ();
+
+foreach my $d ( @available_drivers ) {
+SKIP: {
+        unless( has_schema( 'TestApp::User', $d ) ) {
+                skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+        }
+        unless( should_test( $d ) ) {
+                skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+        }
+        diag("start testing with '$d' handle") if $ENV{TEST_VERBOSE};
+
+        my $handle = get_handle( $d );
+        connect_handle( $handle );
+        isa_ok($handle->dbh, 'DBI::db');
+
+        {my $ret = init_schema( 'TestApp::User', $handle );
+        isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back" );}
+
+        my $rec = TestApp::User->new( handle => $handle );
+        isa_ok($rec, 'Jifty::DBI::Record');
+
+	my $col = $rec->column('name');
+	is($col->label, 'Name');
+
+	is($col->attributes->{arbitary_data}, 'fooo');
+
+	is_deeply($col->serialize_metadata, { type => 'varchar', label => 'Name', sort_order => 0, writable => 1, name => 'name', readable => 1 });
+
+        cleanup_schema( 'TestApp', $handle );
+        disconnect_handle( $handle );
+}
+}
+
+package TestApp::User;
+use base qw/Jifty::DBI::Record/;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+        id integer primary key,
+        name varchar
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id integer auto_increment primary key,
+        name varchar
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id serial primary key,
+        name varchar
+)
+EOF
+
+}
+
+use Jifty::DBI::Schema;
+
+use Jifty::DBI::Record schema {
+    column name     => type is 'varchar', label is 'Name', arbitary_data is 'fooo';
+};
+
+
+1;
+


More information about the Jifty-commit mailing list