Add serialization to Gitalist::Git::Object.
Dan Brook [Fri, 6 May 2011 22:45:21 +0000 (23:45 +0100)]
Another first attempt once more based on Tom's earlier work. Much
still to be built on.

Makefile.PL
lib/Gitalist/Git/Object.pm
t/02git_object.t
t/json_view.t [new file with mode: 0644]

index def336c..6a18510 100644 (file)
@@ -96,6 +96,7 @@ requires 'File::Type::WebImages';
 requires 'File::Which';
 requires 'HTML::Entities';
 requires 'IPC::Run';
+requires 'JSON';
 requires 'JSON::XS';
 requires 'List::MoreUtils';
 requires 'Path::Class' => '0.17';
index 3906405..7596e89 100644 (file)
@@ -1,7 +1,9 @@
 use MooseX::Declare;
 use Moose::Autobox;
 
-class Gitalist::Git::Object is dirty {
+class Gitalist::Git::Object with Gitalist::Git::Serializable is dirty {
+    use MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize;
+
     use MooseX::Types::Moose qw/Str Int Bool Maybe ArrayRef/;
     use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
     use overload '""' => '_to_string', fallback => 1;
@@ -32,11 +34,12 @@ class Gitalist::Git::Object is dirty {
                 lazy_build => 1 )
         for qw/modestr size/;
 
-    has _gpp_obj => ( isa => 'Git::PurePerl::Object',
-                      required => 1,
-                      is => 'ro',
+    has _gpp_obj => ( isa        => 'Git::PurePerl::Object',
+                      required   => 1,
+                      is         => 'ro',
                       lazy_build => 1,
-                      handles => [ 'content' ],
+                      handles    => [ 'content' ],
+                      traits     => ['DoNotSerialize']
                   );
 
     # objects can't determine their mode or filename
index f3dd216..84ac439 100644 (file)
@@ -41,6 +41,27 @@ is($object->modestr, 'drwxr-xr-x', "modestr is correct" );
 is($object->size, 33, "size is correct");
 is($object,'729a7c3f6ba5453b42d16a43692205f67fb23bc1', 'stringifies correctly');
 
+is_deeply $object->pack, {
+    __CLASS__
+         => 'Gitalist::Git::Object::Tree',
+    file   => 'dir1',
+    mode   => 16384,
+    modestr
+         => 'drwxr-xr-x',
+    repository
+         => {
+             __CLASS__   => 'Gitalist::Git::Repository',
+             description => 'some test repository',
+             is_bare     => 1,
+             last_change => '2009-11-12T19:00:34Z',
+             name        => 'repo1',
+             owner       => 'Dan'
+         },
+    sha1   => '729a7c3f6ba5453b42d16a43692205f67fb23bc1',
+    size   => 33,
+    type   => 'tree'
+}, 'Serialized tree correctly';
+
 # Create object from sha1.
 my $obj2 = Gitalist::Git::Object::Blob->new(
     repository => $repository,
@@ -60,12 +81,72 @@ dies_ok {
     print $obj2->comment;
 } 'comment is an empty string';
 
+is_deeply $obj2->pack,  {
+    __CLASS__
+         => 'Gitalist::Git::Object::Blob',
+    mode   => 0,
+    modestr
+         => '----------',
+    repository
+         => {
+             __CLASS__   => 'Gitalist::Git::Repository',
+             description => 'some test repository',
+             is_bare     => 1,
+             last_change => '2009-11-12T19:00:34Z',
+             name        => 'repo1',
+             owner       => 'Dan'
+         },
+    sha1   => '5716ca5987cbf97d6bb54920bea6adde242d87e6',
+    size   => 4,
+    type   => 'blob'
+}, 'Serialized blob correctly';
+
 my $commit_obj = Gitalist::Git::Object::Commit->new(
     repository => $repository,
     sha1 => '3f7567c7bdf7e7ebf410926493b92d398333116e',
 );
 isa_ok($commit_obj, 'Gitalist::Git::Object::Commit', "commit object");
 isa_ok($commit_obj->tree->[0], 'Gitalist::Git::Object::Tree');
+
+is_deeply $commit_obj->pack,  {
+    __CLASS__
+         => 'Gitalist::Git::Object::Commit',
+    mode   => 0,
+    modestr
+         => '----------',
+    repository
+         => {
+             __CLASS__   => 'Gitalist::Git::Repository',
+             description => 'some test repository',
+             is_bare     => 1,
+             last_change => '2009-11-12T19:00:34Z',
+             name        => 'repo1',
+             owner       => 'Dan'
+         },
+    sha1   => '3f7567c7bdf7e7ebf410926493b92d398333116e',
+    size   => 218,
+    tree   => [ {
+        __CLASS__
+             => 'Gitalist::Git::Object::Tree',
+        mode   => 0,
+        modestr
+             => '----------',
+        repository
+             => {
+                 __CLASS__   => 'Gitalist::Git::Repository',
+                 description => 'some test repository',
+                 is_bare     => 1,
+                 last_change => '2009-11-12T19:00:34Z',
+                 name        => 'repo1',
+                 owner       => 'Dan'
+             },
+        sha1   => '9062594aebb5df0de7fb92413f17a9eced196c22',
+        size   => 33,
+        type   => 'tree'
+    } ],
+    type   => 'commit'
+}, 'Serialized commit correctly';
+
 my ($tree, $patch) = $commit_obj->diff(
     patch => 1,
 );
diff --git a/t/json_view.t b/t/json_view.t
new file mode 100644 (file)
index 0000000..e5a436c
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+use FindBin qw/$Bin/;
+BEGIN {
+    my $env = "$FindBin::Bin/../script/env";
+    if (-r $env) {
+        do $env or die $@;
+    }
+}
+
+use strict;
+use warnings;
+use Test::More;
+use HTTP::Request::Common;
+use JSON::XS qw/decode_json encode_json/;
+
+BEGIN {
+    $ENV{GITALIST_CONFIG} = $Bin;
+    $ENV{GITALIST_REPO_DIR} = '';
+    use_ok 'Catalyst::Test', 'Gitalist';
+}
+
+my $res = request(GET 'http://localhost/repo1', 'Content-Type' => 'application/json');
+is $res->code, 200;
+my $data = decode_json $res->content;
+is ref($data), 'HASH';
+delete $data->{owner}
+  if $data && exists $data->{owner};
+is_deeply $data, {
+          'is_bare' => 1,
+          '__CLASS__' => 'Gitalist::Git::Repository',
+          'last_change' => '2009-11-12T19:00:34Z',
+          'name' => 'repo1',
+          'description' => 'some test repository'
+        };
+
+done_testing;
+
+