From: Stevan Little Date: Fri, 30 Mar 2007 20:44:17 +0000 (+0000) Subject: atomic file stuff X-Git-Tag: 0_02~28 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Storage.git;a=commitdiff_plain;h=e64b730236154a4f45ace9db2bbf915bd91956f1 atomic file stuff --- diff --git a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm index 902f7c5..a75c71d 100644 --- a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm +++ b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm @@ -4,22 +4,7 @@ use Moose; use IO::AtomicFile; -has 'file' => ( - is => 'ro', - isa => 'Str', - required => 1, -); - -sub load { - my ($self) = @_; - # NOTE:sv - # AtomicFile gives us no real - # benefit when reading, so why - # bother - # - SL - my $fh = IO::File->new($self->file, 'r'); - return do { local $/; <$fh>; }; -} +extends 'MooseX::Storage::Engine::IO::File'; sub store { my ($self, $data) = @_; @@ -37,7 +22,7 @@ __END__ =head1 NAME -MooseX::Storage::Engine::IO::File +MooseX::Storage::Engine::IO::AtomicFile =head1 SYNOPSIS diff --git a/lib/MooseX/Storage/IO/AtomicFile.pm b/lib/MooseX/Storage/IO/AtomicFile.pm new file mode 100644 index 0000000..8d29e18 --- /dev/null +++ b/lib/MooseX/Storage/IO/AtomicFile.pm @@ -0,0 +1,69 @@ + +package MooseX::Storage::IO::AtomicFile; +use Moose::Role; + +use MooseX::Storage::Engine::IO::AtomicFile; + +with 'MooseX::Storage::IO::File'; + +sub store { + my ( $self, $filename ) = @_; + MooseX::Storage::Engine::IO::AtomicFile->new( file => $filename )->store( $self->freeze() ); +} + +1; + +__END__ + +=pod + +=head1 NAME + +MooseX::Storage::IO::AtomicFile + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=head1 METHODS + +=over 4 + +=item B + +=item B + +=back + +=head2 Introspection + +=over 4 + +=item B + +=back + +=head1 BUGS + +All complex software has bugs lurking in it, and this module is no +exception. If you find a bug please either email me, or add the bug +to cpan-RT. + +=head1 AUTHOR + +Chris Prather Echris.prather@iinteractive.comE + +Stevan Little Estevan.little@iinteractive.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2007 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + + diff --git a/t/012_basic_json_io_atomic.t b/t/012_basic_json_io_atomic.t new file mode 100644 index 0000000..8f28dbd --- /dev/null +++ b/t/012_basic_json_io_atomic.t @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More no_plan => 1; + +BEGIN { + use_ok('MooseX::Storage'); +} + +{ + package Foo; + use Moose; + use MooseX::Storage; + + with Storage(format => 'JSON', io => 'AtomicFile'); + + has 'number' => (is => 'ro', isa => 'Int'); + has 'string' => (is => 'ro', isa => 'Str'); + has 'float' => (is => 'ro', isa => 'Num'); + has 'array' => (is => 'ro', isa => 'ArrayRef'); + has 'hash' => (is => 'ro', isa => 'HashRef'); + has 'object' => (is => 'ro', isa => '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; diff --git a/t/022_basic_yaml_io_atomic.t b/t/022_basic_yaml_io_atomic.t new file mode 100644 index 0000000..632def9 --- /dev/null +++ b/t/022_basic_yaml_io_atomic.t @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More no_plan => 1; + +BEGIN { + use_ok('MooseX::Storage'); +} + +{ + package Foo; + use Moose; + use MooseX::Storage; + + with Storage(format => 'YAML', io => 'AtomicFile'); + + has 'number' => (is => 'ro', isa => 'Int'); + has 'string' => (is => 'ro', isa => 'Str'); + has 'float' => (is => 'ro', isa => 'Num'); + has 'array' => (is => 'ro', isa => 'ArrayRef'); + has 'hash' => (is => 'ro', isa => 'HashRef'); + has 'object' => (is => 'ro', isa => 'Object'); +} + +my $file = 'temp.yaml'; + +{ + 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;