| 1 | package Catmandu::Fix::include; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Catmandu; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Catmandu::Fix; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use File::Spec qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Cwd qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | with 'Catmandu::Fix::Inlineable'; | |||||||||
| 16 | |||||||||||
| 17 | 1 | has path => (fix_arg => 1); | |||||||||
| 18 | has _path => ( | ||||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | lazy => 1, | ||||||||||
| 21 | builder => sub { | ||||||||||
| 22 | |||||||||||
| 23 | 3 | ▶ | my $self = $_[0]; | ||||||||
|
Subroutine
_build__path: called CC=5 SCAR=16.6
| |||||||||||
| 24 | |||||||||||
| 25 | 3 | my $path = $self->path(); | |||||||||
| 26 | 3 | my $real_path; | |||||||||
| 27 | 3 | my $load_paths = Catmandu->_env->load_paths; | |||||||||
| 28 | |||||||||||
| 29 | 3 | ▶ | if (File::Spec->file_name_is_absolute($path)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | $real_path = $path; | |||||||||
| 31 | } | ||||||||||
| 32 | else { | ||||||||||
| 33 | 3 | for my $p (@$load_paths) { | |||||||||
| 34 | 3 | my $n = File::Spec->catfile($p, $path); | |||||||||
| 35 | 3 | ▶ | if (-r $n) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 3 | $real_path = Cwd::realpath($n); | |||||||||
| 37 | 3 | last; | |||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 3 | ▶ | die("unable to find $path in load_path of Catmandu (load_path: " | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | . join(',', @$load_paths) . ")") | ||||||||||
| 45 | unless defined $real_path; | ||||||||||
| 46 | 3 | $real_path; | |||||||||
| 47 | } | ||||||||||
| 48 | 1 | ); | |||||||||
| 49 | |||||||||||
| 50 | has _fixer => ( | ||||||||||
| 51 | is => 'ro', | ||||||||||
| 52 | lazy => 1, | ||||||||||
| 53 | builder => sub { | ||||||||||
| 54 | 3 | ▶ | Catmandu::Fix->new(fixes => [$_[0]->_path()]); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | } | ||||||||||
| 56 | 1 | ); | |||||||||
| 57 | |||||||||||
| 58 | sub fix { | ||||||||||
| 59 | 3 | ▶ | my ($self, $data) = @_; | ||||||||
|
Subroutine
fix: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 60 | 3 | $self->_fixer()->fix($data); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | 1; | |||||||||
| 64 | |||||||||||
| 65 | __END__ | ||||||||||
| 66 | |||||||||||
| 67 | =pod | ||||||||||
| 68 | |||||||||||
| 69 | =head1 NAME | ||||||||||
| 70 | |||||||||||
| 71 | Catmandu::Fix::include - include fixes from another file | ||||||||||
| 72 | |||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||
| 74 | |||||||||||
| 75 | include('/path/to/myfixes.txt') | ||||||||||
| 76 | |||||||||||
| 77 | =head1 NOTES | ||||||||||
| 78 | |||||||||||
| 79 | =over 4 | ||||||||||
| 80 | |||||||||||
| 81 | =item path is relative to a Catmandu load path | ||||||||||
| 82 | |||||||||||
| 83 | #1. a catmandu load path is a directory where a catmandu configuration file can be found | ||||||||||
| 84 | #2. as there are multiple load paths, it will loop through all the path, and include the first file it can find | ||||||||||
| 85 | #3. in catmandu, the default_load_path is either | ||||||||||
| 86 | # 3.1. the directory of the running script | ||||||||||
| 87 | # 3.2. the parent directory of the running script if the directory is 'bin' | ||||||||||
| 88 | |||||||||||
| 89 | #use default load_path | ||||||||||
| 90 | #called from script "/opt/catmandu-project/fix.pl" | ||||||||||
| 91 | #default_load_path: /opt/catmandu-project | ||||||||||
| 92 | #file must be located at "/opt/catmandu-project/fixes/myfixes.txt" | ||||||||||
| 93 | Catmandu->fixer("include('fixes/myfixes.txt')"); | ||||||||||
| 94 | |||||||||||
| 95 | #use default load_path | ||||||||||
| 96 | #called from script "/opt/catmandu-project/bin/fix.pl" | ||||||||||
| 97 | #default_load_path: /opt/catmandu-project (notice the absence of 'bin') | ||||||||||
| 98 | #file must be located at "/opt/catmandu-project/fixes/myfixes.txt" | ||||||||||
| 99 | Catmandu->fixer("include('fixes/myfixes.txt')"); | ||||||||||
| 100 | |||||||||||
| 101 | #load fixes, located at /opt/catmandu-project/fixes/myfixes.txt | ||||||||||
| 102 | Catmandu->load("/opt/catmandu-project"); | ||||||||||
| 103 | Catmandu->fixer("include('fixes/myfixes.txt')"); | ||||||||||
| 104 | |||||||||||
| 105 | #look for 'fixes/myfixes.txt' in /opt/catmandu-project2, and if that fails in /opt/catmandu-project-1 | ||||||||||
| 106 | Catmandu->load("/opt/catmandu-project2","/opt/catmandu-project-1"); | ||||||||||
| 107 | Catmandu->fixer("include('fixes/myfixes.txt')"); | ||||||||||
| 108 | |||||||||||
| 109 | #if "/opt/catmandu-project/fixes/myfixes2.txt" does not exists, the fix will fail | ||||||||||
| 110 | Catmandu->load("/opt/catmandu-project"); | ||||||||||
| 111 | Catmandu->fixer("include('fixes/myfixes2.txt)"); | ||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | =item circular references are not detected | ||||||||||
| 115 | |||||||||||
| 116 | =item if the 'include' is enclosed within an if-statement, the fixes are inserted in the control structure, but only executed if the if-statement evaluates to 'true'. | ||||||||||
| 117 | |||||||||||
| 118 | #the fixes in the file 'add_address_fields.txt' are only executed when field 'name' has content, | ||||||||||
| 119 | #but, the fixes are included in the control structure. | ||||||||||
| 120 | if all_match('name','.+') | ||||||||||
| 121 | include('add_address_fields.txt') | ||||||||||
| 122 | end | ||||||||||
| 123 | |||||||||||
| 124 | =back | ||||||||||
| 125 | |||||||||||
| 126 | =head1 SEE ALSO | ||||||||||
| 127 | |||||||||||
| 128 | L<Catmandu::Fix> | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||