--- /dev/null
+
+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>; };
+}
+
+sub store {
+ my ($self, $data) = @_;
+ my $fh = IO::File->new($self->file, 'w');
+ print $fh $data;
+}
\ No newline at end of file
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;
+ 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));
+ my ( $class, $data ) = @_;
+ my $e = MooseX::Storage::Engine->new( class => $class );
+ $class->new( $e->expand_object($data) );
}
sub load {
- my ($class, $filename) = @_;
- $class->unpack(JSON::Syck::LoadFile($filename));
+ my ( $class, $filename ) = @_;
+ $class->unpack(
+ $class->thaw( MooseX::Storage::IO->new( file => $filename )->load() )
+ );
}
sub store {
- my ($self, $filename) = @_;
- JSON::Syck::DumpFile($filename, $self->pack());
+ my ( $self, $filename ) = @_;
+ MooseX::Storage::IO->new( file => $filename )->store( $self->freeze() );
}
sub thaw {
- my ($class, $json) = @_;
- $class->unpack(JSON::Syck::Load($json));
+ my ( $class, $json ) = @_;
+ $class->unpack( JSON::Syck::Load($json) );
}
sub freeze {
my $self = shift;
- JSON::Syck::Dump($self->pack());
+ JSON::Syck::Dump( $self->pack() );
}
1;