[Bps-public-commit] r16122 - in Prophet/branches/dispatcher: lib/Prophet lib/Prophet/CLI
sartak at bestpractical.com
sartak at bestpractical.com
Sat Sep 27 17:16:26 EDT 2008
Author: sartak
Date: Sat Sep 27 17:16:26 2008
New Revision: 16122
Modified:
Prophet/branches/dispatcher/ (props changed)
Prophet/branches/dispatcher/lib/Prophet/CLI/Dispatcher.pm
Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm
Log:
r72991 at onn: sartak | 2008-09-27 17:15:45 -0400
search command
Modified: Prophet/branches/dispatcher/lib/Prophet/CLI/Dispatcher.pm
==============================================================================
--- Prophet/branches/dispatcher/lib/Prophet/CLI/Dispatcher.pm (original)
+++ Prophet/branches/dispatcher/lib/Prophet/CLI/Dispatcher.pm Sat Sep 27 17:16:26 2008
@@ -293,6 +293,16 @@
print "Publish complete.\n";
};
+on search => sub {
+ my $self = shift;
+
+ my $records = $self->context->get_collection_object;
+ my $search_cb = $self->context->get_search_callback;
+ $records->matching($search_cb);
+
+ $self->display_collection($records);
+};
+
# catch-all. () makes sure we don't hit the annoying historical feature of
# the empty regex meaning the last-used regex
on qr/()/ => sub {
@@ -609,6 +619,16 @@
return $ret;
}
+
+sub display_collection {
+ my $self = shift;
+ my $items = shift;
+
+ for (sort { $a->luid <=> $b->luid } @$items) {
+ print $_->format_summary . "\n";
+ }
+}
+
no Moose;
__PACKAGE__->meta->make_immutable;
Modified: Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm
==============================================================================
--- Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm (original)
+++ Prophet/branches/dispatcher/lib/Prophet/CLIContext.pm Sat Sep 27 17:16:26 2008
@@ -381,6 +381,100 @@
}
}
+=head2 get_search_callback
+
+Returns a callback used for filtering a collection based on properties and
+arguments.
+
+=cut
+
+sub get_search_callback {
+ my $self = shift;
+
+ my %prop_checks;
+ for my $check ($self->prop_set) {
+ push @{ $prop_checks{ $check->{prop} } }, $check;
+ }
+
+ my $regex = $self->arg('regex');
+
+ return sub {
+ my $item = shift;
+ my $props = $item->get_props;
+ my $did_limit = 0;
+
+ if ($self->prop_names > 0) {
+ $did_limit = 1;
+
+ for my $prop (keys %prop_checks) {
+ my $got = $props->{$prop};
+ my $ok = 0;
+ for my $check (@{ $prop_checks{$prop} }) {
+ $ok = 1
+ if $self->cmp_ok($check->{value}, $check->{cmp}, $got);
+ }
+ return 0 if !$ok;
+ }
+ }
+
+ # if they specify a regex, it must match
+ if ($regex) {
+ $did_limit = 1;
+ my $ok = 0;
+
+ for (values %$props) {
+ if (/$regex/) {
+ $ok = 1;
+ last;
+ }
+ }
+ return 0 if !$ok;
+ }
+
+ return $self->default_match($item) if !$did_limit;
+
+ return 1;
+ };
+}
+
+=head2 default_match
+
+By default, items are included in the list; you don't need to explicitly match
+them.
+
+=cut
+
+sub default_match { 1 }
+
+=head2 cmp_ok expected, cmp, got
+
+Returns 0 or 1 based on whether the comparison succeeded or not. (I hate having
+syntax! ;))
+
+=cut
+
+sub cmp_ok {
+ my $self = shift;
+ my ($expected, $cmp, $got) = @_;
+
+ $got = '' if !defined($got); # avoid undef warnings
+
+ if ($cmp eq '=') {
+ return 0 unless $got eq $expected;
+ }
+ elsif ($cmp eq '=~') {
+ return 0 unless $got =~ $expected;
+ }
+ elsif ($cmp eq '!=' || $cmp eq '<>' || $cmp eq 'ne') {
+ return 0 if $got eq $expected;
+ }
+ elsif ($cmp eq '!~') {
+ return 0 if $got =~ $expected;
+ }
+
+ return 1;
+}
+
__PACKAGE__->meta->make_immutable;
no Moose;
More information about the Bps-public-commit
mailing list