File Coverage

blib/lib/Spreadsheet/Compare/Common.pm
Criterion Covered Total %
statement 79 86 91.8
branch 15 18 83.3
condition 4 8 50.0
subroutine 44 48 91.6
pod 0 1 0.0
total 142 161 88.2


line stmt bran cond sub pod time code
1             package Spreadsheet::Compare::Common;
2              
3 14     14   13013 use Mojo::Base -strict, -signatures;
  14         2908838  
  14         177  
4 14     14   58936 use Mojo::Util qw(monkey_patch);
  14         35  
  14         1790  
5 14     14   7801 use Module::Load qw(load_remote autoload_remote);
  14         12839  
  14         201  
6 14     14   8050 use Class::Method::Modifiers qw(install_modifier);
  14         19338  
  14         1325  
7 14     14   12517 use Log::Log4perl qw(:easy);
  14         647078  
  14         90  
8 14     14   10782 use Carp;
  14         33  
  14         1025  
9 14     14   8434 use YAML qw(Dump);
  14         102627  
  14         929  
10 14     14   13471 use Path::Tiny qw(path tempdir);
  14         158513  
  14         16386  
11              
12             sub attr {
13 374     374 0 1031 my( $self, $attrs, $value, %kv ) = @_;
14              
15 374         728 my $ro = delete $kv{ro};
16 374 100       1260 return Mojo::Base::attr( $self, $attrs, $value, %kv ) unless $ro;
17              
18 116 100       396 $attrs = [$attrs] unless ref($attrs) eq 'ARRAY';
19 116   33     502 my $class = ref $self || $self;
20 116         245 for my $attr (@$attrs) {
21 128         485 my $ro_attr = '__ro__' . $attr;
22 128         409 Mojo::Base::attr( $self, $ro_attr, $value, %kv );
23             my $sub = sub {
24 72287 100   72287   177506 croak qq(attribute "$attr" is readonly) if @_ > 1;
        72263      
        72263      
        72263      
        72287      
        72263      
        72263      
        72263      
        72287      
        72275      
        143506      
        214797      
        143518      
        72263      
        72263      
        72263      
        71745      
25 72286 50       162334 carp qq(found rw value for readonly attribute "$attr") if exists $_[0]->{$attr};
26 72286         181751 return $_[0]->$ro_attr;
27 128         5237 };
28 128         364 monkey_patch( $class, $attr, $sub );
29             }
30 116         1858 return;
31             }
32              
33              
34 72     71327   337 sub import ( $class, %args ) {
  72         215  
  72         197  
  72         144  
35 72         205 my $pkg = caller;
36              
37 72         380 require experimental;
38 72         459 experimental->import(qw(postderef lexical_subs));
39              
40 72         5945 load_remote $pkg, 'Path::Tiny', qw(cwd path tempfile tempdir);
41 72         18265 load_remote $pkg, 'YAML::Any', qw(Dump Load DumpFile LoadFile);
42 72         91350 load_remote $pkg, 'Log::Log4perl', ':easy';
43 72         68181 load_remote $pkg, 'List::Util', qw(any max none pairgrep pairmap reduce);
44 72         16819 load_remote $pkg, 'POSIX', qw(strftime);
45              
46 72         136449 autoload_remote $pkg, 'Carp';
47 72         26843 autoload_remote $pkg, 'Try::Tiny';
48              
49 72 100       39863 if ( $args{test} ) {
50 14         71 load_remote $pkg, 'Mojo::Base', qw( -strict -signatures);
51 14         5111 load_remote $pkg, 'Mojo::Loader', qw(data_section);
52 14         535990 load_remote $pkg, 'FindBin', qw($Bin $Script);
53 14         27367 load_remote $pkg, 'Module::Load', qw(load autoload);
54              
55 14         3045 autoload_remote $pkg, 'Test::More';
56 14         1297173 autoload_remote $pkg, 'Test::Exception';
57              
58 14 100 66     61754 if ( $args{temp} and path('t')->is_dir ) {
59 6         779 my $fn = path($0)->basename('.t');
60             my $tmpd = tempdir(
61             DIR => 't',
62 6   50     763 CLEANUP=> $ENV{SPREADSHEET_COMPARE_CLEANUP} // 1,
63             TEMPLATE => "${fn}_XXXX",
64             );
65 6     71255   4838 monkey_patch( $pkg, 'tmpd', sub { $tmpd } );
  0     0   0  
66 6         185 $ENV{SC_TMPD} = $tmpd->absolute;
67             }
68              
69 14         40971 return;
70             }
71              
72             install_modifier(
73             $pkg, 'around', 'new',
74             sub {
75 137     71392   14999 my $orig = shift;
76 137         1120 my $self = $orig->(@_);
77 137         1891 for my $attr ( keys %$self ) {
78 744 100       3212 croak qq(attribute "$attr" is readonly\n) if $self->can( '__ro__' . $attr );
79             }
80 136 100       853 $self->init() if $self->can('init');
81 136         716 return $self;
82             },
83 58         641 );
84              
85 58     71629   22702 monkey_patch( $pkg, 'has', sub { attr( $pkg, @_ ) } );
  374     374   2940  
        374      
        374      
        262      
        262      
86              
87             monkey_patch(
88             $pkg,
89             'get_log_settings',
90             sub {
91 66     118   591 my $logger = Log::Log4perl->get_logger('');
        118      
        66      
        66      
        62      
        62      
92 66         1488 return $logger->is_trace, $logger->is_debug;
93             }
94 58         1874 );
95              
96             monkey_patch(
97             $pkg,
98             'call_stack',
99             sub {
100 0     22   0 my $trace = '';
        22      
        0      
        0      
        71255      
        0      
101 0         0 for my $lev ( 0 .. 9 ) {
102 0         0 my( $package, $file, $line ) = caller($lev);
103 0 0       0 next unless $line;
104 0         0 $trace .= "$package, at $file line $line\n";
105             }
106 0         0 return $trace;
107             }
108 58         1072 );
109              
110 58         3242 return;
111             }
112              
113              
114             1;
115              
116              
117             =head1 NAME
118              
119             Spreadsheet::Compare::Common - convenient imports for Spreadsheet::Compare Modules
120              
121             =head1 DESCRIPTION
122              
123             This module injects various Modules and functions into the namespace of the caller:
124              
125             =over 4
126              
127             =item * L<Carp|https://metacpan.org/pod/Carp>
128              
129             =item * L<Try::Tiny|https://metacpan.org/pod/Try::Tiny>
130              
131             =item * C<cwd>, C<path>, C<tempfile>, C<tempdir> from L<Path::Tiny|https://metacpan.org/pod/Path::Tiny>
132              
133             =item * C<Dump>, C<Load>, C<DumpFile>, C<LoadFile> from L<YAML|https://metacpan.org/pod/YAML>
134              
135             =item * L<Log::Log4perl|https://metacpan.org/pod/Log::Log4perl> in easy mode
136              
137             =item * C<any>, C<max>, C<none>, C<pairgrep>, C<pairmap>, C<reduce> from L<List::Util|https://metacpan.org/pod/List::Util>
138              
139             =item * C<strftime> from L<POSIX|https://metacpan.org/pod/POSIX>
140              
141             =back
142              
143             In addition it enables the postderef feature and extends the C<has> function of
144             L<Mojo::Base|https://metacpan.org/pod/Mojo::Base>
145             with an C<ro> option to specify that the attribute is readonly, e.g.:
146              
147             use Mojo::Base -base, -signatures;
148             use Spreadsheet::Compare::Common;
149              
150             has thing => 42, ro => 1;
151              
152             If the module is loaded with the "test" option set to a true value,
153              
154             use Spreadsheet::Compare::Common test => 1;
155              
156             ... use test functions
157              
158             it will additionally inject the following:
159              
160             =over 4
161              
162             =item * L<Mojo::Base|https://metacpan.org/pod/Mojo::Base> with C<-strict> and C<-signatures>
163              
164             =item * L<Test::More|https://metacpan.org/pod/Test::More>
165              
166             =item * L<Test::Exception|https://metacpan.org/pod/Test::Exception>
167              
168             =item * C<data_section> from L<Mojo::Loader|https://metacpan.org/pod/Mojo::Loader>
169              
170             =item * C<$Bin> and C<$Script> from L<FindBin|https://metacpan.org/pod/FindBin>
171              
172             =back
173              
174             The test option can be extended with a "temp" option. This will create a temporary directory
175             in the "t" directory starting with the test file name. (e.g. t/01_base_V3CQ for t/01_base.t).
176             By default it will be cleaned up afterwards. To keep the directory set the environment variable
177             C<SPREADSHEET_COMPARE_CLEANUP> to a true value. The absolute name of the temp directory will
178             be available in the environment variable C<SC_TMPD>
179              
180             use Spreadsheet::Compare::Common test => 1, temp => 1;
181              
182             ... save temp data to $ENV{SC_TMPD}
183              
184              
185             =cut