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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed May 23 22:47:35 EDT 2007


Author: jesse
Date: Wed May 23 22:47:33 2007
New Revision: 3289

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

Log:
 r57064 at pinglin:  jesse | 2007-05-23 22:47:06 -0400
 * Added a way to serialize a model's schema into a perl data structure.
   
 


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	Wed May 23 22:47:33 2007
@@ -8,8 +8,10 @@
 use UNIVERSAL::require;
 use version;
 
-__PACKAGE__->mk_accessors qw/
-    name
+
+
+our @ATTRS = qw/
+name
     type
     default
     readable writable
@@ -33,6 +35,8 @@
     record_class
     /;
 
+__PACKAGE__->mk_accessors(@ATTRS);
+
 =head1 NAME
 
 Jifty::DBI::Column
@@ -81,8 +85,22 @@
 }
 
 
+=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.
+
+=cut
+
+sub serialize_metadata {
+    my $self = shift;
+    return {map { $_ => $self->$_() } grep { $_ ne 'record_class' && $_ !~ /^_/ && defined $self->$_}  @ATTRS};
 
 
+}
+
 =head2 validator
 
 Gets/sets the validator coderef for the column.

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	Wed May 23 22:47:33 2007
@@ -514,6 +514,39 @@
     return @{$self->_READABLE_COLS_CACHE() || $self->_READABLE_COLS_CACHE([sort map { $_->name } grep { $_->readable } $self->columns])};
 }
 
+=head2 serialize_metadata
+
+Returns a hash which describes how this class is stored in the database. 
+Right now, the keys are C<class>, C<table>, and C<columns>. C<class> and C<table>
+return simple scalars, but C<columns> returns a hash of C<name =&gt; value> pairs
+for all the columns in this model. See C<Jifty::DBI::Column/serialize_metadata> for 
+the format of that hash.
+
+
+=cut
+
+sub serialize_metadata {
+    my $self = shift;
+    return {
+            class => (ref($self) || $self),
+            table => $self->table,
+            columns => { $self->_serialize_columns },
+    }
+}
+
+sub _serialize_columns {
+    my $self = shift;
+    my %serialized_columns;
+    foreach my $column ( $self->columns  ) {
+        $serialized_columns{ $column->name } = $column->serialize_metadata();
+    }
+
+    return %serialized_columns;
+}
+
+
+
+
 =head2 writable_attributes
 
 Returns a list of this table's writable columns
@@ -669,7 +702,7 @@
 
 =head2 as_hash 
 
-Returns a version of this object's readable columns rendered as a hash of key => value pairs
+Returns a version of this record's readable columns rendered as a hash of key => value pairs
 
 =cut
 


More information about the Jifty-commit mailing list