« lib/Mojolicious/Plugin/Fondation/Action/Controllers.pm
↑ Summary
lib/Mojolicious/Plugin/Fondation/Action/Templates.pm »
| 1 | package Mojolicious::Plugin::Fondation::Utils; | ||||||||||||||||||||||||||||||||
| 2 | 17 | $Mojolicious::Plugin::Fondation::Utils::VERSION = '0.08'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Utility functions: config merge, name resolution, share directory helpers | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use Mojo::Base -strict, -signatures; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use File::ShareDir 'dist_dir'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use Hash::Merge (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use Mojo::File; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 17 | our @EXPORT_OK = qw( long_name short_name share_relative find_share_dir merge); | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 378 | ▶ | sub long_name { | ||||||||||||||||||||||||||||||
|
Subroutine
long_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 14 | 378 | my ($name) = @_; | |||||||||||||||||||||||||||||||
| 15 | 378 | ▶ | return $name if $name =~ /^Mojolicious::Plugin::/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 16 | 353 | return "Mojolicious::Plugin::$name"; | |||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 944 | ▶ | sub short_name { | ||||||||||||||||||||||||||||||
|
Subroutine
short_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 20 | 944 | my ($name) = @_; | |||||||||||||||||||||||||||||||
| 21 | 944 | $name =~ s/^Mojolicious::Plugin:://; | |||||||||||||||||||||||||||||||
| 22 | 944 | return $name; | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 177 | ▶ | sub share_relative { | ||||||||||||||||||||||||||||||
|
Subroutine
share_relative: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 26 | 177 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 27 | # Normalize to forward slashes (cross-platform: Windows uses backslashes) | ||||||||||||||||||||||||||||||||
| 28 | 177 | (my $normalized = $path) =~ s{\\}{/}g; | |||||||||||||||||||||||||||||||
| 29 | 177 | my $pos = rindex($normalized, '/share/'); | |||||||||||||||||||||||||||||||
| 30 | 177 | ▶ | return $path if $pos == -1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 176 | return substr($normalized, $pos + 1); | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 165 | ▶ | sub find_share_dir ($class_name, $override = undef) { | ||||||||||||||||||||||||||||||
|
Subroutine
find_share_dir: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 35 | 165 | ▶ | return Mojo::File->new($override) if $override; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 119 | my $dist = $class_name; | |||||||||||||||||||||||||||||||
| 38 | 119 | $dist =~ s/::/-/g; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | # 1) Try File::ShareDir (installed modules) | ||||||||||||||||||||||||||||||||
| 41 | 119 | my $dir = eval { Mojo::File->new(dist_dir($dist)) }; | |||||||||||||||||||||||||||||||
| 42 | 119 | ▶ | return $dir if $dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # 2) Fallback: derive share dir from the class's location in %INC | ||||||||||||||||||||||||||||||||
| 45 | # This handles test/dev environments where the module is not yet installed. | ||||||||||||||||||||||||||||||||
| 46 | 52 | (my $rel_path = "$class_name.pm") =~ s{::}{/}g; | |||||||||||||||||||||||||||||||
| 47 | 52 | ▶ | if (my $inc_path = $INC{$rel_path}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 52 | my $pm = Mojo::File->new($inc_path); | |||||||||||||||||||||||||||||||
| 49 | 52 | my $root = $pm; | |||||||||||||||||||||||||||||||
| 50 | 52 | my $levels = scalar(split /::/, $class_name) + 1; # +1 for lib/ | |||||||||||||||||||||||||||||||
| 51 | 52 | $root = $root->dirname for 1 .. $levels; | |||||||||||||||||||||||||||||||
| 52 | 52 | my $share = $root->child('share'); | |||||||||||||||||||||||||||||||
| 53 | 52 | ▶ | return $share if -d $share; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | # 3) Last resort | ||||||||||||||||||||||||||||||||
| 57 | 0 | return Mojo::File->curfile->sibling('share'); | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # 3-level merging with priority (uses Hash::Merge for recursive merge): | ||||||||||||||||||||||||||||||||
| 61 | # 1. direct config (in dependencies list) -- highest priority | ||||||||||||||||||||||||||||||||
| 62 | # 2. config in the app file (myapp.conf) | ||||||||||||||||||||||||||||||||
| 63 | # 3. plugin defaults (via fondation_meta) -- lowest priority | ||||||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||||||
| 65 | # Hash::Merge::merge() is LEFT_PRECEDENT, so we reverse the call order: | ||||||||||||||||||||||||||||||||
| 66 | # merge(direct, merge(app_conf, defaults)) | ||||||||||||||||||||||||||||||||
| 67 | # -> direct wins over app_conf wins over defaults for scalars | ||||||||||||||||||||||||||||||||
| 68 | # -> arrays are concatenated, hashes are merged recursively | ||||||||||||||||||||||||||||||||
| 69 | 285 | ▶ | sub merge ($direct = {}, $app_conf = {}, $plugin_defaults = {}) { | ||||||||||||||||||||||||||||||
|
Subroutine
merge: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 70 | 285 | ▶ | return Hash::Merge::merge( | ||||||||||||||||||||||||||||||
|
Condition100%6/6
$direct // {}
$app_conf // {}
$plugin_defaults // {}
MC/DC100%1/1
$direct // {}
$direct
MC/DC100%1/1
$app_conf // {}
$app_conf
MC/DC100%1/1
$plugin_defaults // {}
$plugin_defaults
Truth table100%2/2
A
A= $direct
Truth table100%2/2
A
A= $app_conf
Truth table100%2/2
A
A= $plugin_defaults
| |||||||||||||||||||||||||||||||||
| 71 | $direct // {}, | ||||||||||||||||||||||||||||||||
| 72 | Hash::Merge::merge($app_conf // {}, $plugin_defaults // {}) | ||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 17 | 1; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||||||