File Coverage

blib/lib/Mojo/Home.pm
Criterion Covered Total %
statement 16 16 100.0
branch 6 6 100.0
condition 8 9 88.8
subroutine 4 4 100.0
pod 2 2 100.0
total 36 37 97.3


line stmt bran cond sub pod time code
1             package Mojo::Home;
2 51     51   427 use Mojo::Base 'Mojo::File';
  51         157  
  51         454  
3              
4 51     51   485 use Mojo::Util qw(class_to_path);
  51         142  
  51         20532  
5              
6             sub detect {
7 112     112 1 513 my ($self, $class) = @_;
8              
9             # Environment variable
10 112         262 my $home;
11 112 100 100     717 if ($ENV{MOJO_HOME}) { $home = Mojo::File->new($ENV{MOJO_HOME})->to_array }
  59 100       300  
12              
13             # Location of the application class (Windows mixes backslash and slash)
14             elsif ($class && (my $path = $INC{my $file = class_to_path $class})) {
15 49         257 $home = Mojo::File->new($path)->to_array;
16 49         827 splice @$home, (my @dummy = split(/\//, $file)) * -1;
17 49   100     591 @$home && $home->[-1] eq $_ && pop @$home for qw(lib blib);
      66        
18             }
19              
20 112 100       1230 $$self = Mojo::File->new(@$home)->to_abs->to_string if $home;
21 112         897 return $self;
22             }
23              
24 3     3 1 27 sub rel_file { shift->child(split(/\//, shift)) }
25              
26             1;
27              
28             =encoding utf8
29              
30             =head1 NAME
31              
32             Mojo::Home - Home sweet home
33              
34             =head1 SYNOPSIS
35              
36             use Mojo::Home;
37              
38             # Find and manage the project root directory
39             my $home = Mojo::Home->new;
40             $home->detect;
41             say $home->child('templates', 'layouts', 'default.html.ep');
42             say "$home";
43              
44             =head1 DESCRIPTION
45              
46             L is a container for home directories based on L.
47              
48             =head1 METHODS
49              
50             L inherits all methods from L and implements the following new ones.
51              
52             =head2 detect
53              
54             $home = $home->detect;
55             $home = $home->detect('My::App');
56              
57             Detect home directory from the value of the C environment variable or the location of the application class.
58              
59             =head2 rel_file
60              
61             my $path = $home->rel_file('foo/bar.html');
62              
63             Return a new L object relative to the home directory.
64              
65             =head1 OPERATORS
66              
67             L inherits all overloaded operators from L.
68              
69             =head1 SEE ALSO
70              
71             L, L, L.
72              
73             =cut