[Bps-public-commit] r16059 - in Prophet/branches/dispatcher: lib/Prophet

sartak at bestpractical.com sartak at bestpractical.com
Thu Sep 25 15:14:00 EDT 2008


Author: sartak
Date: Thu Sep 25 15:13:59 2008
New Revision: 16059

Modified:
   Prophet/branches/dispatcher/   (props changed)
   Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm

Log:
 r72457 at onn:  sartak | 2008-09-25 15:13:15 -0400
 Move the interesting bits of RecordCommand into CLIContext


Modified: Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm
==============================================================================
--- Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm	(original)
+++ Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm	Thu Sep 25 15:13:59 2008
@@ -81,6 +81,11 @@
     documentation => "The commands the user executes from the commandline",
 );
 
+has record_class => (
+    is  => 'rw',
+    isa => 'Prophet::Record',
+);
+
 =head2 mutate_attributes ( args => $hashref, props => $hashref, type => 'str' )
 
 A hook for running a second command from within a command without having       
@@ -264,6 +269,78 @@
     }
 }
 
+=head2 _get_record_object [{ type => 'type' }]
+
+Tries to determine a record class from either the given type argument or
+the current object's C<$type> attribute.
+
+Returns a new instance of the record class on success, or throws a fatal
+error with a stack trace on failure.
+
+=cut
+
+sub _get_record_object {
+    my $self = shift;
+    my %args = validate(@_, {
+        type => { default => $self->type },
+    });
+
+    my $constructor_args = {
+        app_handle => $self->cli->app_handle,
+        handle     => $self->cli->handle,
+        type       => $args{type},
+    };
+
+    if ($args{type}) {
+        my $class = $self->_type_to_record_class($args{type});
+        return $class->new($constructor_args);
+    }
+    elsif (my $class = $self->record_class) {
+        Prophet::App->require($class);
+        return $class->new($constructor_args);
+    }
+    else {
+        Carp::confess("I was asked to get a record object, but I have neither a type nor a record class");
+    }
+}
+
+=head2 _load_record
+
+Attempts to load the record specified by the C<uuid> attribute.
+
+Returns the loaded record on success, or throws a fatal error if no
+record can be found.
+
+=cut
+
+sub _load_record {
+    my $self = shift;
+    my $record = $self->_get_record_object;
+    $record->load( uuid => $self->uuid )
+        || $self->fatal_error("I couldn't find the " . $self->type . ' ' . $self->uuid);
+    return $record;
+}
+
+=head2 _type_to_record_class $type
+
+Takes a type and tries to figure out a record class name from it.
+Returns C<'Prophet::Record'> if no better class name is found.
+
+=cut
+
+sub _type_to_record_class {
+    my $self = shift;
+    my $type = shift;
+    my $try = $self->cli->app_class . "::Model::" . ucfirst( lc($type) );
+    Prophet::App->try_to_require($try);    # don't care about fails
+    return $try if ( $try->isa('Prophet::Record') );
+
+    $try = $self->cli->app_class . "::Record";
+    Prophet::App->try_to_require($try);    # don't care about fails
+    return $try if ( $try->isa('Prophet::Record') );
+    return 'Prophet::Record';
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 



More information about the Bps-public-commit mailing list