[Bps-public-commit] r14295 - in Prophet/trunk: lib/Prophet/CLI/Command t t/lib/TestApp

jesse at bestpractical.com jesse at bestpractical.com
Fri Jul 18 18:15:23 EDT 2008


Author: jesse
Date: Fri Jul 18 18:15:23 2008
New Revision: 14295

Added:
   Prophet/trunk/t/testing.conf
Modified:
   Prophet/trunk/lib/Prophet/CLI/Command/Search.pm
   Prophet/trunk/lib/Prophet/CLI/Command/Show.pm
   Prophet/trunk/lib/Prophet/Record.pm
   Prophet/trunk/lib/Prophet/Test.pm
   Prophet/trunk/t/lib/TestApp/Bug.pm
   Prophet/trunk/t/test_app.conf

Log:
* Updated the listings format to allow customization
* rejiggered some of the colorization


Modified: Prophet/trunk/lib/Prophet/CLI/Command/Search.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI/Command/Search.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Search.pm	Fri Jul 18 18:15:23 2008
@@ -53,12 +53,7 @@
     $records->matching($search_cb);
 
     for ( sort { $a->luid <=> $b->luid } $records->items ) {
-        if ( $_->summary_props ) {
             print $_->format_summary . "\n";
-        } else {
-            # XXX OLD HACK TO MAKE TESTS PASS
-            printf( "%s %s %s \n", $_->uuid, $_->prop('summary') || "(no summary)", $_->prop('status')  || '(no status)' );
-        }
     }
 }
 

Modified: Prophet/trunk/lib/Prophet/CLI/Command/Show.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI/Command/Show.pm	(original)
+++ Prophet/trunk/lib/Prophet/CLI/Command/Show.pm	Fri Jul 18 18:15:23 2008
@@ -1,5 +1,6 @@
 package Prophet::CLI::Command::Show;
 use Moose;
+use Params::Validate;
 extends 'Prophet::CLI::Command';
 with 'Prophet::CLI::RecordCommand';
 
@@ -7,12 +8,93 @@
     my $self = shift;
 
     my $record = $self->_load_record;
-    print $record->stringify_props(
+    print $self->stringify_props(
+        record => $record,
         batch   => $self->has_arg('batch'),
         verbose => $self->has_arg('verbose'),
     );
 }
 
+=head2 stringify_props
+
+Returns a stringified form of the properties suitable for displaying directly
+to the user. Also includes luid and uuid.
+
+You may define a "color_prop" method which transforms a property name and value
+(by adding color).
+
+You may also define a "color_prop_foo" method which transforms values of
+property "foo" (by adding color).
+
+=cut
+
+sub stringify_props {
+    my $self = shift;
+    my %args = validate( @_, {record => { ISA => 'Prophet::Record'},
+                            batch =>  1,
+                            verbose => 1});
+
+    my $record = $args{'record'};
+    my $props = $record->get_props;
+
+    my $colorize = $args{'batch'} ? 0 : 1;
+
+    # which props are we going to display?
+    my @show_props;
+    if ($record->can('props_to_show')) {
+        @show_props = $record->props_to_show(\%args);
+
+        # if they ask for verbosity, then display all the other fields
+        # after the fields that our subclass wants to show
+        if ($args{verbose}) {
+            my %already_shown = map { $_ => 1 } @show_props;
+            push @show_props, grep { !$already_shown{$_} }
+                              keys %$props;
+        }
+    }
+    else {
+        @show_props = ('id', keys %$props);
+    }
+
+    # kind of ugly but it simplifies the code
+    $props->{id} = $record->luid ." (" . $record->uuid . ")";
+
+    my $max_length = 0;
+    my @fields;
+
+    for my $field (@show_props) {
+        my $value = $props->{$field};
+
+        # don't bother displaying unset fields
+        next if !defined($value);
+
+        # color if we can (and should)
+        my ($colorized_field, $colorized_value) = ($field, $value);
+        if ($colorize) {
+            ($colorized_field,$colorized_value) = $record->colorize($field => $value);
+
+    }
+        push @fields, [$field, $colorized_field, $colorized_value];
+
+        # don't check length($field) here, since coloring will increase the
+        # length but we only care about display length
+        $max_length = length($field)
+            if length($field) > $max_length;
+    }
+
+    $max_length = 0 if $args{batch};
+
+    # this code is kind of ugly. we need to format based on uncolored length
+    return join '',
+           map {
+               my ($field, $colorized_field, $colorized_value) = @$_;
+               $colorized_field .= ':';
+               $colorized_field .= ' ' x ($max_length - length($field));
+               "$colorized_field $colorized_value\n"
+           }
+           @fields;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Modified: Prophet/trunk/lib/Prophet/Record.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Record.pm	(original)
+++ Prophet/trunk/lib/Prophet/Record.pm	Fri Jul 18 18:15:23 2008
@@ -356,25 +356,50 @@
 
 =cut
 
-use constant summary_format => '%u';
-use constant summary_props  => ();
+
+sub _default_summary_format { 'No summary format defined for this record type' }
 
 sub format_summary {
     my $self   = shift;
-    my $format = $self->summary_format;
-    if ( $format =~ /%u/ ) {
-        my $uuid = $self->uuid;
-        $format =~ s/%u/$uuid/g;
-    }
-    if ( $format =~ /%l/ ) {
-        my $luid = sprintf('%s',$self->luid);
-        $format =~ s/%l/$luid/g;
+
+    my $configured_format =
+         $self->app_handle->config->get('summary_format_'.$self->type) 
+         || $self->app_handle->config->get('default_summary_format')
+         || $self->_default_summary_format ;
+    my $props = $self->get_props;
+
+    my @out;
+    foreach my $atom(split(/\s*\|\s*/,$configured_format)) {
+            my ($format_string,$prop);
+            if ($atom =~ /,/) {
+                  ($format_string,$prop) = split(/,/,$atom);
+                $prop  = ($props->{$prop} || "(no $prop)") unless ($prop =~ /^\$/);
+            } else {
+                $format_string = '%s';
+                $prop = $atom;
+            }
+            push @out, $self->format_atom( $format_string => $prop);
     }
-    return sprintf( $format,
-        map { $self->prop($_) || "(no $_)" } $self->summary_props );
+    return join(' ', @out);
 
 }
 
+sub format_atom {
+    my $self = shift;
+    my $string = shift;
+    my $value_in = shift;
+    my $value;
+    if ($value_in =~ /^\$[gu]uid/) {
+            $value = $self->uuid;
+    } elsif ($value_in eq '$luid') {
+            $value = $self->luid;
+    } else {
+            $value = $value_in;
+    }
+    return sprintf($string, $value);
+}
+
+
 =head2 find_or_create_luid
 
 Finds the luid for the records uuid, or creates a new one. Returns the luid.
@@ -388,86 +413,24 @@
     return $luid;
 }
 
-=head2 stringify_props
-
-Returns a stringified form of the properties suitable for displaying directly
-to the user. Also includes luid and uuid.
 
-You may define a "color_prop" method which transforms a property name and value
-(by adding color).
 
-You may also define a "color_prop_foo" method which transforms values of
-property "foo" (by adding color).
 
-=cut
-
-sub stringify_props {
-    my $self = shift;
-    my %args = @_;
+sub colorize {
+        my $self = shift;
+        my ($field, $value) = @_;
+        my $colorized_field;
+        my $colorized_value;
 
-    my $props = $self->get_props;
-
-    # which props are we going to display?
-    my @show_props;
-    if ($self->can('props_to_show')) {
-        @show_props = $self->props_to_show(\%args);
-
-        # if they ask for verbosity, then display all the other fields
-        # after the fields that our subclass wants to show
-        if ($args{verbose}) {
-            my %already_shown = map { $_ => 1 } @show_props;
-            push @show_props, grep { !$already_shown{$_} }
-                              keys %$props;
-        }
-    }
-    else {
-        @show_props = ('id', keys %$props);
-    }
-
-    # kind of ugly but it simplifies the code
-    $props->{id} = $self->luid ." (" . $self->uuid . ")";
-
-    my $max_length = 0;
-    my @fields;
-
-    for my $field (@show_props) {
-        my $value = $props->{$field};
-
-        # don't bother displaying unset fields
-        next if !defined($value);
-
-        # color if we can (and should)
-        my ($color_field, $color_value) = ($field, $value);
-        if (!$args{batch}) {
-            if ($self->can("color_prop_$field")) {
-                my $method = "color_prop_$field";
-                $color_value = $self->$method($value);
+            if (my $method = $self->can("color_prop_$field")) {
+                $colorized_value = $self->$method($value);
             }
             else {
-                ($color_field, $color_value) = $self->color_prop($field, $value);
+                ($colorized_field, $colorized_value) = $self->color_prop($field, $value);
             }
+            return ($colorized_field, $colorized_value)
         }
 
-        push @fields, [$field, $color_field, $color_value];
-
-        # don't check length($field) here, since coloring will increase the
-        # length but we only care about display length
-        $max_length = length($field)
-            if length($field) > $max_length;
-    }
-
-    $max_length = 0 if $args{batch};
-
-    # this code is kind of ugly. we need to format based on uncolored length
-    return join '',
-           map {
-               my ($field, $color_field, $color_value) = @$_;
-               $color_field .= ':';
-               $color_field .= ' ' x ($max_length - length($field));
-               "$color_field $color_value\n"
-           }
-           @fields;
-}
 
 =head2 color_prop property, value
 

Modified: Prophet/trunk/lib/Prophet/Test.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Test.pm	(original)
+++ Prophet/trunk/lib/Prophet/Test.pm	Fri Jul 18 18:15:23 2008
@@ -46,6 +46,7 @@
         no warnings 'redefine';
         *Test::Builder::plan = sub { };
     }
+$ENV{'PROPHET_APP_CONFIG'} = 't/testing.conf';
 }
 
 {
@@ -127,6 +128,9 @@
 sub _mk_cmp_closure {
     my ( $exp, $err ) = @_;
     my $line = 0;
+
+    $exp = ref($exp) eq 'ARRAY' ? $exp : [$exp];
+
     sub {
         my $output = shift;
         chomp $output;

Modified: Prophet/trunk/t/lib/TestApp/Bug.pm
==============================================================================
--- Prophet/trunk/t/lib/TestApp/Bug.pm	(original)
+++ Prophet/trunk/t/lib/TestApp/Bug.pm	Fri Jul 18 18:15:23 2008
@@ -2,8 +2,12 @@
 use strict;
 
 package TestApp::Bug;
+use Moose;
+extends 'Prophet::Record';
+
 use base qw/Prophet::Record/;
 
+
 sub new { shift->SUPER::new( @_, type => 'bug' ) }
 
 sub validate_prop_name {

Modified: Prophet/trunk/t/test_app.conf
==============================================================================
--- Prophet/trunk/t/test_app.conf	(original)
+++ Prophet/trunk/t/test_app.conf	Fri Jul 18 18:15:23 2008
@@ -1,5 +1,4 @@
 #This is not a config directive
 foo=bar
 # nor is this
-
 re = rawr

Added: Prophet/trunk/t/testing.conf
==============================================================================
--- (empty file)
+++ Prophet/trunk/t/testing.conf	Fri Jul 18 18:15:23 2008
@@ -0,0 +1 @@
+default_summary_format = %s,$uuid | %s,summary | %s,status



More information about the Bps-public-commit mailing list