[Jifty-commit] jifty branch, master, updated. 1.10518-69-g87dc253

Jifty commits jifty-commit at lists.jifty.org
Mon Mar 19 09:25:58 EDT 2012


The branch, master has been updated
       via  87dc2539994d2e99b1c509089c2016922a8a7e94 (commit)
      from  80ce185bb11242a59abd08244497fa2b56b39ff4 (commit)

Summary of changes:
 lib/Jifty/Filter/JSON.pm            |   80 +++++++++++++++++++++++++++++++++++
 t/TestApp/lib/TestApp/Model/User.pm |    3 +
 t/TestApp/t/00-model-User.t         |    6 ++-
 3 files changed, 88 insertions(+), 1 deletions(-)
 create mode 100644 lib/Jifty/Filter/JSON.pm

- Log -----------------------------------------------------------------
commit 87dc2539994d2e99b1c509089c2016922a8a7e94
Author: Luke Closs <luke at primeradiant.com>
Date:   Fri Mar 2 13:47:26 2012 -0800

    Add Jifty::Filter::JSON

diff --git a/lib/Jifty/Filter/JSON.pm b/lib/Jifty/Filter/JSON.pm
new file mode 100644
index 0000000..eb760d7
--- /dev/null
+++ b/lib/Jifty/Filter/JSON.pm
@@ -0,0 +1,80 @@
+use strict;
+use warnings;
+
+package Jifty::Filter::JSON;
+use base qw/ Jifty::DBI::Filter /;
+use Jifty::JSON qw(encode_json decode_json);
+
+=head1 NAME
+
+Jifty::Filter::JSON - This filter stores arbitrary Perl via JSON
+
+=head1 SYNOPSIS
+
+  use Jifty::DBI::Record schema {
+      column my_data =>
+          type is 'text',
+          filters are qw/ Jifty::Filter::JSON /;
+  };
+
+  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<JSON>. This is very similar to the L<Jifty::DBI::Filter::Storable> filter except that the L<JSON> format remains human-readable in the database. You can store virtually any Perl data, scalar, hash, or array into the database using this filter. 
+
+In addition, JSON (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 JSON formatted text.
+
+=cut
+
+sub encode {
+    my $self = shift;
+
+    my $value_ref = $self->value_ref;
+    return unless defined $$value_ref;
+
+    $$value_ref = encode_json($$value_ref);
+}
+
+=head2 decode
+
+This method is used to decode the JSON 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 = decode_json($$value_ref);
+}
+
+=head1 SEE ALSO
+
+L<Jifty::DBI::Filter>, L<Jifty::JSON>
+
+=head1 AUTHOR
+
+Luke Closs E<lt>cpan at 5thplane.comE<gt>
+
+=head1 LICENSE
+
+This program is free software and may be modified or distributed under the same terms as Perl itself.
+
+=cut
+
+1
diff --git a/t/TestApp/lib/TestApp/Model/User.pm b/t/TestApp/lib/TestApp/Model/User.pm
index 27e7a93..6a380cb 100644
--- a/t/TestApp/lib/TestApp/Model/User.pm
+++ b/t/TestApp/lib/TestApp/Model/User.pm
@@ -34,6 +34,9 @@ column 'created_on' =>
   is immutable,
   default is defer { DateTime->now },
   filters are qw(Jifty::Filter::DateTime Jifty::DBI::Filter::Date);
+column 'my_data' =>
+  type is 'text',
+  filters are qw/Jifty::Filter::JSON/;
 };
 
 
diff --git a/t/TestApp/t/00-model-User.t b/t/TestApp/t/00-model-User.t
index 8f801fe..3aaad7c 100644
--- a/t/TestApp/t/00-model-User.t
+++ b/t/TestApp/t/00-model-User.t
@@ -8,7 +8,7 @@ A basic test harness for the User model.
 
 =cut
 
-use Jifty::Test::Dist tests => 24;
+use Jifty::Test::Dist tests => 26;
 Jifty::Test->web; # initialize for use with the as_*_action tests
 # Make sure we can load the model
 use_ok('TestApp::Model::User');
@@ -25,6 +25,10 @@ ok($o->id, "New User has valid id set");
 is($o->id, $id, "Create returned the right id");
 is($o->name, $$, "Created object has the right name");
 
+is $o->my_data, undef, 'my_data is undef';
+$o->set_my_data( { foo => 'bar' } );
+is_deeply $o->my_data, { foo => 'bar' }, 'my_data filtered correctly';
+
 # Test the as_foo_action methods
 my $action = $o->as_create_action( moniker => 'test1' );
 isa_ok($action, 'TestApp::Action::CreateUser');

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list