File Coverage

blib/lib/Data/Context/Finder/File.pm
Criterion Covered Total %
statement 54 54 100.0
branch 8 8 100.0
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 75 75 100.0


line stmt bran cond sub pod time code
1             package Data::Context::Finder::File;
2              
3             # Created on: 2013-10-26 20:02:08
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 6     6   30 use Moose;
  6         10  
  6         67  
10 6     6   41555 use namespace::autoclean;
  6         56  
  6         61  
11 6     6   543 use version;
  6         11  
  6         52  
12 6     6   439 use Carp;
  6         12  
  6         432  
13 6     6   34 use Scalar::Util;
  6         10  
  6         254  
14 6     6   36 use List::Util;
  6         9  
  6         379  
15 6     6   32 use Data::Dumper qw/Dumper/;
  6         12  
  6         309  
16 6     6   36 use English qw/ -no_match_vars /;
  6         10  
  6         57  
17 6     6   2569 use Moose::Util::TypeConstraints;
  6         9  
  6         68  
18 6     6   14008 use Path::Class;
  6         12  
  6         441  
19 6     6   41 use Data::Context::Util qw/do_require/;
  6         11  
  6         3370  
20              
21             our $VERSION = version->new('0.1.8');
22              
23             extends 'Data::Context::Finder';
24              
25             subtype 'ArrayRefStr'
26             => as 'ArrayRef[Str]';
27              
28             coerce 'ArrayRefStr'
29             => from 'Str'
30             => via { [$_] };
31              
32             has path => (
33             is => 'rw',
34             isa => 'ArrayRefStr',
35             coerce => 1,
36             required => 1,
37             );
38             has suffixes => (
39             is => 'rw',
40             isa => 'HashRef[HashRef]',
41             default => sub {
42             return {
43             json => {
44             suffix => '.dc.json',
45             module => 'Data::Context::Loader::File::JSON',
46             },
47             js => {
48             suffix => '.dc.js',
49             module => 'Data::Context::Loader::File::JS',
50             },
51             yaml => {
52             suffix => '.dc.yml',
53             module => 'Data::Context::Loader::File::YAML',
54             },
55             xml => {
56             suffix => '.dc.xml',
57             module => 'Data::Context::Loader::File::XML',
58             },
59             };
60             },
61             );
62             has suffix_order => (
63             is => 'rw',
64             isa => 'ArrayRefStr',
65             coerce => 1,
66             default => sub { [qw/js json yaml xml/] },
67             );
68             has default => (
69             is => 'rw',
70             isa => 'Str',
71             default => '_default',
72             );
73              
74             sub find {
75 41     41 1 99 my ($self, @path) = @_;
76              
77 41         58 my $default;
78             my $default_type;
79              
80 41         83 for my $search ( @{ $self->path } ) {
  41         2055  
81 41         51 for my $type ( @{ $self->suffix_order } ) {
  41         1459  
82 142         9919 my $config = file(
83             $search,
84             @path[0 .. @path-2],
85             $path[-1] . $self->suffixes->{$type}->{suffix}
86             );
87 142 100       12412 if ( -e $config ) {
88 13         1287 my $module = $self->suffixes->{$type}->{module};
89 13         67 do_require($module);
90 13         644 return $module->new(
91             file => $config,
92             type => $type,
93             );
94             }
95 129 100       5675 next if $default;
96              
97 123         5476 $config = file(
98             $search,
99             @path[0 .. @path - 2],
100             $self->default . $self->suffixes->{$type}->{suffix}
101             );
102 123 100       11665 if ( -e $config ) {
103 8         418 $default = $config;
104 8         43 $default_type = $type;
105             }
106             }
107             }
108              
109 28 100       938 if ($default) {
110 8         356 my $module = $self->suffixes->{$default_type}->{module};
111 8         44 do_require($module);
112 8         401 return $module->new(
113             file => $default,
114             type => $default_type,
115             );
116             }
117              
118 20         84 return;
119             }
120              
121             __PACKAGE__->meta->make_immutable;
122              
123             1;
124              
125             __END__
126              
127             =head1 NAME
128              
129             Data::Context::Finder::File - <One-line description of module's purpose>
130              
131             =head1 VERSION
132              
133             This documentation refers to Data::Context::Finder::File version 0.1.8
134              
135              
136             =head1 SYNOPSIS
137              
138             use Data::Context::Finder::File;
139              
140             # Brief but working code example(s) here showing the most common usage(s)
141             # This section will be as far as many users bother reading, so make it as
142             # educational and exemplary as possible.
143              
144              
145             =head1 DESCRIPTION
146              
147             =head1 SUBROUTINES/METHODS
148              
149             =head2 C<find ( @path )>
150              
151             Finds a config file matching C<@path> or C<@path[ 0 .. @path - 2 ]/_default>
152             if it exists and returns a L<Data::Context::Loader::File> object.
153              
154             =head1 DIAGNOSTICS
155              
156             =head1 CONFIGURATION AND ENVIRONMENT
157              
158             =head1 DEPENDENCIES
159              
160             =head1 INCOMPATIBILITIES
161              
162             =head1 BUGS AND LIMITATIONS
163              
164             There are no known bugs in this module.
165              
166             Please report problems to Ivan Wills (ivan.wills@gmail.com).
167              
168             Patches are welcome.
169              
170             =head1 AUTHOR
171              
172             Ivan Wills - (ivan.wills@gmail.com)
173              
174             =head1 LICENSE AND COPYRIGHT
175              
176             Copyright (c) 2013 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
177             All rights reserved.
178              
179             This module is free software; you can redistribute it and/or modify it under
180             the same terms as Perl itself. See L<perlartistic>. This program is
181             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
182             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
183             PARTICULAR PURPOSE.
184              
185             =cut