File Coverage

blib/lib/Data/Context/Loader/File.pm
Criterion Covered Total %
statement 36 42 85.7
branch 1 2 50.0
condition n/a
subroutine 11 13 84.6
pod 3 3 100.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             package Data::Context::Loader::File;
2              
3             # Created on: 2013-10-27 20:02:37
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   1640 use Moose;
  2         2  
  2         16  
10 2     2   10197 use namespace::autoclean;
  2         3  
  2         22  
11 2     2   142 use version;
  2         4  
  2         14  
12 2     2   120 use Carp;
  2         5  
  2         146  
13 2     2   11 use Scalar::Util;
  2         4  
  2         95  
14 2     2   11 use List::Util;
  2         2  
  2         121  
15 2     2   15 use Data::Dumper qw/Dumper/;
  2         2  
  2         91  
16 2     2   8 use English qw/ -no_match_vars /;
  2         2  
  2         17  
17 2     2   737 use Data::Context::Util qw/do_require/;
  2         3  
  2         609  
18              
19             our $VERSION = version->new('0.1.10');
20              
21             extends 'Data::Context::Loader';
22              
23             has file => (
24             is => 'rw',
25             isa => 'Path::Tiny',
26             required => 1,
27             );
28             has type => (
29             is => 'ro',
30             isa => 'Str',
31             required => 1,
32             );
33             has module => (
34             is => 'ro',
35             isa => 'Str',
36             required => 1,
37             );
38             has stats => (
39             is => 'rw',
40             lazy_build => 1,
41             builder => '_stats',
42             init_arg => undef,
43             );
44              
45             sub _stats {
46 4     4   7 my ($self) = @_;
47 4         182 my $stat = $self->file->stat;
48 4 50       7540 if ( !-f $self->file ) {
49 0         0 my $msg = 'Cannot find the file "' . $self->file . '"';
50 0         0 $self->log->error($msg);
51 0         0 confess $msg;
52             }
53              
54             return {
55 4         180 size => $stat->size,
56             modified => $stat->mtime,
57             };
58             }
59              
60             sub changed {
61 0     0 1 0 my ($self) = @_;
62              
63             # check if we already have the raw data and if so that it is current
64 0         0 return -s $self->file != $self->stats->{size};
65             }
66              
67 0     0 1 0 sub loader { confess "Not implemented!" }
68              
69             sub load {
70 4     4 1 8 my ($self) = @_;
71              
72 4         133 do_require($self->module);
73              
74             # get the raw data
75 4         232 my $raw = $self->loader(scalar $self->file->slurp);
76              
77             # Reset the file stats on load.
78 4         81694 $self->stats($self->_stats);
79              
80 4         15 return $raw;
81             }
82              
83             __PACKAGE__->meta->make_immutable;
84              
85             1;
86              
87             __END__
88              
89             =head1 NAME
90              
91             Data::Context::Loader::File - Loads a config file from disk
92              
93             =head1 VERSION
94              
95             This documentation refers to Data::Context::Loader::File version 0.1.10
96              
97             =head1 SYNOPSIS
98              
99             use Data::Context::Loader::File;
100              
101             # Load a file of relaxed json type
102             my $file = Data::Context::Loader::File->new(
103             file => '/path/config.dc.js',
104             type => 'js',
105             );
106              
107             =head1 DESCRIPTION
108              
109             Loads files found by L<Data::Context::Finder::File> and performs checks to
110             see if the file has changed on disk.
111              
112             =head1 SUBROUTINES/METHODS
113              
114             =head2 C<changed ()>
115              
116             Checks if the file has changed on disk
117              
118             =head2 C<loader ($str)>
119              
120             Method for passing file data (should be implemented by child class.
121              
122             =head2 C<load ()>
123              
124             Loads the file passing it with the appropriate parser.
125              
126             =head1 DIAGNOSTICS
127              
128             A list of every error and warning message that the module can generate (even
129             the ones that will "never happen"), with a full explanation of each problem,
130             one or more likely causes, and any suggested remedies.
131              
132             =head1 CONFIGURATION AND ENVIRONMENT
133              
134             A full explanation of any configuration system(s) used by the module, including
135             the names and locations of any configuration files, and the meaning of any
136             environment variables or properties that can be set. These descriptions must
137             also include details of any configuration language used.
138              
139             =head1 DEPENDENCIES
140              
141             A list of all of the other modules that this module relies upon, including any
142             restrictions on versions, and an indication of whether these required modules
143             are part of the standard Perl distribution, part of the module's distribution,
144             or must be installed separately.
145              
146             =head1 INCOMPATIBILITIES
147              
148             A list of any modules that this module cannot be used in conjunction with.
149             This may be due to name conflicts in the interface, or competition for system
150             or program resources, or due to internal limitations of Perl (for example, many
151             modules that use source code filters are mutually incompatible).
152              
153             =head1 BUGS AND LIMITATIONS
154              
155             A list of known problems with the module, together with some indication of
156             whether they are likely to be fixed in an upcoming release.
157              
158             Also, a list of restrictions on the features the module does provide: data types
159             that cannot be handled, performance issues and the circumstances in which they
160             may arise, practical limitations on the size of data sets, special cases that
161             are not (yet) handled, etc.
162              
163             The initial template usually just has:
164              
165             There are no known bugs in this module.
166              
167             Please report problems to Ivan Wills (ivan.wills@gmail.com).
168              
169             Patches are welcome.
170              
171             =head1 AUTHOR
172              
173             Ivan Wills - (ivan.wills@gmail.com)
174             <Author name(s)> (<contact address>)
175              
176             =head1 LICENSE AND COPYRIGHT
177              
178             Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
179             All rights reserved.
180              
181             This module is free software; you can redistribute it and/or modify it under
182             the same terms as Perl itself. See L<perlartistic>. This program is
183             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
184             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
185             PARTICULAR PURPOSE.
186              
187             =cut