[Jifty-commit] r2806 - in Jifty-DBI/trunk: lib/Jifty/DBI/Filter t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Feb 20 09:46:49 EST 2007


Author: sterling
Date: Tue Feb 20 09:46:48 2007
New Revision: 2806

Added:
   Jifty-DBI/trunk/lib/Jifty/DBI/Filter/YAML.pm
   Jifty-DBI/trunk/t/06filter_yaml.t   (contents, props changed)
Modified:
   Jifty-DBI/trunk/Makefile.PL

Log:
added a YAML filter

Modified: Jifty-DBI/trunk/Makefile.PL
==============================================================================
--- Jifty-DBI/trunk/Makefile.PL	(original)
+++ Jifty-DBI/trunk/Makefile.PL	Tue Feb 20 09:46:48 2007
@@ -21,9 +21,15 @@
 build_requires('Test::More' => 0.52);
 build_requires('DBD::SQLite');
 no_index directory => 'ex';
-features( 'Memcached support' => [
-    -default => 1,
-    Cache::Memcached => '']
+features( 
+    'Memcached support' => [
+        -default => 1,
+        Cache::Memcached => ''
+    ],
+    'YAML filter' => [
+        -default => 1,
+        (can_cc() ? requires('YAML::Syck') : requires('YAML')),
+    ],
 );
 auto_install();
 &WriteAll;

Added: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/YAML.pm
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/YAML.pm	Tue Feb 20 09:46:48 2007
@@ -0,0 +1,86 @@
+use strict;
+use warnings;
+
+package Jifty::DBI::Filter::YAML;
+use base qw/ Jifty::DBI::Filter /;
+
+eval "use YAML::Syck";
+if ($@) { use YAML; }
+
+=head1 NAME
+
+Jifty::DBI::Filter::YAML - This filter stores arbitrary Perl via YAML
+
+=head1 SYNOPSIS
+
+  use Jifty::DBI::Record schema {
+      column my_data =>
+          type is 'text',
+          filters are qw/ Jifty::DBI::Filter::YAML /;
+  };
+
+  my $thing = __PACKAGE__->new;
+  $thing->create( my_data => { foo => 'bar', baz => [ 1, 2, 3 ] } );
+
+  my $my_data = $thing->my_data;
+  while (my ($key, $value) = %$my_data) {
+      # do something...
+  }
+
+=head1 DESCRIPTION
+
+This filter provides the ability to store arbitrary data structures into a database column using L<YAML>. This is very similar to the L<Jifty::DBI::Filter::Storable> filter except that the L<YAML> format remains human-readable in the database. You can store virtually any Perl data, scalar, hash, array, or object into the database using this filter. 
+
+In addition, YAML (at least the storage of scalars, hashes, and arrays) is compatible with data structures written in other languages, so you may store or read data between applications written in different languages.
+
+=head1 METHODS
+
+=head2 encode
+
+This method is used to encode the Perl data structure into YAML formatted text.
+
+=cut
+
+sub encode {
+    my $self = shift;
+
+    my $value_ref = $self->value_ref;
+    return unless defined $$value_ref;
+
+    $$value_ref = Dump($$value_ref);
+}
+
+=head2 decode
+
+This method is used to decode the YAML formatted text from the database into the Perl data structure.
+
+=cut
+
+sub decode {
+    my $self = shift;
+
+    my $value_ref = $self->value_ref;
+    return unless defined $$value_ref;
+
+    $$value_ref = Load($$value_ref);
+}
+
+=head1 IMPLEMENTATION
+
+This class will attempt to use L<YAML::Syck> if it is available and then fall back upon L<YAML>. This has been done because the Syck library is written in C and is considerably faster.
+
+=head1 SEE ALSO
+
+L<Jifty::DBI::Filter>, L<YAML>, L<YAML::Syck>
+
+=head1 AUTHOR
+
+Andrew Sterling Hanenkamp E<lt>hanenkamp at cpan.orgE<gt>
+
+=head1 LICENSE
+
+This program is free software and may be modified or distributed under the same terms as Perl itself.
+
+=cut
+
+1

Added: Jifty-DBI/trunk/t/06filter_yaml.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/t/06filter_yaml.t	Tue Feb 20 09:46:48 2007
@@ -0,0 +1,114 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 9;
+
+eval "use YAML::Syck ()";
+if ($@) {
+    eval "use YAML ()";
+    if ($@) {
+        plan skip_all => "neither YAML::Syck nor YAML is installed";
+    }
+}
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+my $complex_data = {
+    foo => 'bar',
+    baz => [ 1,  2, 3 ],
+};
+
+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', 'init schema');
+    }
+
+   my $rec = TestApp::User->new( handle => $handle );
+   isa_ok($rec, 'Jifty::DBI::Record');
+
+   my ($id) = $rec->create( my_data => $complex_data );
+   ok($id, 'created record');
+   ok($rec->load($id), 'loaded record');
+   is($rec->id, $id, 'record id matches');
+   is(ref $rec->my_data, 'HASH', 'my_data is a HASH');
+   is_deeply($rec->my_data, $complex_data, 'my_data matches initial data');
+
+   # undef/NULL
+   $rec->set_my_data;
+   is($rec->my_data, undef, 'set undef value');
+
+   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,
+    my_data text
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id integer auto_increment primary key,
+    my_data text
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id serial primary key,
+    my_data text
+)
+EOF
+
+}
+
+BEGIN {
+    use Jifty::DBI::Schema;
+
+    use Jifty::DBI::Record schema {
+    column my_data =>
+        type is 'text',
+        filters are qw/ Jifty::DBI::Filter::YAML /;
+    }
+}
+


More information about the Jifty-commit mailing list