File Coverage

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


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 2     2   10 use Moose;
  2         4  
  2         15  
10 2     2   10441 use namespace::autoclean;
  2         2  
  2         20  
11 2     2   141 use version;
  2         3  
  2         15  
12 2     2   120 use Carp;
  2         2  
  2         143  
13 2     2   10 use Scalar::Util;
  2         2  
  2         63  
14 2     2   8 use List::Util;
  2         4  
  2         102  
15 2     2   10 use Data::Dumper qw/Dumper/;
  2         3  
  2         81  
16 2     2   8 use English qw/ -no_match_vars /;
  2         3  
  2         22  
17 2     2   721 use Moose::Util::TypeConstraints;
  2         3  
  2         20  
18 2     2   3002 use Path::Tiny;
  2         3  
  2         95  
19 2     2   8 use Data::Context::Util qw/do_require/;
  2         4  
  2         830  
20              
21             our $VERSION = version->new('0.1.10');
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 1     1 1 3 my ($self, @path) = @_;
76              
77 1         2 my $default;
78             my $default_type;
79              
80 1         3 for my $search ( @{ $self->path } ) {
  1         27  
81 1         2 for my $type ( @{ $self->suffix_order } ) {
  1         28  
82             my $config = path(
83             $search,
84             @path[0 .. @path-2],
85             $path[-1] . $self->suffixes->{$type}->{suffix}
86 1         35 );
87 1 50       36 if ( -e $config ) {
88 1         50 my $module = $self->suffixes->{$type}->{module};
89 1         4 do_require($module);
90 1         138 return $module->new(
91             file => $config,
92             type => $type,
93             );
94             }
95 0 0         next if $default;
96              
97             $config = path(
98             $search,
99             @path[0 .. @path - 2],
100             $self->default . $self->suffixes->{$type}->{suffix}
101 0           );
102 0 0         if ( -e $config ) {
103 0           $default = $config;
104 0           $default_type = $type;
105             }
106             }
107             }
108              
109 0 0         if ($default) {
110 0           my $module = $self->suffixes->{$default_type}->{module};
111 0           do_require($module);
112 0           return $module->new(
113             file => $default,
114             type => $default_type,
115             );
116             }
117              
118 0           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.10
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