[Jifty-commit] r4741 - in jifty/trunk: lib/Jifty
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Tue Dec 18 21:22:14 EST 2007
Author: sartak
Date: Tue Dec 18 21:22:13 2007
New Revision: 4741
Modified:
jifty/trunk/ (props changed)
jifty/trunk/lib/Jifty/Collection.pm
jifty/trunk/lib/Jifty/DateTime.pm
jifty/trunk/lib/Jifty/Record.pm
jifty/trunk/lib/Jifty/Result.pm
Log:
r49203 at onn: sartak | 2007-12-18 21:22:02 -0500
Give the interesting classes jifty_serialize_format instead of hardcoding in recurse_object_to_data (obra++)
Modified: jifty/trunk/lib/Jifty/Collection.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Collection.pm (original)
+++ jifty/trunk/lib/Jifty/Collection.pm Tue Dec 18 21:22:13 2007
@@ -144,6 +144,20 @@
return ( current_user => $self->current_user );
}
+=head2 jifty_serialize_format
+
+This returns an array reference of the individual records that make up this
+collection.
+
+=cut
+
+sub jifty_serialize_format {
+ my $records = shift->items_array_ref;
+ my $result = shift;
+
+ return [ map { $result->_record_to_data($_) } @$records ];
+}
+
=head1 SEE ALSO
L<Jifty::DBI::Collection>, L<Jifty::Object>, L<Jifty::Record>
Modified: jifty/trunk/lib/Jifty/DateTime.pm
==============================================================================
--- jifty/trunk/lib/Jifty/DateTime.pm (original)
+++ jifty/trunk/lib/Jifty/DateTime.pm Tue Dec 18 21:22:13 2007
@@ -268,6 +268,24 @@
return 1;
}
+=head2 jifty_serialize_format
+
+This returns a DateTime (or string) consistent with Jifty's date format.
+
+=cut
+
+sub jifty_serialize_format {
+ my $dt = shift;
+
+ # if it looks like just a date, then return just the date portion
+ return $dt->ymd
+ if lc($dt->time_zone->name) eq 'floating'
+ && $dt->hms('') eq '000000';
+
+ # otherwise let stringification take care of it
+ return $dt;
+}
+
=head1 WHY?
There are other ways to do some of these things and some of the decisions here may seem arbitrary, particularly if you read the code. They are.
Modified: jifty/trunk/lib/Jifty/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Record.pm (original)
+++ jifty/trunk/lib/Jifty/Record.pm Tue Dec 18 21:22:13 2007
@@ -827,4 +827,28 @@
}
}
+=head2 jifty_serialize_format
+
+This is used to create a hash reference of the object's values. Unlike
+Jifty::DBI::Record->as_hash, this won't transform refers_to columns into JDBI
+objects
+
+=cut
+
+sub jifty_serialize_format {
+ my $record = shift;
+ my %data;
+
+ # XXX: maybe just test ->virtual?
+ for ($record->readable_attributes) {
+ next if UNIVERSAL::isa($record->column($_)->refers_to,
+ 'Jifty::DBI::Collection');
+ next if $record->column($_)->container;
+
+ $data{$_} = Jifty::Util->stringify($record->_value($_));
+ }
+
+ return \%data;
+}
+
1;
Modified: jifty/trunk/lib/Jifty/Result.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Result.pm (original)
+++ jifty/trunk/lib/Jifty/Result.pm Tue Dec 18 21:22:13 2007
@@ -233,16 +233,8 @@
my $self = shift;
my $o = shift;
- my %types = (
- 'Jifty::DBI::Collection' => \&_collection_to_data,
- 'Jifty::DBI::Record' => \&_record_to_data,
- 'Jifty::DateTime' => \&_datetime_to_data,
- );
-
- for my $type (keys %types) {
- if (UNIVERSAL::isa($o, $type)) {
- return $types{$type}->($self, $o);
- }
+ if ($o->can('jifty_serialize_format')) {
+ return $o->jifty_serialize_format($self);
}
# As the last resort, return the object itself and expect the
@@ -250,45 +242,4 @@
return $o;
}
-# return an arrayref of the records
-sub _collection_to_data {
- my $self = shift;
- my $records = shift->items_array_ref;
-
- return [ map { $self->_record_to_data($_) } @$records ];
-}
-
-# We could use ->as_hash but this method avoids transforming refers_to
-# columns into JDBI objects
-sub _record_to_data {
- my $self = shift;
- my $record = shift;
- my %data;
-
- # XXX: maybe just test ->virtual?
- for ($record->readable_attributes) {
- next if UNIVERSAL::isa($record->column($_)->refers_to,
- 'Jifty::DBI::Collection');
- next if $record->column($_)->container;
-
- $data{$_} = Jifty::Util->stringify($record->_value($_));
- }
-
- return \%data;
-}
-
-# returns a datetime string consistent with the Jifty "date" magic
-sub _datetime_to_data {
- my $self = shift;
- my $dt = shift;
-
- # if it looks like just a date, then return just the date portion
- return $dt->ymd
- if lc($dt->time_zone->name) eq 'floating'
- && $dt->hms('') eq '000000';
-
- # otherwise let stringification take care of it
- return $dt;
-}
-
1;
More information about the Jifty-commit
mailing list