summaryrefslogtreecommitdiffstats
path: root/lib/Term/Filter
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-03-05 22:56:18 -0600
committerJesse Luehrs <doy@tozt.net>2012-03-05 22:56:18 -0600
commit0f6d95e7959d2978a869b373649fc5e49f733159 (patch)
tree10700bfb03f62a2f9e414f236c488c70d3b7b1d1 /lib/Term/Filter
parent1ac156b50b0262a7bfcce77eb54f2d627d45d2ec (diff)
downloadterm-filter-0f6d95e7959d2978a869b373649fc5e49f733159.tar.gz
term-filter-0f6d95e7959d2978a869b373649fc5e49f733159.zip
factor out the callback stuff, make Term::Filter a role
Diffstat (limited to 'lib/Term/Filter')
-rw-r--r--lib/Term/Filter/Callback.pm56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/Term/Filter/Callback.pm b/lib/Term/Filter/Callback.pm
new file mode 100644
index 0000000..4a32352
--- /dev/null
+++ b/lib/Term/Filter/Callback.pm
@@ -0,0 +1,56 @@
+package Term::Filter::Callback;
+use Moose;
+# ABSTRACT: Simple callback-based wrapper for L<Term::Filter>
+
+with 'Term::Filter';
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=cut
+
+=attr callbacks
+
+=cut
+
+has callbacks => (
+ is => 'ro',
+ isa => 'HashRef[CodeRef]',
+ default => sub { {} },
+);
+
+sub _callback {
+ my $self = shift;
+ my ($event, @args) = @_;
+ my $callback = $self->callbacks->{$event};
+ return unless $callback;
+ return $self->$callback(@args);
+}
+
+sub _has_callback {
+ my $self = shift;
+ my ($event) = @_;
+ return exists $self->callbacks->{$event};
+}
+
+for my $method (qw(setup cleanup munge_input munge_output
+ read read_error winch)) {
+ __PACKAGE__->meta->add_around_method_modifier(
+ $method => sub {
+ my $orig = shift;
+ my $self = shift;
+ if ($self->_has_callback($method)) {
+ return $self->_callback($method, @_);
+ }
+ else {
+ return $self->$orig(@_);
+ }
+ },
+ );
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;