1 package App::Commentry::CommentSet;
3 use aliased 'App::Commentry::Comment';
10 has base_dir => (is => 'ro', required => 1);
11 has path => (is => 'ro', required => 1);
13 has _json => (is => 'lazy');
15 sub _build__json { JSON->new->utf8->pretty->convert_blessed }
17 has _members => (is => 'lazy');
21 File::Spec->catfile($self->base_dir, $self->path.'.json');
26 my $file = $self->_filename;
27 return [] unless -e $file;
29 open my $in, '<', $file
30 or die "Couldn't open ${file}: $!";
33 [ map Comment->new($_), @{$self->_json->decode($text)} ];
38 my $file = $self->_filename;
39 mkpath((File::Spec->splitpath($file))[1]);
40 open my $out, '>', "${file}.new"
41 or die "Couldn't open ${file}.new to write: $!";
42 print $out $self->_json->encode($self->_members);
43 move("${file}.new", $file);
52 my ($self, $mapping) = @_;
53 require App::Commentry::MappedSet;
54 App::Commentry::MappedSet->new(source => $self, mapping => $mapping);
58 my ($self, $params) = @_;
59 push @{$self->_members}, my $new = Comment->new($params);