App-plmetrics

 view release on metacpan or  search on metacpan

lib/App/plmetrics.pm  view on Meta::CPAN

package App::plmetrics;
use strict;
use warnings;
use Module::Path;
use Perl::Metrics::Lite;
use Statistics::Swoop;
use Text::ASCIITable;

our $VERSION = '0.06';

my %VIEWER = (
    qr/^modules?$/i => sub { $_[0]->_view_module($_[1]) },
    qr/^methods?$/i => sub { $_[0]->_view_methods($_[1]) },
    qr/^cc$/i       => sub { $_[0]->_view_cc_lines($_[1], 'cc') },
    qr/^lines?$/i   => sub { $_[0]->_view_cc_lines($_[1], 'lines') },
    qr/^files?$/i   => sub { $_[0]->_view_files($_[1]) },
);

sub new {
    my ($class, $opt) = @_;
    bless +{ opt => $opt } => $class;
}

sub opt { $_[0]->{opt} }

sub run {
    my $self = shift;

    my ($targets, $base_path) = $self->_get_targets;
    my $stats = $self->_get_stats($targets, $base_path);
    $self->_view($stats);
}

sub _view {
    my ($self, $stats) = @_;

    my $result_opt = $self->opt->{'--result'} || 'module';

    for my $regex (keys %VIEWER) {
        if ($result_opt =~ $regex) {
            $VIEWER{$regex}->($self, $stats);
            return;
        }
    }
    print STDERR "wrong option: --result $result_opt\nsee the --help\n";
}

sub _view_cc_lines {
    my ($self, $stats, $label) = @_;

    print "$label\n";
    my $t = Text::ASCIITable->new;
    my @metrics_keys = keys %{$stats};
    for my $pl ( $self->opt->{'--sort'} ? sort @metrics_keys : @metrics_keys ) {
        $t->setCols($self->_header);
        $t->addRow( $pl, $self->_row($stats->{$pl}{$label}) );
    }
    print $t. "\n";
}

sub _header { ('', qw/avg max min range sum methods/) }

sub _row {
    my ($self, $list) = @_;

    my $stats = Statistics::Swoop->new($list);
    return( $self->_round($stats->avg), $stats->max, $stats->min,
                $stats->range, $stats->sum, $stats->count );
}

sub _view_module {
    my ($self, $stats) = @_;

    my @metrics_keys = keys %{$stats};
    for my $pl ( $self->opt->{'--sort'} ? sort @metrics_keys : @metrics_keys ) {
        print "$pl\n";
        my $t = Text::ASCIITable->new;
        $t->setCols($self->_header);
        $t->addRow( 'cc', $self->_row($stats->{$pl}{cc}) );
        $t->addRow( 'lines', $self->_row($stats->{$pl}{lines}) );
        print $t. "\n";

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.970 second using v1.00-cache-1.14-grep-28634ff-cpan-4d46879620f )