[Bps-public-commit] r12406 - in Prophet/branches/luid: lib/Prophet

sartak at bestpractical.com sartak at bestpractical.com
Fri May 16 10:38:07 EDT 2008


Author: sartak
Date: Fri May 16 10:37:52 2008
New Revision: 12406

Added:
   Prophet/branches/luid/lib/Prophet/Config.pm
Modified:
   Prophet/branches/luid/   (props changed)
   Prophet/branches/luid/lib/Prophet/App.pm

Log:
- Merge //mirror/bps-public/Prophet/trunk to //mirror/bps-public/Prophet/branches/luid

Modified: Prophet/branches/luid/lib/Prophet/App.pm
==============================================================================
--- Prophet/branches/luid/lib/Prophet/App.pm	(original)
+++ Prophet/branches/luid/lib/Prophet/App.pm	Fri May 16 10:37:52 2008
@@ -4,10 +4,15 @@
 package Prophet::App;
 use base qw/Class::Accessor/;
 use Path::Class;
-__PACKAGE__->mk_accessors(qw/_resdb_handle/);
+__PACKAGE__->mk_accessors(qw/_resdb_handle _config/);
 
 use constant DEFAULT_REPLICA_TYPE => 'prophet';
 
+=head1 NAME
+
+Prophet::App
+
+=cut
 
 sub _handle {
     my $self = shift;
@@ -89,5 +94,31 @@
     $@ = '';
 }
 
+=head2 config
+
+If called with no arguments, returns the L<Prophet::Config> instance.
+
+If called with one arguments, returns the value of that config setting.
+
+If called with two arguments, sets the value of that config setting.
+
+=cut
+
+sub config {
+    my $self = shift;
+
+    unless ($self->_config) {
+        require Prophet::Config;
+        $self->_config(Prophet::Config->new);
+    }
+
+    return $self->_config if @_ == 0;
+
+    my $key = shift;
+    return $self->_config->get($key) if @_ == 0;
+
+    my $value = shift;
+    return $self->_config->set($key => $value);
+}
 
 1;

Added: Prophet/branches/luid/lib/Prophet/Config.pm
==============================================================================
--- (empty file)
+++ Prophet/branches/luid/lib/Prophet/Config.pm	Fri May 16 10:37:52 2008
@@ -0,0 +1,121 @@
+use warnings;
+use strict;
+
+package Prophet::Config;
+
+use Path::Class;
+
+=head1 NAME
+
+Prophet::Config
+
+=head1 SYNOPSIS
+
+    In ~/.prophetrc:
+
+        prefer_luids: 1
+
+=head1 DESCRIPTION
+
+This class represents configuration of Prophet and the application built on top
+of it.
+
+=head1 METHODS
+
+=head2 new
+
+Takes no arguments. Automatically loads the config for you. This is actually
+a singleton so multiple calls to new get the same config.
+
+=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