use strict;
use warnings;

package Code::Statistics::Config;
$Code::Statistics::Config::VERSION = '1.190680';
# ABSTRACT: merges configuration options from various sources

use Moose;
use MooseX::HasDefaults::RO;

use Hash::Merge qw( merge );
use Config::INI::Reader;
use Path::Class qw(file);
use File::HomeDir;

has args => ( isa => 'HashRef', default => sub {{}} );

has command => ( isa => 'Str', );

has conf_file => ( isa => 'Str', default => '.codestatrc' );

has global_conf_file => ( isa => 'Str', default => sub { file( File::HomeDir->my_home, '.codestatrc' )->stringify } );

has profile => ( isa => 'Str', );


sub assemble {
    my ( $self ) = @_;

    my $config = {};

    $config = merge( $self->_global_config, $config );
    $config = merge( $self->_local_config,  $config );
    $config = merge( $self->args,    $config );

    return $config;
}

sub _local_config {
    my ( $self ) = @_;

    return $self->_merged_conf_from( $self->conf_file );
}

sub _global_config {
    my ( $self ) = @_;

    return $self->_merged_conf_from( $self->global_conf_file );
}

sub _merged_conf_from {
    my ( $self, $file ) = @_;

    return {} if !$file or !-e $file;

    my $conf = Config::INI::Reader->read_file( $file );

    my $merge;
    my @sections = grep { defined } ( '_', $self->command, $self->_profile_section );
    for ( @sections ) {
        next if !$conf->{$_};
        $merge = merge( $conf->{$_}, $merge );
    }

    return $merge;
}

sub _profile_section {
    my ( $self ) = @_;

    my $section = $self->command;
    $section .= '::' . $self->profile if $self->profile;

    return $section;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Code::Statistics::Config - merges configuration options from various sources

=head1 VERSION

version 1.190680

=head2 assemble
    Builds the command-related configuration hash. The hash contains all config
    options from the global config file, local file and command line arguments.

=head1 AUTHOR

Christian Walde <mithaldu@yahoo.de>

=head1 COPYRIGHT AND LICENSE


Christian Walde has dedicated the work to the Commons by waiving all of his
or her rights to the work worldwide under copyright law and all related or
neighboring legal rights he or she had in the work, to the extent allowable by
law.

Works under CC0 do not require attribution. When citing the work, you should
not imply endorsement by the author.

=cut
