[Bps-public-commit] r12483 - in Prophet/branches/moose: . lib/Prophet/Replica

sartak at bestpractical.com sartak at bestpractical.com
Sat May 17 09:02:13 EDT 2008


Author: sartak
Date: Sat May 17 09:02:12 2008
New Revision: 12483

Modified:
   Prophet/branches/moose/   (props changed)
   Prophet/branches/moose/lib/Prophet/Replica.pm
   Prophet/branches/moose/lib/Prophet/Replica/Native.pm

Log:
 r56230 at onn:  sartak | 2008-05-17 09:02:08 -0400
 Make the replica rebless logic more moosey


Modified: Prophet/branches/moose/lib/Prophet/Replica.pm
==============================================================================
--- Prophet/branches/moose/lib/Prophet/Replica.pm	(original)
+++ Prophet/branches/moose/lib/Prophet/Replica.pm	Sat May 17 09:02:12 2008
@@ -31,7 +31,8 @@
 );
 
 has url => (
-    is => 'rw',
+    is  => 'rw',
+    isa => 'Str',
 );
 
 use constant state_db_uuid => 'state';
@@ -57,7 +58,7 @@
 
 =head1 METHODS
 
-=head2 new
+=head2 BUILD
 
 Instantiates a new replica
 
@@ -65,12 +66,22 @@
 
 sub _unimplemented { my $self = shift; die ref($self). " does not implement ". shift; }
 
-sub new {
-    my $self = shift->SUPER::new(@_);
-    $self->_rebless_to_replica_type(@_);
-    $self->setup();
-    return $self;
-}
+around new => sub {
+    my $orig  = shift;
+    my $class = shift;
+    my %args  = @_ == 1 ? %{ $_[0] } : @_;
+
+    my ($new_class, $scheme, $url) = $class->_url_to_replica_class($args{url});
+
+    if (!$new_class) {
+        $class->log_fatal("$scheme isn't a replica type I know how to handle. (The Replica URL given was $args{url})");
+    }
+
+    return $orig->($class, %args, url => $args{url}) if $class eq $new_class;
+
+    $new_class->require;
+    return $new_class->new(%args);
+};
 
 =head2 register_replica_scheme { class=> Some::Perl::Class, scheme => 'scheme:' }
 
@@ -87,23 +98,19 @@
 
 
 }
-=head2 _rebless_to_replica_type
 
-Reblesses this replica into the right sort of replica for whatever kind of replica $self->url points to
+=head2 _url_to_replica_class
 
+Returns the replica class for the given url.
 
 =cut
-sub _rebless_to_replica_type {
+
+sub _url_to_replica_class {
     my $self = shift;
+    my $url  = shift;
 
-    my ($scheme, $real_url) = split(/:/,$self->url,2);
-    $self->url($real_url);
-    if ( my $class = $Prophet::Replica::REPLICA_TYPE_MAP->{$scheme}) {
-    $class->require or die $@;
-    return bless $self, $class;
-    } else {
-        $self->log_fatal( "$scheme isn't a replica type I know how to handle. (The Replica URL given was ".$self->url.")");
-    }
+    my ($scheme, $real_url) = split /:/, $url;
+    return ($Prophet::Replica::REPLICA_TYPE_MAP->{$scheme}, $scheme, $real_url);
 }
 
 sub import_changesets {

Modified: Prophet/branches/moose/lib/Prophet/Replica/Native.pm
==============================================================================
--- Prophet/branches/moose/lib/Prophet/Replica/Native.pm	(original)
+++ Prophet/branches/moose/lib/Prophet/Replica/Native.pm	Sat May 17 09:02:12 2008
@@ -42,13 +42,13 @@
 use constant record_dir        => 'records';
 use constant changeset_index   => 'changesets.idx';
 
-=head2 setup
+=head2 BUILD
 
 Open a connection to the SVN source identified by C<$self->url>.
 
 =cut
 
-sub setup {
+sub BUILD {
     my $self = shift;
     $self->{url}
         =~ s/^prophet://;  # url-based constructor in ::replica should do better



More information about the Bps-public-commit mailing list