App-StaticImageGallery

 view release on metacpan or  search on metacpan

lib/App/StaticImageGallery/Dir.pm  view on Meta::CPAN

package App::StaticImageGallery::Dir;
BEGIN {
  $App::StaticImageGallery::Dir::VERSION = '0.002';
}

use Path::Class    ();
use File::Basename ();
use App::StaticImageGallery::Image;
use App::StaticImageGallery::Style::Source::Dispatcher;
use parent 'App::StaticImageGallery::Base::Any';

sub init {
    my $self = shift;
    my %args = @_;
    $self->msg_verbose(10,"App::StaticImageGallery::Dir->init: %s",$args{work_dir});
    foreach my $key (qw/tt_dir tt_class link_to_parent_dir work_dir/){
        if ( defined $args{$key} ) {
            $self->{'_' . $key} = $args{$key};
        }
    }
    $self->{init_args} = \%args;

    return $self;
}


# 
# Images
########################################################################################

sub _build__images {
    my $self   = shift;
    my @images = ();
    while (my $file = $self->work_dir->next) {
        if ( $file->is_dir and ( $self->opt->get_recursive() > 0 ) ){
            $self->msg_verbose(10,"Check dir %s",File::Basename::basename($file->stringify));
            unless (
                File::Basename::basename($file->stringify) =~ /^\./ 
                or $self->work_dir->stringify eq $file->stringify
            ){
                $self->msg_verbose(1,"Go into %s",$file);
                $self->add_dir(File::Basename::basename($file->stringify));

                my $args = $self->{init_args};
                $args->{work_dir} = $file;
                $args->{link_to_parent_dir} = 1;
                my $dir = App::StaticImageGallery::Dir->new(%$args);
                $dir->write_index;
            }
        }else{
            if ( $file->stringify =~ /.*(jpg|png|jpeg|tif)$/i ){
                $self->msg_verbose(10,"Push Image %s - %s",$self->work_dir,$file->basename);
                push @images, App::StaticImageGallery::Image->new(
                    dir      => $self,
                    original => $file->basename,
                    ctx      => $self->ctx,
                );
            }

        }
    }
    return \@images;
}

sub all_images {
    my $self = shift;
    $self->{_images} = $self->_build__images() unless ( defined $self->{_images} );
    return wantarray ? @{ $self->{_images} } : $self->{_images};
};
sub images { return shift->all_images };


sub count_images{
    my $self = shift;
    return scalar @{ $self->all_images }
};
sub get_image {
    my $self = shift;
    my $indx = shift;
    $self->{_images} = $self->_build__images() unless ( defined $self->{_images} );
    return $self->{_images}[$indx];
};

# 
# Directories 
########################################################################################
sub _build__dirs { return [] };
sub all_dirs {
    my $self = shift;
    $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
    return wantarray ? @{ $self->{_dirs} } : $self->{_dirs};
}
sub dirs { return shift->all_dirs };
sub count_dirs{
    my $self = shift;
    return scalar @{ $self->all_dirs }
};
sub get_dir {
    my $self = shift;
    my $indx = shift;
    $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
    return $self->{_dirs}[$indx];
};
sub add_dir {
    my $self = shift;
    my $dir = shift;
    $self->{_dirs} = $self->_build__dirs() unless ( defined $self->{_dirs} );
    push @{$self->{_dirs}}, $dir;
    return wantarray ? @{ $self->{_dirs} } : $self->{_dirs};
};



( run in 2.372 seconds using v1.01-cache-2.11-cpan-df04353d9ac )