File Coverage

blib/lib/App/Tables.pm
Criterion Covered Total %
statement 21 72 29.1
branch 0 14 0.0
condition 0 10 0.0
subroutine 7 21 33.3
pod n/a
total 28 117 23.9


line stmt bran cond sub pod time code
1             package App::Tables::Provider::xls;
2 2     2   62379 use Modern::Perl;
  2         4  
  2         14  
3             require App::Tables::Excel;
4            
5             =head1 TODO
6              
7             everything works fine now. need comments, tests and documentation.
8              
9             =head1 Contribute
10              
11             yes, please
12              
13             https://github.com/eiro/app-tables
14              
15             =cut
16              
17             # qw< tables2xls xls2tables >;
18             # qw< tables2xls xls2tables >;
19             # use Data::Table::Excel qw< tables2xls xls2tables >;
20             # use Modern::Perl;
21              
22             sub new {
23 0     0     my $self = pop;
24             state $io =
25             { reader => sub {
26 0     0     App::Tables::Excel::tables_from_file
27             ( (shift)
28             , qw( format 2003 headers_are built ))
29             }
30 0           , writer => Data::Table::Excel->can('tables2xls') };
31 0           bless { %$self, %$io } , __PACKAGE__;
32             }
33              
34             sub read {
35 0     0     my ( $self ) = @_;
36 0           my ( $data, $headers ) = $self->{reader}( $$self{base} );
37 0           my %whole;
38 0           @whole{@$headers} = @$data;
39 0           \%whole;
40             }
41              
42             sub write {
43 0     0     my ( $self, $data ) = @_;
44 0           my @headers = keys %$data;
45 0           my @data = map { $$data{$_} } @headers;
  0            
46 0           $self->{writer}( $$self{base}, \@data, \@headers );
47             }
48              
49             package App::Tables::Provider::xlsx;
50             require Data::Table::Excel;
51 2     2   871 use Modern::Perl;
  2         4  
  2         13  
52             our @ISA = 'App::Tables::Provider::xls';
53             # use parent 'App::Tables::Provider::xls';
54              
55             sub new {
56 0     0     my $self = pop;
57             state $io =
58             { reader => sub {
59 0     0     App::Tables::Excel::tables_from_file
60             ( (shift)
61             , qw( format 2007 headers_are built ))
62             }
63 0           , writer => Data::Table::Excel->can('tables2xlsx') };
64 0           bless { %$self, %$io } , __PACKAGE__;
65             }
66              
67             package App::Tables::Provider::dir;
68 2     2   430 use Modern::Perl;
  2         7  
  2         7  
69 2     2   2070 use IO::All;
  2         25803  
  2         19  
70              
71 0     0     sub new { bless pop, __PACKAGE__ }
72              
73             sub read {
74 0     0     my ( $self ) = @_;
75 0           my @headers;
76 0           my @data = map {
77 0           push @headers, m{ ([^/]+) $ }x; # basename
78 0           Data::Table::fromTSV $_
79             } glob "$$self{base}/*";
80 0           my %whole;
81 0           @whole{ @headers } = @data;
82 0           \%whole
83             }
84              
85             sub write {
86 0     0     my ( $self, $data ) = @_;
87 0 0         map { -d $_ or io($_)->mkpath } $$self{base};
  0            
88 0           while ( my ( $name, $sheet) = each $data ) {
89 0           io( "$$self{base}/$name" ) < $sheet->tsv(0);
90             }
91             }
92              
93             package App::Tables;
94             # ABSTRACT: manipulation of tables from any sources
95             our $VERSION = '0.4';
96              
97 2     2   1326 use Modern::Perl;
  2         4  
  2         19  
98 2     2   262 use Exporter 'import';
  2         4  
  2         463  
99             our @EXPORT_OK = qw<
100             init
101             >;
102              
103             our %EXPORT_TAGS =
104             ( all=> \@EXPORT_OK );
105              
106             # possible types are xls, xslx and /
107             # could be some urlized dsn+query stuff
108              
109             sub _init_file {
110 0     0     my ( $put, $type, $desc, $args ) = @_;
111             { base => ($$args{$put} || die "no data while grabbing $desc" )
112 0   0       , type => ($$args{$type} || do {
      0        
113             }) }
114             }
115              
116             sub extension_of {
117 0 0   0     (shift) =~ qr{
118             (?: (? / )
119             | [.] (? xlsx? )
120             )$
121 2     2   2672 }x and $+{type};
  2         1180  
  2         681  
122             }
123              
124             sub _file_spec {
125 0     0     my ( $data, $type ) = @_;
126 0 0         defined $data or die "no data";
127 0 0 0       map { $_ eq '/' and $_ = 'dir' }
  0   0        
128             $type ||= extension_of($data) || 'dir';
129              
130 0           { base => $data
131             , type => $type }
132              
133             }
134              
135             sub init {
136 0 0   0     my %args = @_ ? @_ : @ARGV;
137 0 0         my %conf = map {
138 0           $args{$_}
139             ? ( $_ => [ split /,/, $args{$_} ] )
140             : ()
141             } qw< can >;
142              
143 0 0         map { die "no $_" unless $args{$_} }
  0            
144             qw< from to >;
145              
146 0           $conf{from} = _file_spec @args{qw< from is >};
147 0           $conf{to} = _file_spec @args{qw< to will >};
148 0           \%conf
149             }
150              
151             sub provider {
152 0     0     my $spec = shift;
153 0           my $provider = "App::Tables::Provider::$$spec{type}";
154 0           $provider->new( $spec )
155             }
156              
157             1;