From: Stevan Little Date: Fri, 30 Mar 2007 02:21:16 +0000 (+0000) Subject: moving things around X-Git-Tag: 0_02~35 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a23e18d73284bb17b1c158787cf8c315ac772887;p=gitmo%2FMooseX-Storage.git moving things around --- diff --git a/lib/MooseX/Storage/Engine/IO/File.pm b/lib/MooseX/Storage/Engine/IO/File.pm new file mode 100644 index 0000000..d8a209a --- /dev/null +++ b/lib/MooseX/Storage/Engine/IO/File.pm @@ -0,0 +1,23 @@ + +package MooseX::Storage::IO::File; +use Moose; + +use IO::File; + +has file => ( + isa => 'Str', + is => 'ro', + required => 1, +); + +sub load { + my ($self) = @_; + my $fh = IO::File->new($self->file, 'r'); + return do { local $/; <$fh>; }; +} + +sub store { + my ($self, $data) = @_; + my $fh = IO::File->new($self->file, 'w'); + print $fh $data; +} \ No newline at end of file diff --git a/lib/MooseX/Storage/Format/JSON.pm b/lib/MooseX/Storage/Format/JSON.pm new file mode 100644 index 0000000..00e688c --- /dev/null +++ b/lib/MooseX/Storage/Format/JSON.pm @@ -0,0 +1,37 @@ + +package MooseX::Storage::JSON; +use Moose::Role; + +use JSON::Syck (); +use MooseX::Storage::Engine; + +sub pack { + my $self = shift; + my $e = MooseX::Storage::Engine->new( object => $self ); + $e->collapse_object; +} + +sub unpack { + my ( $class, $data ) = @_; + my $e = MooseX::Storage::Engine->new( class => $class ); + $class->new( $e->expand_object($data) ); +} + +sub thaw { + my ( $class, $json ) = @_; + $class->unpack( JSON::Syck::Load($json) ); +} + +sub freeze { + my $self = shift; + JSON::Syck::Dump( $self->pack() ); +} + +1; + +__END__ + +=pod + +=cut + diff --git a/lib/MooseX/Storage/IO/File.pm b/lib/MooseX/Storage/IO/File.pm index 69ecbd9..5d34463 100644 --- a/lib/MooseX/Storage/IO/File.pm +++ b/lib/MooseX/Storage/IO/File.pm @@ -1,21 +1,24 @@ package MooseX::Storage::IO::File; -use Moose; - -has file => ( - isa => 'Str', - is => 'ro', - required => 1, -); - -sub load { - my ($self) = @_; - my $fh = IO::File->new($self->file, 'r'); - return do { local $/; <$fh>; }; +use Moose::Role; + +use MooseX::Storage::IO::File; + +sub load { + my ( $class, $filename ) = @_; + $class->thaw( MooseX::Storage::IO::File->new( file => $filename )->load() ); } sub store { - my ($self, $data) = @_; - my $fh = IO::File->new($self->file, 'w'); - print $fh $data; -} \ No newline at end of file + my ( $self, $filename ) = @_; + MooseX::Storage::IO::File->new( file => $filename )->store( $self->freeze() ); +} + +1; + +__END__ + +=pod + +=cut + diff --git a/lib/MooseX/Storage/JSON.pm b/lib/MooseX/Storage/JSON.pm deleted file mode 100644 index 71189e4..0000000 --- a/lib/MooseX/Storage/JSON.pm +++ /dev/null @@ -1,52 +0,0 @@ - -package MooseX::Storage::JSON; -use Moose::Role; - -with 'MooseX::Storage::Base'; - -use JSON::Syck (); -use MooseX::Storage::Engine; -use MooseX::Storage::IO::File; - -sub pack { - my $self = shift; - my $e = MooseX::Storage::Engine->new( object => $self ); - $e->collapse_object; -} - -sub unpack { - my ( $class, $data ) = @_; - my $e = MooseX::Storage::Engine->new( class => $class ); - $class->new( $e->expand_object($data) ); -} - -sub load { - my ( $class, $filename ) = @_; - $class->unpack( - $class->thaw( MooseX::Storage::IO->new( file => $filename )->load() ) - ); -} - -sub store { - my ( $self, $filename ) = @_; - MooseX::Storage::IO->new( file => $filename )->store( $self->freeze() ); -} - -sub thaw { - my ( $class, $json ) = @_; - $class->unpack( JSON::Syck::Load($json) ); -} - -sub freeze { - my $self = shift; - JSON::Syck::Dump( $self->pack() ); -} - -1; - -__END__ - -=pod - -=cut - diff --git a/t/001_basic.t b/t/001_basic.t index 6ffbeac..3d78e23 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -20,28 +20,64 @@ use Test::More no_plan => 1; has 'object' => (is => 'ro', isa => 'Object'); } -my $foo = Foo->new( - number => 10, - string => 'foo', - float => 10.5, - array => [ 1 .. 10 ], - hash => { map { $_ => undef } (1 .. 10) }, - object => Foo->new( number => 2 ), -); - -is($foo->freeze, -'{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}', -'... got the right JSON'); - -my $foo2 = Foo->thaw('{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}'); -isa_ok($foo2, 'Foo'); - -is($foo2->number, 10, '... got the right number'); -is($foo2->string, 'foo', '... got the right string'); -is($foo2->float, 10.5, '... got the right float'); -is_deeply($foo2->array, [ 1 .. 10], '... got the right array'); -is_deeply($foo2->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); - -isa_ok($foo2->object, 'Foo'); -is($foo2->object->number, 2, '... got the right number (in the embedded object)'); +{ + my $foo = Foo->new( + number => 10, + string => 'foo', + float => 10.5, + array => [ 1 .. 10 ], + hash => { map { $_ => undef } (1 .. 10) }, + object => Foo->new( number => 2 ), + ); + isa_ok($foo, 'Foo'); + + is($foo->freeze, + '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}', + '... got the right JSON'); +} + +{ + my $foo = Foo->thaw('{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}'); + isa_ok($foo, 'Foo'); + + is($foo->number, 10, '... got the right number'); + is($foo->string, 'foo', '... got the right string'); + is($foo->float, 10.5, '... got the right float'); + is_deeply($foo->array, [ 1 .. 10], '... got the right array'); + is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); + + isa_ok($foo->object, 'Foo'); + is($foo->object->number, 2, '... got the right number (in the embedded object)'); +} + +my $file = 'temp.json'; + +{ + my $foo = Foo->new( + number => 10, + string => 'foo', + float => 10.5, + array => [ 1 .. 10 ], + hash => { map { $_ => undef } (1 .. 10) }, + object => Foo->new( number => 2 ), + ); + isa_ok($foo, 'Foo'); + + $foo->store($file); +} + +{ + my $foo = Foo->load($file); + isa_ok($foo, 'Foo'); + + is($foo->number, 10, '... got the right number'); + is($foo->string, 'foo', '... got the right string'); + is($foo->float, 10.5, '... got the right float'); + is_deeply($foo->array, [ 1 .. 10], '... got the right array'); + is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); + + isa_ok($foo->object, 'Foo'); + is($foo->object->number, 2, '... got the right number (in the embedded object)'); +} +unlink $file;