subclassing Bread::Board::Service to replace _filter_component
André Walker [Tue, 5 Jul 2011 19:13:24 +0000 (16:13 -0300)]
lib/Catalyst/ConstructorInjection.pm [new file with mode: 0644]
lib/Catalyst/Service/WithContext.pm [new file with mode: 0644]

diff --git a/lib/Catalyst/ConstructorInjection.pm b/lib/Catalyst/ConstructorInjection.pm
new file mode 100644 (file)
index 0000000..15756a4
--- /dev/null
@@ -0,0 +1,11 @@
+package Catalyst::ConstructorInjection;
+use Moose;
+
+extends 'Bread::Board::ConstructorInjection';
+
+with 'Catalyst::Service::WithContext';
+
+__PACKAGE__->meta->make_immutable;
+
+no Moose;
+1;
diff --git a/lib/Catalyst/Service/WithContext.pm b/lib/Catalyst/Service/WithContext.pm
new file mode 100644 (file)
index 0000000..96ebbc8
--- /dev/null
@@ -0,0 +1,28 @@
+package Catalyst::Service::WithContext;
+use Moose::Role;
+
+use Bread::Board::Types;
+
+with 'Bread::Board::Service';
+
+has accept_context_sub => (
+    is => 'ro',
+    isa => 'Str',
+    default => 'ACCEPT_CONTEXT',
+);
+
+around 'get' => sub {
+    my ( $orig, $self, @params ) = @_;
+
+    my $instance = $self->$orig();
+    my $ac_sub   = $self->accept_context_sub;
+
+    if ( $instance->can($ac_sub) ) {
+        return $instance->$ac_sub( @params );
+    }
+
+    return $instance;
+};
+
+no Moose::Role;
+1;