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;
},
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();
},
-package MooseX::Storage::IO::File;
+package MooseX::Storage::Engine::IO::File;
use Moose;
use IO::File;
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
-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 ) = @_;
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;
use Moose;
use MooseX::Storage;
- with Storage('JSON');
+ with Storage('format' => 'JSON');
has 'number' => (is => 'ro', isa => 'Int');
has 'string' => (is => 'ro', isa => 'Str');
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;