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   12928 use Mojo::Base -strict, -signatures;
  14         2895585  
  14         115  
4 14     14   58927 use Mojo::Util qw(monkey_patch);
  14         31  
  14         1309  
5 14     14   7664 use Module::Load qw(load_remote autoload_remote);
  14         13183  
  14         99  
6 14     14   8123 use Class::Method::Modifiers qw(install_modifier);
  14         19436  
  14         898  
7 14     14   12560 use Log::Log4perl qw(:easy);
  14         670378  
  14         90  
8 14     14   10560 use Carp;
  14         34  
  14         898  
9 14     14   8032 use YAML qw(Dump);
  14         101345  
  14         893  
10 14     14   13702 use Path::Tiny qw(path tempdir);
  14         161677  
  14         16662  
11              
12             sub attr {
13 374     374 0 993 my( $self, $attrs, $value, %kv ) = @_;
14              
15 374         728 my $ro = delete $kv{ro};
16 374 100       1300 return Mojo::Base::attr( $self, $attrs, $value, %kv ) unless $ro;
17              
18 116 100       430 $attrs = [$attrs] unless ref($attrs) eq 'ARRAY';
19 116   33     560 my $class = ref $self || $self;
20 116         275 for my $attr (@$attrs) {
21 128         503 my $ro_attr = '__ro__' . $attr;
22 128         443 Mojo::Base::attr( $self, $ro_attr, $value, %kv );
23             my $sub = sub {
24 72287 100   72287   180351 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       154223 carp qq(found rw value for readonly attribute "$attr") if exists $_[0]->{$attr};
26 72286         191285 return $_[0]->$ro_attr;
27 128         5574 };
28 128         380 monkey_patch( $class, $attr, $sub );
29             }
30 116         1968 return;
31             }
32              
33              
34 72     71327   350 sub import ( $class, %args ) {
  72         201  
  72         206  
  72         136  
35 72         204 my $pkg = caller;
36              
37 72         359 require experimental;
38 72         489 experimental->import(qw(postderef lexical_subs));
39              
40 72         5943 load_remote $pkg, 'Path::Tiny', qw(cwd path tempfile tempdir);
41 72         17761 load_remote $pkg, 'YAML::Any', qw(Dump Load DumpFile LoadFile);
42 72         91974 load_remote $pkg, 'Log::Log4perl', ':easy';
43 72         69464 load_remote $pkg, 'List::Util', qw(any max none pairgrep pairmap reduce);
44 72         16900 load_remote $pkg, 'POSIX', qw(strftime);
45              
46 72         136911 autoload_remote $pkg, 'Carp';
47 72         13626 autoload_remote $pkg, 'Try::Tiny';
48              
49 72 100       39163 if ( $args{test} ) {
50 14         77 load_remote $pkg, 'Mojo::Base', qw( -strict -signatures);
51 14         4977 load_remote $pkg, 'Mojo::Loader', qw(data_section);
52 14         540719 load_remote $pkg, 'FindBin', qw($Bin $Script);
53 14         27731 load_remote $pkg, 'Module::Load', qw(load autoload);
54              
55 14         3041 autoload_remote $pkg, 'Test::More';
56 14         1316088 autoload_remote $pkg, 'Test::Exception';
57              
58 14 100 66     61979 if ( $args{temp} and path('t')->is_dir ) {
59 6         727 my $fn = path($0)->basename('.t');
60             my $tmpd = tempdir(
61             DIR => 't',
62 6   50     741 CLEANUP=> $ENV{SPREADSHEET_COMPARE_CLEANUP} // 1,
63             TEMPLATE => "${fn}_XXXX",
64             );
65 6     71255   5061 monkey_patch( $pkg, 'tmpd', sub { $tmpd } );
  0     0   0  
66 6         181 $ENV{SC_TMPD} = $tmpd->absolute;
67             }
68              
69 14         41640 return;
70             }
71              
72             install_modifier(
73             $pkg, 'around', 'new',
74             sub {
75 137     71392   15592 my $orig = shift;
76 137         1311 my $self = $orig->(@_);
77 137         1776 for my $attr ( keys %$self ) {
78 744 100       3306 croak qq(attribute "$attr" is readonly\n) if $self->can( '__ro__' . $attr );
79             }
80 136 100       891 $self->init() if $self->can('init');
81 136         883 return $self;
82             },
83 58         633 );
84              
85 58     71629   22769 monkey_patch( $pkg, 'has', sub { attr( $pkg, @_ ) } );
  374     374   3028  
        374      
        374      
        262      
        262      
86              
87             monkey_patch(
88             $pkg,
89             'get_log_settings',
90             sub {
91 66     118   543 my $logger = Log::Log4perl->get_logger('');
        118      
        66      
        66      
        62      
        62      
92 66         1451 return $logger->is_trace, $logger->is_debug;
93             }
94 58         1678 );
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         1087 );
109              
110 58         3236 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