File Coverage

blib/lib/Hub/Data/HashFile.pm
Criterion Covered Total %
statement 6 54 11.1
branch 0 28 0.0
condition 0 3 0.0
subroutine 2 10 20.0
pod 8 8 100.0
total 16 103 15.5


line stmt bran cond sub pod time code
1             package Hub::Data::HashFile;
2 1     1   7 use strict;
  1         2  
  1         46  
3 1     1   5 use Hub qw/:lib :console/;
  1         3  
  1         9  
4              
5             our $VERSION = '4.00043';
6             our @EXPORT = qw//;
7             our @EXPORT_OK = qw//;
8              
9             # ------------------------------------------------------------------------------
10             # new - Constructor.
11             # ------------------------------------------------------------------------------
12              
13             sub new {
14 0     0 1   my $self = shift;
15 0   0       my $classname = ref($self) || $self;
16 0 0         my $path = shift or croak "Provide a path";
17 0           my $obj = Hub::fhandler($path, $classname);
18 0 0         unless($obj) {
19 0           $obj = bless {}, $classname;
20 0           tie %$obj, 'Hub::Knots::TiedObject', 'Hub::Knots::SortedHash';
21 0           $obj->{'*path'} = $path;
22 0           $obj->{'*stats'} = stat($path);
23 0           Hub::fattach($path, $obj);
24             }
25 0           return $obj;
26             }
27              
28             # ------------------------------------------------------------------------------
29             # reload - Callback from L when a read from disk is performed
30             # ------------------------------------------------------------------------------
31              
32             sub reload {
33 0     0 1   my ($self,$opts,$file) = Hub::objopts(\@_);
34 0 0         croak "Illegal call to instance method" unless ref($self);
35 0           for (keys %$self) { delete $self->{$_}; }
  0            
36 0           Hub::hparse(\$file->{'contents'}, -into => $self, -hint => $self->{'*path'});
37             #warn "data:\n\n", Hub::hprint($self), "\n\n";
38             #warn "contents:\n\n", $file->{'contents'}, "\n\n";
39             }
40              
41             # ------------------------------------------------------------------------------
42             # get_data - Return data structure
43             # ------------------------------------------------------------------------------
44              
45             sub get_data {
46 0     0 1   my $self = shift;
47 0           my $index = shift;
48 0 0         croak "Illegal call to instance method" unless ref($self);
49 0 0         if (defined $index) {
50 0           return Hub::subset($self->{'*public'}, $index);
51             }
52 0           return $self;
53             }
54              
55             # ------------------------------------------------------------------------------
56             # set_data - Set data
57             # ------------------------------------------------------------------------------
58              
59             sub set_data {
60 0     0 1   my ($opts,$self,$value) = Hub::opts(\@_, {'index' => '/'});
61 0 0         croak "Illegal call to instance method" unless ref($self);
62 0 0         if ($$opts{'index'} eq '/') {
63 0 0         die "Provide a hash value for the root element" unless isa($value, 'HASH');
64 0           %{$self->{'*public'}} = %{$value};
  0            
  0            
65             } else {
66 0           Hub::setv($self->{'*public'}, $$opts{'index'}, $value);
67             }
68             }
69              
70             # ------------------------------------------------------------------------------
71             # get_content - Return file contents
72             # get_content [options]
73             #
74             # options:
75             #
76             # -as_ref => 1 # Return a reference
77             # ------------------------------------------------------------------------------
78              
79             sub get_content {
80 0     0 1   my ($opts, $self) = Hub::opts(\@_, {'as_ref' => 0});
81 0 0         croak "Illegal call to instance method" unless ref($self);
82 0           return Hub::hprint($self, -as_ref => $$opts{'as_ref'});
83             }
84              
85             # ------------------------------------------------------------------------------
86             # set_content - Set file contents
87             # ------------------------------------------------------------------------------
88              
89             sub set_content {
90 0     0 1   my $self = shift;
91 0           my $contents = shift;
92 0 0         croak "Illegal call to instance method" unless ref($self);
93 0           for (keys %$self) { delete $self->{$_}; }
  0            
94 0 0         Hub::hparse(ref($contents) ? $contents : \$contents, -into => $self);
95             }
96              
97             # ------------------------------------------------------------------------------
98             # save - Save file contents to disk
99             # ------------------------------------------------------------------------------
100              
101             sub save {
102 0     0 1   my $self = shift;
103 0 0         croak "Illegal call to instance method" unless ref($self);
104 0           Hub::writefile($self->{'*path'}, $self->get_content(-asref => 1));
105 0           Hub::frefresh($self->{'*path'}, -force);
106             }
107              
108             sub set_sort_order {
109 0     0 1   my $self = shift;
110 0 0         croak "Illegal call to instance method" unless ref($self);
111 0           my $sort_order = shift;
112 0 0         croak "Provide an array reference" unless isa($sort_order, 'ARRAY');
113 0           $self->{'*tied'}->set_sort_keys(@$sort_order);
114             }
115              
116             # ------------------------------------------------------------------------------
117             1;
118              
119             __END__