use Time::Local qw(timelocal);
use Moo;
+with 'SCSite::PageChildren';
+
has $_ => (is => 'ro') for qw(
title subtitle description keywords body created path
);
has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
-sub children {
- my ($self, %args) = @_;
- if (my $at = delete $args{at_depth}) {
- @args{qw(min_depth max_depth)} = ($at, $at);
- }
- my $ps = $self->_page_set;
- (ref $ps)->new(
- top_dir => $ps->base_dir,
- base_dir => io->dir($ps->base_dir)->catdir($self->path),
- max_depth => 1,
- %args,
- );
-}
+sub _page_set_class { ref($_[0]->_page_set) }
+sub _top_dir { $_[0]->_page_set->top_dir }
+sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
1;
--- /dev/null
+package SCSite::PageChildren;
+
+use Moo::Role;
+
+requires '_page_set_class';
+requires '_top_dir';
+requires '_my_path';
+
+sub children {
+ my ($self, %args) = @_;
+ if (my $at = delete $args{at_depth}) {
+ @args{qw(min_depth max_depth)} = ($at, $at);
+ }
+ $self->_page_set_class->new(
+ top_dir => $self->_top_dir,
+ base_dir => $self->_my_path,
+ max_depth => 1,
+ %args,
+ );
+}
+
+1;
use JSON;
use Moo;
+with 'SCSite::PageChildren';
+
{
my $j = JSON->new;
sub _json { $j }
->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";
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(
$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