File Coverage

blib/lib/Data/Importer/Iterator/Ods.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of Data-Importer
3             #
4             # This software is copyright (c) 2014 by Kaare Rasmussen.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Data::Importer::Iterator::Ods;
10             $Data::Importer::Iterator::Ods::VERSION = '0.006';
11 1     1   34 use 5.010;
  1         3  
  1         54  
12 1     1   7 use namespace::autoclean;
  1         3  
  1         14  
13 1     1   91 use Moose;
  1         2  
  1         11  
14 1     1   11543 use Spreadsheet::ReadSXC qw/read_sxc/;
  0            
  0            
15              
16             extends 'Data::Importer::Iterator';
17              
18             =head1 Description
19              
20             Subclass for handling the import of an ods file
21              
22             =head1 ATTRIBUTES
23              
24             =head2 ods
25              
26             The ods object
27              
28             =cut
29              
30             has 'ods' => (
31             is => 'ro',
32             lazy_build => 1,
33             );
34              
35             =head2 sheet
36              
37             The sheet name or number
38              
39             =cut
40              
41             has sheet => (
42             is => 'ro',
43             isa => 'Int',
44             default => 0,
45             );
46              
47             =head2 options
48              
49             Options to be passed to the Spreadsheet::ReadSXC constructor
50              
51             =cut
52              
53             has 'options' => (
54             is => 'ro',
55             isa => 'HashRef',
56             default => sub {
57             return {
58             OrderBySheet => 1,
59             };
60             },
61             lazy => 1,
62             );
63              
64             =head1 "PRIVATE" ATTRIBUTES
65              
66             =head2 column_names
67              
68             The column names
69              
70             =cut
71              
72             has column_names => (
73             is => 'rw',
74             isa => 'ArrayRef',
75             predicate => 'has_column_names',
76             );
77              
78             =head1 METHODS
79              
80             =head2 _build_ods
81              
82             The lazy builder for the ods object
83              
84             =cut
85              
86             sub _build_ods {
87             my $self = shift;
88             my $worksheets_ref = read_sxc($self->file_name, $self->options);
89             my $ods = $$worksheets_ref[0]{data};
90             return $ods;
91             }
92              
93             =head2 next
94              
95             Return the next row of data from the file
96              
97             =cut
98              
99             sub next {
100             my $self = shift;
101             my $ods = $self->ods;
102             # Use the first row as column names:
103             if (!$self->has_column_names) {
104             my @fieldnames = map {my $header = lc $_; $header =~ tr/ /_/; $header} @{ $ods->[$self->lineno] };
105             die "Only one column detected, please use comma ',' to separate data." if @fieldnames < 2;
106             my %fieldnames = map {$_ => 1} @fieldnames;
107             if (my @missing = grep {!$fieldnames{$_} } @{ $self->mandatory }) {
108             die 'Column(s) required, but not found:' . join ', ', @missing;
109             }
110              
111             $self->column_names(\@fieldnames);
112             }
113             my $columns = $self->column_names;
114             $self->inc_lineno;
115             my @cells = @{ $ods->[$self->lineno] || [] };
116             return unless grep { $_ } @cells;
117              
118             my $colno = 0;
119             return { map { $columns->[$colno++] => $_ } @cells };
120             }
121              
122             __PACKAGE__->meta->make_immutable;
123              
124             #
125             # This file is part of Data-Importer
126             #
127             # This software is copyright (c) 2014 by Kaare Rasmussen.
128             #
129             # This is free software; you can redistribute it and/or modify it under
130             # the same terms as the Perl 5 programming language system itself.
131             #
132              
133             __END__