From: Stevan Little Date: Fri, 30 Mar 2007 02:35:48 +0000 (+0000) Subject: roles X-Git-Tag: 0_02~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4d1850a68a911b072b7c9abba786099f9acb6a67;p=gitmo%2FMooseX-Storage.git roles --- diff --git a/lib/MooseX/Storage.pm b/lib/MooseX/Storage.pm index e3e2c3e..c6101b9 100644 --- a/lib/MooseX/Storage.pm +++ b/lib/MooseX/Storage.pm @@ -4,24 +4,42 @@ package MooseX::Storage; sub import { my $pkg = caller(); $pkg->meta->alias_method('Storage' => sub { - my $engine_name = 'MooseX::Storage::' . (shift); - Class::MOP::load_class($engine_name) - || die "Could not load engine ($engine_name) for package ($pkg)"; - return $engine_name; + my %params = @_; + + my @roles = ( + 'MooseX::Storage::Basic' + ); + + push @roles => 'MooseX::Storage::Format::' . $params{'format'}; + Class::MOP::load_class($roles[-1]) + || die "Could not load format role (" . $roles[-1] . ") for package ($pkg)"; + + if (exists $params{'io'}) { + push @roles => 'MooseX::Storage::IO::' . $params{'io'}; + Class::MOP::load_class($roles[-1]) + || die "Could not load IO role (" . $roles[-1] . ") for package ($pkg)"; + } + + return @roles; }); } -package MooseX::Storage::Base; +package MooseX::Storage::Basic; use Moose::Role; -requires 'pack'; -requires 'unpack'; +use MooseX::Storage::Engine; -requires 'freeze'; -requires 'thaw'; +sub pack { + my $self = shift; + my $e = MooseX::Storage::Engine->new( object => $self ); + $e->collapse_object; +} -requires 'load'; -requires 'store'; +sub unpack { + my ( $class, $data ) = @_; + my $e = MooseX::Storage::Engine->new( class => $class ); + $class->new( $e->expand_object($data) ); +} 1; diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index 4e801b7..f31386c 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -92,7 +92,7 @@ my %TYPES = ( }, collapse => sub { my $obj = shift; - ($obj->can('does') && $obj->does('MooseX::Storage::Base')) + ($obj->can('does') && $obj->does('MooseX::Storage::Basic')) || confess "Bad object ($obj) does not do MooseX::Storage::Base role"; $obj->pack(); }, diff --git a/lib/MooseX/Storage/Engine/IO/File.pm b/lib/MooseX/Storage/Engine/IO/File.pm index d8a209a..d6f9854 100644 --- a/lib/MooseX/Storage/Engine/IO/File.pm +++ b/lib/MooseX/Storage/Engine/IO/File.pm @@ -1,5 +1,5 @@ -package MooseX::Storage::IO::File; +package MooseX::Storage::Engine::IO::File; use Moose; use IO::File; @@ -20,4 +20,6 @@ sub store { my ($self, $data) = @_; my $fh = IO::File->new($self->file, 'w'); print $fh $data; -} \ No newline at end of file +} + +1; \ No newline at end of file diff --git a/lib/MooseX/Storage/Format/JSON.pm b/lib/MooseX/Storage/Format/JSON.pm index 00e688c..f39c293 100644 --- a/lib/MooseX/Storage/Format/JSON.pm +++ b/lib/MooseX/Storage/Format/JSON.pm @@ -1,21 +1,11 @@ -package MooseX::Storage::JSON; +package MooseX::Storage::Format::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) ); -} +requires 'pack'; +requires 'unpack'; sub thaw { my ( $class, $json ) = @_; diff --git a/lib/MooseX/Storage/IO/File.pm b/lib/MooseX/Storage/IO/File.pm index 5d34463..957419a 100644 --- a/lib/MooseX/Storage/IO/File.pm +++ b/lib/MooseX/Storage/IO/File.pm @@ -2,16 +2,19 @@ package MooseX::Storage::IO::File; use Moose::Role; -use MooseX::Storage::IO::File; +use MooseX::Storage::Engine::IO::File; + +requires 'thaw'; +requires 'freeze'; sub load { my ( $class, $filename ) = @_; - $class->thaw( MooseX::Storage::IO::File->new( file => $filename )->load() ); + $class->thaw( MooseX::Storage::Engine::IO::File->new( file => $filename )->load() ); } sub store { my ( $self, $filename ) = @_; - MooseX::Storage::IO::File->new( file => $filename )->store( $self->freeze() ); + MooseX::Storage::Engine::IO::File->new( file => $filename )->store( $self->freeze() ); } 1; diff --git a/t/001_basic.t b/t/001_basic.t index 3d78e23..9c005bf 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -10,7 +10,7 @@ use Test::More no_plan => 1; use Moose; use MooseX::Storage; - with Storage('JSON'); + with Storage('format' => 'JSON'); has 'number' => (is => 'ro', isa => 'Int'); has 'string' => (is => 'ro', isa => 'Str'); @@ -50,34 +50,3 @@ use Test::More no_plan => 1; 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;