package Religion::Bible::Regex::Config;

use warnings;
use strict;
use Carp;

use version; our $VERSION = qv('0.2');

use YAML::Loader;
use Data::Dumper;

# Input files are assumed to be in the UTF-8 strict character encoding.
use utf8;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

sub new {
	my $class = shift;
    my $self = {};
    my $config = shift;

    bless ($self, $class);

    croak "Religion::Bible::Regex::Config must be initialize with a string which containing either a the location of a YAML file or actual YAML" unless (defined($config));

    # If $config is a file that exists
    # Crud method for testing that this is not YAML --> m/\n/ == 0
    if (m/\n/ == 0 && -e $config) {
        $self->{config} = $self->_read_yaml_file($config);
    } else {
       my $yaml_loader = YAML::Loader->new();
       $self->{config} = $yaml_loader->load($config); 
    }

    return $self;
}

sub get {
    my $self = shift;
    my @keys = @_;

    my $ret = $self->{config};
    foreach my $key (@keys) {
        carp "Configuration not found: {$key}" unless defined($ret->{$key});
        $ret = $ret->{$key};
    }
    return $ret;
}

sub get_reference {
	my $self = shift;
	return $self->{'config'}{'reference'}; 
}

sub get_regex {
	my $self = shift;
	return $self->{'config'}{'regex'}; 
}

sub get_versification {
	my $self = shift;
	return $self->{'config'}{'versification'}; 
}

sub _read_yaml_file {
    my $self = shift;
    my $path_to_config_file = shift;
    my $config;

    my $yaml_loader = YAML::Loader->new();
    my $yaml_text;

    # Vous ouvrez le fichier de configuration 
    if(open(*CONFIG, "<:encoding(UTF-8)", $path_to_config_file)) { 
        {
            local( $/, *FH );
            $yaml_text = <CONFIG>;  # slurp it
        }
        $config = $yaml_loader->load($yaml_text);
    	close (CONFIG);
    }

    return $config;
}
1; # Magic true value required at end of module
__END__

=head1 NAME

Religion::Bible::Regex::Config - Creates a configuration object for the Religion::Bible::Regex objects from a YAML file.


=head1 VERSION

This document describes Religion::Bible::Regex::Config version 0.2


=head1 SYNOPSIS

    use Religion::Bible::Regex::Config;

    # Initialize with a YAML file or a string containing YAML
    my $c = new Religion::Bible::Regex::Config("config.yml");

    # Retrieve configurations in YAML format    
    my $regex_configurations = $c->get_regex_configs;
    my $reference_configurations = $c->get_reference_configs;
    
    # Initialize other Religion::Bible::Regex objects
    my $r   = new Religion::Bible::Regex::Regex($c);
    my $v   = new Religion::Bible::Regex::Versification($r, $c);
    my $ref = new Religion::Bible::Regex::Reference($r, $c);
      

=head1 INTERFACE 

=head2 new

Creates a configuration object from a YAML file or string

=head2 get

Returns a configuration string

=head2 gethash

Returns a hash of all configurations

=head2 get_reference

Returns a hash of the reference configurations

=head2 get_regex

Returns a hash of the regex configurations

=head2 get_versification

Returns a hash of the versification configurations


=head1 DIAGNOSTICS

=over

=item If you do not pass a YAML file or string when creating a new instance then your program will croak.

=item If you pass in invalid YAML then expect your program to stop and dump the errors
to the STDOUT.

See the YAML module for more details.

=back


=head1 CONFIGURATION AND ENVIRONMENT
  
Religion::Bible::Regex::Config requires no configuration files or environment variables.


=head1 DEPENDENCIES

B<YAML>

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-religion-bible-regex-config@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.


=head1 AUTHOR

Daniel Holmlund  C<< <holmlund.dev@gmail.com> >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2009, Daniel Holmlund C<< <holmlund.dev@gmail.com> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.


=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
