From: Chris Prather Date: Thu, 29 Mar 2007 22:48:56 +0000 (+0000) Subject: refactor Storage output into an IO engine to allow better flexability X-Git-Tag: 0_02~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bff7e5f724df04544cca9d3c4ed91777910f2194;p=gitmo%2FMooseX-Storage.git refactor Storage output into an IO engine to allow better flexability --- diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index d496fd1..4e801b7 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -120,6 +120,9 @@ sub match_type { # --- pass it on # this should cover 80% of all use cases + # CHRIS: To cover the last 20% we need a way + # for people to extend this process. + # NOTE: # if this method hasnt returned by now # then we have no been able to find a diff --git a/lib/MooseX/Storage/IO/File.pm b/lib/MooseX/Storage/IO/File.pm new file mode 100644 index 0000000..69ecbd9 --- /dev/null +++ b/lib/MooseX/Storage/IO/File.pm @@ -0,0 +1,21 @@ + +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 diff --git a/lib/MooseX/Storage/JSON.pm b/lib/MooseX/Storage/JSON.pm index 7787bdc..71189e4 100644 --- a/lib/MooseX/Storage/JSON.pm +++ b/lib/MooseX/Storage/JSON.pm @@ -6,37 +6,40 @@ 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; + 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;