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) = @_;
=head1 NAME
-MooseX::Storage::Engine::IO::File
+MooseX::Storage::Engine::IO::AtomicFile
=head1 SYNOPSIS
--- /dev/null
+
+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<load ($filename)>
+
+=item B<store ($filename)>
+
+=back
+
+=head2 Introspection
+
+=over 4
+
+=item B<meta>
+
+=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 E<lt>chris.prather@iinteractive.comE<gt>
+
+Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 by Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+
--- /dev/null
+#!/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;
--- /dev/null
+#!/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;