summaryrefslogtreecommitdiffstats
path: root/lib/Term/Filter/Callback.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Term/Filter/Callback.pm')
-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;