File Coverage

blib/lib/Hub/Data/Directory.pm
Criterion Covered Total %
statement 32 54 59.2
branch 5 26 19.2
condition 2 8 25.0
subroutine 4 7 57.1
pod 5 5 100.0
total 48 100 48.0


line stmt bran cond sub pod time code
1             package Hub::Data::Directory;
2 1     1   5 use strict;
  1         3  
  1         33  
3 1     1   4 use Hub qw/:lib/;
  1         2  
  1         6  
4             our $AUTOLOAD = '';
5             our $VERSION = '4.00043';
6             our @EXPORT = qw//;
7             our @EXPORT_OK = qw//;
8              
9             sub new {
10 8     8 1 12 my $self = shift;
11 8   33     35 my $classname = ref($self) || $self;
12 8 50       21 my $path = shift or croak "Provide a path";
13 8         44 $path = Hub::abspath($path, -must_exist);
14 8 50       22 return unless defined $path;
15 8         27 my $obj = Hub::fhandler($path, $classname);
16 8 50       20 unless($obj) {
17 8         30 $obj = bless {}, $classname;
18 8         56 tie %$obj, 'Hub::Knots::TiedObject', 'Hub::Knots::SortedHash';
19 8         44 $obj->{'*path'} = $path;
20 8         34 $obj->{'*stats'} = stat($path);
21 8         31 Hub::fattach($path, $obj);
22             }
23 8         65 return $obj;
24             }
25              
26             sub get_data {
27 0     0 1 0 my $self = shift;
28 0         0 my $index = shift;
29 0 0       0 croak "Illegal call to instance method" unless ref($self);
30 0 0       0 if (defined $index) {
31 0 0 0     0 $self->{$index} = Hub::mkhandler("$self->{'*path'}/$index")
32             if -e "$self->{'*path'}/$index" && !defined $self->{$index};
33 0         0 return $index =~ /^\{(.*)\}$/
34             ? mkinst('Subset', # map {$self->{'*path'}.'/'.$_}
35 0 0       0 grep {/$1/} keys %{$self->{'*public'}})
  0         0  
36             : $self->{$index};
37             }
38 0         0 return $self;
39             }
40              
41             sub get_content {
42 0     0 1 0 my $self = shift;
43 0 0       0 croak "Illegal call to instance method" unless ref($self);
44 0         0 return $self;
45             }
46              
47             sub reload {
48 8     8 1 10 my $self = shift;
49 8 50       19 croak "Illegal call to instance method" unless ref($self);
50 8         10 my $instance = shift;
51 8         13 my $dir = $instance->{'filename'};
52             #warn "-loading: $dir\n";
53             # Sorted list
54 8         11 my @list = sort grep {!/^\./} @{$instance->{'contents'}};
  64         135  
  8         18  
55 8         31 Hub::sort_dir_list($dir, \@list);
56             # Stub out each entry
57 8         48 $self->{'*tied'}->set_sort_keys(@list);
58 8   50     39 $self->{$_} ||= undef for (@list);
59             # Prune deleted files from our list
60 8         32 foreach my $k (keys %$self) {
61 59 50       63 delete $self->{$k} unless grep {$_ eq $k} @list;
  615         786  
62             }
63             }
64              
65             sub set_sort_order {
66 0     0 1   my $self = shift;
67 0 0         croak "Illegal call to instance method" unless ref($self);
68 0           my $sort_order = shift;
69 0 0         croak "Provide an array reference" unless isa($sort_order, 'ARRAY');
70 0           my $md_filename = $self->{'*path'} . '/' . Hub::META_FILENAME;
71 0           my $md = $$Hub{Hub::getaddr($md_filename)};
72 0 0         $md = mkinst('HashFile', $md_filename) unless (defined $md);
73 0           $md->{'sort_order'} = $sort_order;
74 0           $md->save();
75 0           Hub::frefresh($self->{'*path'}, -force);
76             }
77              
78             # ------------------------------------------------------------------------------
79             1;
80              
81             __END__