From: Matt S Trout Date: Sun, 23 Oct 2011 03:41:24 +0000 (+0000) Subject: factor out child code into PageChildren and add an all_paths method to PageSet X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cbb06a720cd579a1754fc772ee4ce73cd99d921b;p=scpubgit%2FSCS.git factor out child code into PageChildren and add an all_paths method to PageSet --- diff --git a/lib/SCSite/Page.pm b/lib/SCSite/Page.pm index 7d09627..028483e 100644 --- a/lib/SCSite/Page.pm +++ b/lib/SCSite/Page.pm @@ -4,6 +4,8 @@ use IO::All; use Time::Local qw(timelocal); use Moo; +with 'SCSite::PageChildren'; + has $_ => (is => 'ro') for qw( title subtitle description keywords body created path ); @@ -18,18 +20,8 @@ sub published_at { 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; diff --git a/lib/SCSite/PageChildren.pm b/lib/SCSite/PageChildren.pm new file mode 100644 index 0000000..42590f9 --- /dev/null +++ b/lib/SCSite/PageChildren.pm @@ -0,0 +1,22 @@ +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; diff --git a/lib/SCSite/PageSet.pm b/lib/SCSite/PageSet.pm index 60a2a62..c5854fb 100644 --- a/lib/SCSite/PageSet.pm +++ b/lib/SCSite/PageSet.pm @@ -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