| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Catmandu::Exporter::Table; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = '0.3.0'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 33904 | use Catmandu::Sane; | 
|  | 2 |  |  |  |  | 178824 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 6 | 2 |  |  | 2 |  | 576 | use Moo; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 7 | 2 |  |  | 2 |  | 1733 | use Text::MarkdownTable; | 
|  | 2 |  |  |  |  | 32974 |  | 
|  | 2 |  |  |  |  | 93 |  | 
| 8 | 2 |  |  | 2 |  | 18 | use IO::Handle::Util (); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 37 |  | 
| 9 | 2 |  |  | 2 |  | 8 | use IO::File; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 298 |  | 
| 10 | 2 |  |  | 2 |  | 1737 | use JSON::XS (); | 
|  | 2 |  |  |  |  | 9948 |  | 
|  | 2 |  |  |  |  | 917 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | with 'Catmandu::Exporter'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # JSON Table Schema | 
| 15 |  |  |  |  |  |  | has schema => ( | 
| 16 |  |  |  |  |  |  | is => 'ro', | 
| 17 |  |  |  |  |  |  | coerce => sub { | 
| 18 |  |  |  |  |  |  | my $schema = $_[0]; | 
| 19 |  |  |  |  |  |  | unless (ref $schema and ref $schema eq 'HASH') { | 
| 20 |  |  |  |  |  |  | $schema = \*STDIN if $schema eq '-'; | 
| 21 |  |  |  |  |  |  | my $fh = ref $schema | 
| 22 |  |  |  |  |  |  | ? IO::Handle::Util::io_from_ref($schema) | 
| 23 |  |  |  |  |  |  | : IO::File->new($schema, "r"); | 
| 24 |  |  |  |  |  |  | die "failed to load JSON Table Schema from $schema" unless $fh; | 
| 25 |  |  |  |  |  |  | local $/; | 
| 26 |  |  |  |  |  |  | $schema = JSON::XS::decode_json(<$fh>); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | $schema; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | has fields => ( | 
| 33 |  |  |  |  |  |  | is      => 'ro', | 
| 34 |  |  |  |  |  |  | lazy    => 1, | 
| 35 |  |  |  |  |  |  | builder => sub { | 
| 36 | 4 | 100 |  | 4 |  | 964 | return unless $_[0]->schema; | 
| 37 | 2 |  |  |  |  | 5 | [ map { $_->{name} } @{$_[0]->schema->{fields}} ]; | 
|  | 4 |  |  |  |  | 17 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | has columns => ( | 
| 42 |  |  |  |  |  |  | is      => 'ro', | 
| 43 |  |  |  |  |  |  | lazy    => 1, | 
| 44 |  |  |  |  |  |  | builder => sub { | 
| 45 | 5 | 100 |  | 5 |  | 742 | return unless $_[0]->schema; | 
| 46 | 2 |  | 66 |  |  | 3 | [ map { $_->{title} // $_->{name} } @{$_[0]->schema->{fields}} ]; | 
|  | 4 |  |  |  |  | 27 |  | 
|  | 2 |  |  |  |  | 7 |  | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | has widths   => (is => 'ro'); | 
| 51 |  |  |  |  |  |  | has condense => (is => 'ro'); | 
| 52 |  |  |  |  |  |  | has header   => (is => 'ro'); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | has _table => ( | 
| 55 |  |  |  |  |  |  | is      => 'lazy', | 
| 56 |  |  |  |  |  |  | default => sub { | 
| 57 |  |  |  |  |  |  | Text::MarkdownTable->new( | 
| 58 |  |  |  |  |  |  | file => $_[0]->fh, | 
| 59 |  |  |  |  |  |  | map { $_ => $_[0]->$_ } | 
| 60 |  |  |  |  |  |  | grep { defined $_[0]->$_ } | 
| 61 |  |  |  |  |  |  | qw(fields columns widths condense header) | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  | }, | 
| 64 |  |  |  |  |  |  | ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub add { | 
| 67 |  |  |  |  |  |  | $_[0]->_table->add($_[1]) | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub commit { | 
| 71 | 5 |  |  | 5 | 0 | 5409 | $_[0]->_table->done | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | 1; | 
| 75 |  |  |  |  |  |  | __END__ |