<commit_before>package Webservice::InterMine::Role::Showable;

use Moose::Role;

requires qw/to_string views table_format results_iterator/;

sub show {
    my $self = shift;
    my $fh = shift || \*STDOUT;

    binmode $fh, ':encoding(utf8)';
    print $fh $self->to_string, "\n";
    printf $fh $self->table_format, $self->views;
    my $iter = $self->results_iterator;
    while (<$iter>) {
        printf $fh $self->table_format, map {(defined $_) ? $_ : 'UNDEF'} @$_;
    }
}

1;
<commit_msg>Moved result printing into this role so that lists have access to it<commit_after>package Webservice::InterMine::Role::Showable;

use Moose::Role;

requires qw/to_string views table_format results_iterator/;

use IO::Handle;

sub show {
    my $self = shift;
    my $fh = shift || \*STDOUT;

    binmode $fh, ':encoding(utf8)';
    print $fh $self->to_string, "\n";
    printf $fh $self->table_format, $self->views;
    my $iter = $self->results_iterator;
    while (<$iter>) {
        printf $fh $self->table_format, map {(defined $_) ? $_ : 'UNDEF'} @$_;
    }
}

=head2 print_results( %options )

returns the results from a query in the result format
specified. 

The following options are available:

=over 4

=item * to => $filename|GlobRef

A file name to open, or a file handle opened for writing.

=item * as => $format

Possible values: (tsv|csv|arrayrefs|hashrefs|jsonobjects|jsonrows|count)

The format to print results in. The default is C<tsv>

=item * size => $size

The number of results to return. Leave undefined for "all" (default).

=item * start => $start 

The first result to return (starting at 0). The default is 0.

=item * addheaders => 0/1/friendly/path

Whether to return the column headers at the top of TSV/CSV results. The default is
false. There are two styles - friendly: "Gene > pathways > name" and 
path: "Gene.pathways.name". The default style is friendly if a true value is entered and
it is not "path".

=item * json => $json_processor

Possible values: (inflate|instantiate|perl)

What to do with JSON results. The results can be returned as inflated objects,
full instantiated Moose objects, a raw json string, or as a perl
data structure. (default is C<perl>). B<THIS OPTION MAKES LITTLE SENSE WHEN 
PRINTING RESULTS.>

=back

=cut

sub print_results {
    my $self = shift;
    my %args = @_;
    my $to = delete($args{to}) || \*STDOUT;
    $args{as} ||= 'tab'; # For printing, we default to TSV.
    my $out; 
    if ($to) {
        if (ref $to eq 'GLOB') {
            $out = $to;
        } else {
            open($out, '>', $to) or confess "Cannot open $to, $!";
        }
    } else {
        $out = \*STDOUT;
    }
    binmode $out, ':encoding(utf8)';
    my $iter = $self->results_iterator(%args);
    while (my $line = <$iter>) {
        $out->print($line, "\n");
    }
}

1;

{'fraction_non_alphanumeric': 0.13810198300283286, 'fraction_numerical': 0.003895184135977337, 'mean_word_length': 3.2417417417417416, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '832554', 'n_tokens_mistral': 994, 'n_tokens_neox': 936, 'n_words': 324}