basic show works
[scpubgit/Commentry.git] / lib / App / Commentry / CommentSet.pm
1 package App::Commentry::CommentSet;
2
3 use aliased 'App::Commentry::Comment';
4 use JSON;
5 use Moo;
6
7 has base_dir => (is => 'ro', required => 1);
8 has path => (is => 'ro', required => 1);
9
10 has _json => (is => 'lazy');
11
12 sub _build__json { JSON->new->utf8->pretty }
13
14 has _members => (is => 'lazy');
15
16 sub _build__members {
17   my ($self) = @_;
18   my $file = join('/', $self->base_dir, $self->path).'.json';
19   return [] unless -e $file;
20   my $text = do {
21     open my $in, '<', $file
22       or die "Couldn't open ${file}: $!";
23     local $/; <$in>;
24   };
25   [ map Comment->new($_), @{$self->_json->decode($text)} ];
26 }
27
28 sub flatten {
29   my ($self) = @_;
30   @{$self->_members};
31 }
32
33 sub map {
34   my ($self, $mapping) = @_;
35   require App::Commentry::MappedSet;
36   App::Commentry::MappedSet->new(source => $self, mapping => $mapping);
37 }
38
39 1;