diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-06-08 13:56:03 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-06-08 13:56:03 -0500 |
commit | cef5feaae1d2472e2d2b5090d680dd3d7289d76d (patch) | |
tree | e89d4503366b970ff9f71cde2e006ac4f8f19ad2 /lib | |
parent | 1d7c2a0959a8e91ff3d6ccb279fd6928d397e602 (diff) | |
download | reply-cef5feaae1d2472e2d2b5090d680dd3d7289d76d.tar.gz reply-cef5feaae1d2472e2d2b5090d680dd3d7289d76d.zip |
split out config handling
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Reply.pm | 12 | ||||
-rw-r--r-- | lib/Reply/Config.pm | 39 |
2 files changed, 46 insertions, 5 deletions
diff --git a/lib/Reply.pm b/lib/Reply.pm index 1534bfe..c3fa297 100644 --- a/lib/Reply.pm +++ b/lib/Reply.pm @@ -3,11 +3,12 @@ use strict; use warnings; # ABSTRACT: read, eval, print, loop, yay! -use Config::INI::Reader::Ordered; use Module::Runtime qw(compose_module_name use_package_optimistically); use Scalar::Util qw(blessed); use Try::Tiny; +use Reply::Config; + =head1 SYNOPSIS use Reply; @@ -87,9 +88,10 @@ sub new { }, $class; if (defined $opts{config}) { - print "Loading configuration from $opts{config}... "; + if (!ref($opts{config})) { + $opts{config} = Reply::Config->new(file => $opts{config}); + } $self->_load_config($opts{config}); - print "done\n"; } $self->_load_plugin($_) for @{ $opts{plugins} || [] }; @@ -136,9 +138,9 @@ sub run_one { sub _load_config { my $self = shift; - my ($file) = @_; + my ($config) = @_; - my $data = Config::INI::Reader::Ordered->new->read_file($file); + my $data = $config->data; my $root_config; for my $section (@$data) { diff --git a/lib/Reply/Config.pm b/lib/Reply/Config.pm new file mode 100644 index 0000000..ed1f53d --- /dev/null +++ b/lib/Reply/Config.pm @@ -0,0 +1,39 @@ +package Reply::Config; +use strict; +use warnings; + +use Config::INI::Reader::Ordered; +use File::HomeDir; +use File::Spec; + +sub new { + my $class = shift; + my %opts = @_; + + $opts{file} = '.replyrc' + unless defined $opts{file}; + + my $file = File::Spec->catfile( + (File::Spec->file_name_is_absolute($opts{file}) + ? () + : (File::HomeDir->my_home)), + $opts{file} + ); + + my $self = bless {}, $class; + + $self->{file} = $file; + $self->{config} = Config::INI::Reader::Ordered->new; + + return $self; +} + +sub file { shift->{file} } + +sub data { + my $self = shift; + + return $self->{config}->read_file($self->{file}); +} + +1; |