factor out child code into PageChildren and add an all_paths method to PageSet
[scpubgit/SCS.git] / lib / SCSite / PageSet.pm
index 60a2a62..c5854fb 100644 (file)
@@ -10,6 +10,8 @@ use Try::Tiny;
 use JSON;
 use Moo;
 
+with 'SCSite::PageChildren';
+
 {
   my $j = JSON->new;
   sub _json { $j }
@@ -28,6 +30,10 @@ sub _build_rel_path {
     ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
 }
 
+sub _page_set_class { ref($_[0]) }
+sub _top_dir { shift->top_dir }
+sub _my_path { shift->base_dir }
+
 sub get {
   my ($self, $spec) = @_;
   $spec->{path} or die "path is required to get";
@@ -83,10 +89,7 @@ sub _depth_under_base {
 
 sub flatten {
   my ($self) = @_;
-  return unless (my $base = $self->base_dir)->exists;
-  my %seen;
   my $slash = io->dir('/');
-  my $min = $self->min_depth;
   map {
     my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
     $self->_inflate(
@@ -94,7 +97,24 @@ sub flatten {
       $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
       $_
     );
-  } map {
+  } $self->_all_files;
+}
+
+sub all_paths {
+  my ($self) = @_;
+  my $slash = io->dir('/');
+  map {
+    my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
+    $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
+  } $self->_all_files;
+}
+
+sub _all_files {
+  my ($self) = @_;
+  return unless (my $base = $self->base_dir)->exists;
+  my %seen;
+  my $min = $self->min_depth;
+  map {
     $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
       ->all_files($self->max_depth - ($min-1))
   } map