From: Matt S Trout Date: Wed, 12 Jan 2011 13:51:30 +0000 (+0000) Subject: comment saving X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=24b96449f738eaaae2a6f09849e38cfdbbf84aaf;p=scpubgit%2FCommentry.git comment saving --- diff --git a/lib/App/Commentry/Comment.pm b/lib/App/Commentry/Comment.pm index 3670da8..18d2fe2 100644 --- a/lib/App/Commentry/Comment.pm +++ b/lib/App/Commentry/Comment.pm @@ -5,4 +5,6 @@ use Moo; has title => (is => 'ro', required => 1); has body => (is => 'ro', required => 1); +sub TO_JSON { +{ %{$_[0]} } } + 1; diff --git a/lib/App/Commentry/CommentSet.pm b/lib/App/Commentry/CommentSet.pm index ba41cfc..85b0c48 100644 --- a/lib/App/Commentry/CommentSet.pm +++ b/lib/App/Commentry/CommentSet.pm @@ -2,6 +2,9 @@ package App::Commentry::CommentSet; use aliased 'App::Commentry::Comment'; use JSON; +use File::Spec; +use File::Path; +use File::Copy; use Moo; has base_dir => (is => 'ro', required => 1); @@ -9,13 +12,18 @@ has path => (is => 'ro', required => 1); has _json => (is => 'lazy'); -sub _build__json { JSON->new->utf8->pretty } +sub _build__json { JSON->new->utf8->pretty->convert_blessed } has _members => (is => 'lazy'); +sub _filename { + my ($self) = @_; + File::Spec->catfile($self->base_dir, $self->path.'.json'); +} + sub _build__members { my ($self) = @_; - my $file = join('/', $self->base_dir, $self->path).'.json'; + my $file = $self->_filename; return [] unless -e $file; my $text = do { open my $in, '<', $file @@ -25,6 +33,16 @@ sub _build__members { [ map Comment->new($_), @{$self->_json->decode($text)} ]; } +sub _write { + my ($self) = @_; + my $file = $self->_filename; + mkpath((File::Spec->splitpath($file))[1]); + open my $out, '>', "${file}.new" + or die "Couldn't open ${file}.new to write: $!"; + print $out $self->_json->encode($self->_members); + move("${file}.new", $file); +} + sub flatten { my ($self) = @_; @{$self->_members}; @@ -36,4 +54,11 @@ sub map { App::Commentry::MappedSet->new(source => $self, mapping => $mapping); } +sub add { + my ($self, $params) = @_; + push @{$self->_members}, my $new = Comment->new($params); + $self->_write; + $new; +} + 1; diff --git a/lib/App/Commentry/CommentStore.pm b/lib/App/Commentry/CommentStore.pm index 6676cc4..64f31c1 100644 --- a/lib/App/Commentry/CommentStore.pm +++ b/lib/App/Commentry/CommentStore.pm @@ -5,7 +5,11 @@ use Moo; has base_dir => (is => 'ro', required => 1); -has _cache => (is => 'ro', default => sub { {} }); +has _cache => ( + is => 'ro', init_arg => undef, + lazy => 1, default => sub { {} }, + clearer => 'clear_cache' +); sub get { my ($self, $proto) = @_; diff --git a/t/comment_store.t b/t/comment_store.t index 2dcb5ba..2ab2197 100644 --- a/t/comment_store.t +++ b/t/comment_store.t @@ -1,5 +1,6 @@ use strictures 1; use Test::More; +use File::Path; use aliased 'App::Commentry::CommentStore'; use aliased 'App::Commentry::Comment'; @@ -16,4 +17,22 @@ is_deeply( 'Existing set loads ok' ); +my $create = { title => 'Created title', body => 'Created body' }; + +is_deeply( + $store->get({ path => 'create/new' })->add($create), + my $new = Comment->new($create), + 'Create new ok' +); + +$store->clear_cache; + +is_deeply( + [ $store->get({ path => 'create/new' })->flatten ], + [ $new ], + 'Reload ok' +); + +rmtree('t/var/exstore/create'); + done_testing;