| 1 | package SWISH::Prog::InvIndex; | ||
| 2 | |||
| 3 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 11 | ▶ | use base qw( SWISH::Prog::Class ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 11 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 11 | ▶ | use Path::Class (); # do not import file() and dir() |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 11 | ▶ | use Scalar::Util qw( blessed ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use SWISH::Prog::InvIndex::Meta; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use overload( | ||
| 11 | '""' => sub { shift->path }, | ||
| 12 | 'bool' => sub {1}, | ||
| 13 | fallback => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | our $VERSION = '0.75'; | ||
| 17 | |||
| 18 | __PACKAGE__->mk_accessors(qw( path clobber )); | ||
| 19 | |||
| 20 | sub init { | ||
| 21 | my $self = shift; | ||
| 22 | $self->SUPER::init(@_); | ||
| 23 | my $path = $self->{path} || $self->{invindex} || 'index.swish'; | ||
| 24 | |||
| 25 | unless ( blessed($path) && $path->isa('Path::Class::Dir') ) { | ||
| 26 | $self->path( Path::Class::dir($path) ); | ||
| 27 | } | ||
| 28 | |||
| 29 | $self->{clobber} = 0 unless exists $self->{clobber}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub new_from_meta { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | # open swish.xml meta file | ||
| 36 | my $meta = $self->meta; | ||
| 37 | |||
| 38 | # parse for index format | ||
| 39 | my $format = $meta->Index->{Format}; | ||
| 40 | |||
| 41 | # create new object and re-set $self | ||
| 42 | my $newclass = "SWISH::Prog::${format}::InvIndex"; | ||
| 43 | |||
| 44 | warn "reblessing $self into $newclass"; | ||
| 45 | |||
| 46 | eval "require $newclass"; | ||
| 47 | croak $@ if $@; | ||
| 48 | |||
| 49 | return $newclass->new( | ||
| 50 | path => $self->{path}, | ||
| 51 | clobber => $self->{clobber}, | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub open { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | if ( -d $self->path && $self->clobber ) { | ||
| 59 | $self->path->rmtree( $self->verbose, 1 ); | ||
| 60 | } | ||
| 61 | elsif ( -f $self->path ) { | ||
| 62 | croak $self->path | ||
| 63 | . " is not a directory -- won't even attempt to clobber"; | ||
| 64 | } | ||
| 65 | |||
| 66 | if ( !-d $self->path ) { | ||
| 67 | carp "no path $self->{path} -- mkpath"; | ||
| 68 | $self->path->mkpath( $self->verbose ); | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub open_ro { | ||
| 75 | shift->open(@_); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub close { 1; } | ||
| 79 | |||
| 80 | sub meta { | ||
| 81 | my $self = shift; | ||
| 82 | return SWISH::Prog::InvIndex::Meta->new( invindex => $self ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub meta_file { | ||
| 86 | my $self = shift; | ||
| 87 | return $self->path->file( | ||
| 88 | SWISH::Prog::InvIndex::Meta->swish_header_file ); | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ | ||