[Bps-public-commit] r11405 - SVN-PropDB/lib/Prophet
clkao at bestpractical.com
clkao at bestpractical.com
Wed Apr 2 21:38:17 EDT 2008
Author: clkao
Date: Wed Apr 2 21:38:16 2008
New Revision: 11405
Modified:
SVN-PropDB/lib/Prophet/CLI.pm
SVN-PropDB/lib/Prophet/Handle.pm
SVN-PropDB/lib/Prophet/Record.pm
Log:
cli record_class.
Modified: SVN-PropDB/lib/Prophet/CLI.pm
==============================================================================
--- SVN-PropDB/lib/Prophet/CLI.pm (original)
+++ SVN-PropDB/lib/Prophet/CLI.pm Wed Apr 2 21:38:16 2008
@@ -3,7 +3,7 @@
package Prophet::CLI;
use base qw/Class::Accessor/;
-__PACKAGE__->mk_accessors(qw/type uuid _handle _resdb_handle/);
+__PACKAGE__->mk_accessors(qw/record_class type uuid _handle _resdb_handle/);
use Path::Class;
use Prophet;
@@ -15,6 +15,7 @@
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
+ $self->record_class('Prophet::Record') unless $self->record_class;
$self->handle;
$self->resdb_handle;
return $self;
@@ -60,13 +61,19 @@
);
sub record_cmd {
- my ($self, $type) = @_;
+ my ($self, $type, $record_class) = @_;
return sub {
my $cmd = shift @ARGV or die "record subcommand required";
$cmd =~ s/^--//g;
$cmd = $CMD_MAP{$cmd} if exists $CMD_MAP{$cmd};
- my $func = $self->can("do_$cmd") or die "no such crecord ommand $cmd";
- $self->type($type);
+ my $func = $self->can("do_$cmd") or die "no such record command $cmd";
+ if ($record_class) {
+ $self->record_class($record_class);
+ }
+ else {
+ $self->record_class('Prophet::Record');
+ $self->type($type);
+ }
$self->parse_record_cmd_args();
$func->($self);
};
@@ -78,14 +85,17 @@
=cut
+
sub register_types {
my $self = shift;
+ my $model_base = shift;
my @types = (@_);
my $calling_package = (caller)[0];
for my $type (@types) {
no strict 'refs';
- *{$calling_package."::cmd_".$type} = $self->record_cmd($type);
+ my $class = $model_base.'::'.ucfirst($type);
+ *{$calling_package."::cmd_".$type} = $self->record_cmd($type => $class);
}
}
@@ -142,11 +152,16 @@
}
+sub _get_record {
+ my $self = shift;
+ return $self->record_class->new( { handle => $self->handle, type => $self->type } );
+}
+
sub do_create {
my $cli = shift;
- my $record = Prophet::Record->new( handle => $cli->handle, type => $cli->type );
+ my $record = $cli->_get_record;
my ( $id, $results ) = $record->create( props => $cli->args );
- print "Created " . $cli->type . " " . $record->uuid . "\n";
+ print "Created " . $record->record_type . " " . $record->uuid . "\n";
}
@@ -158,7 +173,8 @@
die "Specify a regular expression and we'll search for records matching that regex";
}
- my $records = Prophet::Collection->new( handle => $cli->handle, type => $cli->type );
+ my $record = $cli->_get_record;
+ my $records = $record->collection_class->new( handle => $cli->handle, type => $record->record_type );
$records->matching(
sub {
my $item = shift;
@@ -176,7 +192,7 @@
sub do_update {
my $cli = shift;
- my $record = Prophet::Record->new( handle => $cli->handle, type => $cli->type );
+ my $record = $cli->_get_record;
$record->load( uuid => $cli->uuid );
$record->set_props( props => $cli->args );
@@ -185,7 +201,7 @@
sub do_delete {
my $cli = shift;
- my $record = Prophet::Record->new( handle => $cli->handle, type => $cli->type );
+ my $record = $cli->_get_record;
$record->load( uuid => $cli->uuid );
if ( $record->delete ) {
print $record->type . " " . $record->uuid . " deleted.\n";
@@ -198,7 +214,7 @@
sub do_show {
my $cli = shift;
- my $record = Prophet::Record->new( handle => $cli->handle, type => $cli->type );
+ my $record = $cli->_get_record;
$record->load( uuid => $cli->uuid );
print "id: " . $record->uuid . "\n";
my $props = $record->get_props();
@@ -212,9 +228,6 @@
my $cli = shift;
my $opts = $cli->args();
-
- warn $opts->{from};
- warn $opts->{to};
my $source = Prophet::Sync::Source->new( { url => $opts->{'from'} } );
my $target = Prophet::Sync::Source->new( { url => $opts->{'to'} } );
Modified: SVN-PropDB/lib/Prophet/Handle.pm
==============================================================================
--- SVN-PropDB/lib/Prophet/Handle.pm (original)
+++ SVN-PropDB/lib/Prophet/Handle.pm Wed Apr 2 21:38:16 2008
@@ -368,6 +368,7 @@
sub directory_for_type {
my $self = shift;
my %args = validate( @_, { type => 1 } );
+ Carp::cluck unless defined $args{type};
return join( "/", $self->db_root, $args{'type'} );
}
Modified: SVN-PropDB/lib/Prophet/Record.pm
==============================================================================
--- SVN-PropDB/lib/Prophet/Record.pm (original)
+++ SVN-PropDB/lib/Prophet/Record.pm Wed Apr 2 21:38:16 2008
@@ -23,6 +23,8 @@
my $UUIDGEN = Data::UUID->new();
+use constant collection_class => 'Prophet::Collection';
+
=head1 METHODS
=head2 new { handle => Prophet::Handle, type => $type }
@@ -39,6 +41,8 @@
return $self;
}
+sub record_type { $_[0]->type }
+
=head2 create { props => { %hash_of_kv_pairs } }
Creates a new Prophet database record in your database. Sets the record's properties to the keys and values passed in.
@@ -130,9 +134,11 @@
=cut
+
+
sub prop {
my $self = shift;
- my $prop = shift;
+ my $prop = shift;
return $self->get_props->{$prop};
}
More information about the Bps-public-commit
mailing list