[Bps-public-commit] r12479 - in Prophet/branches/moose: .
sartak at bestpractical.com
sartak at bestpractical.com
Sat May 17 08:32:42 EDT 2008
Author: sartak
Date: Sat May 17 08:32:42 2008
New Revision: 12479
Modified:
Prophet/branches/moose/ (props changed)
Prophet/branches/moose/lib/Prophet/Config.pm
Log:
r56226 at onn: sartak | 2008-05-17 08:32:31 -0400
Moosify Prophet::Config
Modified: Prophet/branches/moose/lib/Prophet/Config.pm
==============================================================================
--- Prophet/branches/moose/lib/Prophet/Config.pm (original)
+++ Prophet/branches/moose/lib/Prophet/Config.pm Sat May 17 08:32:42 2008
@@ -1,10 +1,65 @@
-use warnings;
-use strict;
-
package Prophet::Config;
-
+use Moose;
+use MooseX::AttributeHelpers;
use Path::Class;
+has config => (
+ metaclass => 'Collection::Hash',
+ is => 'rw',
+ isa => 'HashRef',
+ lazy => 1,
+ default => sub { shift->load_config_files },
+ provides => {
+ get => 'get',
+ set => 'set',
+ },
+);
+
+sub prophet_config_file { dir($ENV{HOME}, ".prophetrc") }
+sub app_config_file { dir($ENV{PROPHET_REPO}, "prophetrc") }
+
+my $singleton;
+around new => sub {
+ return $singleton if $singleton;
+ my $orig = shift;
+ return $singleton = $orig->(@_);
+};
+
+sub load_config_files {
+ my $self = shift;
+ my @config = @_;
+ @config = grep { -f $_ } $self->prophet_config_file, $self->app_config_file
+ if !@config;
+
+ my $config = {};
+
+ for my $file (@config) {
+ $self->load_config_file($file, $config);
+ }
+
+ return $config;
+}
+
+sub load_config_file {
+ my $self = shift;
+ my $file = shift;
+ my $config = shift || {};
+
+ for my $line ($file->slurp) {
+ s/\#.*//; # strip comments
+ if ($line =~ /^([^:]+):\s*(.*)$/) {
+ $config->{$1} = $2;
+ }
+ }
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+
+__END__
+
=head1 NAME
Prophet::Config
@@ -29,93 +84,31 @@
=cut
-my $singleton;
-sub new {
- my $class = shift;
- return $singleton if $singleton;
-
- my $self = $singleton = bless {}, $class;
- $self->load_config_files;
- return $self;
-}
-
=head2 prophet_config_file
The file which controls configuration for all Prophet apps. C<$HOME/.prophetc>.
-=cut
-
-sub prophet_config_file { dir($ENV{HOME}, ".prophetrc") }
-
=head2 app_config_file
The file which controls configuration for this application.
C<$PROPHET_REPO/prophetrc>.
-=cut
-
-sub app_config_file { dir($ENV{PROPHET_REPO}, "prophetrc") }
-
=head2 load_config_files [files]
Loads the given config files. If no files are passed in, it will use the
default of L</prophet_config_file> and L</app_config_file>.
-=cut
-
-sub load_config_files {
- my $self = shift;
- my @config = @_;
- @config = ($self->prophet_config_file, $self->app_config_file) if !@config;
-
- for my $file (@config) {
- $self->load_config_file($file);
- }
-}
-
=head2 load_config_file file
Loads the given config file.
-=cut
-
-sub load_config_file {
- my $self = shift;
- my $file = shift;
-
- for my $line ($file->slurp) {
- s/\#.*//; # strip comments
- if ($line =~ /^([^:]+):\s*(.*)$/) {
- $self->{$1} = $2;
- }
- }
-}
-
=head2 get
Gets a specific config setting.
-=cut
-
-sub get {
- my $self = shift;
- my $key = shift;
-
- return $self->{$key};
-}
-
=head2 set
Sets a specific config setting.
=cut
-sub set {
- my $self = shift;
- my $key = shift;
- my $value = shift;
-
- return $self->{$key} = $value;
-}
-1;
-
More information about the Bps-public-commit
mailing list