File Coverage

blib/lib/Catalyst/TraitFor/Controller/Ping.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Catalyst::TraitFor::Controller::Ping;
2             our $VERSION = '0.001';
3              
4             #ABSTRACT: Provides a ping action for consuming controllers
5              
6 1     1   1409 use MooseX::MethodAttributes::Role;
  0            
  0            
7             use namespace::autoclean;
8             use Try::Tiny;
9              
10              
11             has model_name => (isa => 'Str', is => 'ro', predicate => 'has_model_name', clearer => '_clear_model_name', writer => '_set_model_name');
12              
13              
14             has model_method => (isa => 'Str', is => 'ro', predicate => 'has_model_method', clearer => '_clear_model_method', writer => '_set_model_method');
15              
16              
17             has model_method_arguments => (isa => 'ArrayRef', is => 'ro', predicate => 'has_model_method_arguments', clearer => '_clear_model_method_arguments', writer => '_set_model_method_arguments');
18              
19              
20             sub ping :Local
21             {
22             my ($self, $c) = @_;
23            
24             if($self->has_model_name)
25             {
26             my $model = $c->model($self->model_name);
27              
28             if(!defined($model))
29             {
30             $c->error("Unable to find model '${\$self->model_name}'");
31             $c->detach();
32             }
33             elsif($self->has_model_method)
34             {
35             my $args;
36              
37             if($self->has_model_method_arguments)
38             {
39             $args = $self->model_method_arguments;
40             }
41              
42             try
43             {
44             $model->${\$self->model_method}(@$args);
45             }
46             catch
47             {
48             $c->error("Problem calling '${\$self->model_method}' on '${\$self->model_name}': $_");
49             $c->detach();
50             }
51             }
52             }
53             }
54              
55             1;
56              
57              
58             =pod
59              
60             =head1 NAME
61              
62             Catalyst::TraitFor::Controller::Ping - Provides a ping action for consuming controllers
63              
64             =head1 VERSION
65              
66             version 0.001
67              
68             =head1 SYNOPSIS
69              
70             package MyApp::Controller::Foo;
71             use Moose;
72             use namespace::autoclean;
73             BEGIN { extends 'Catalyst::Controller' }
74              
75             with 'Catalyst::TraitFor::Controller::Ping';
76              
77             __PACKAGE__->config
78             (
79             {
80             model_name => 'SomeModel',
81             model_method => 'some_method',
82             model_method_arguments => [qw/ one two three /],
83             }
84             );
85              
86             ...
87              
88             =head1 DESCRIPTION
89              
90             Ever wanted to monitor a web app? With this simple role, you can easily add an action to L</ping> to test if the app is up and running. You can even define a L</model_name> and a L</model_method> to call so it perihperally tests the app's connection to the database (or some other resource). Simply add exceptions for L</ping> in your ACL, and you're good to go.
91              
92             =head1 PUBLIC_ATTRIBUTES
93              
94             =head2 model_name
95              
96             isa: Str, is: ro
97              
98             Define a model name to access via $c->model();
99              
100             =head2 model_method
101              
102             isa: Str, is: ro
103              
104             Define a method name to call upon the model
105              
106             =head2 model_method_arguments
107              
108             isa: ArrayRef, is: ro
109              
110             Define arguments to pass to the method upon the model
111              
112             =head1 PUBLIC_METHODS
113              
114             =head2 ping
115              
116             :Local
117              
118             ping is an action added to which ever controller consumes this role that simply returns. If a L</model_name> is configured, the model will be gathered via $c->model(). If L</model_method> is configured, that method will be called upon the retrived model. If L</model_method_arguments> are provided, they will be passed to the model method. The return value is discarded. Only that the method executed without exception matters for ping. Ping will return no content so it doesn't forward to views or anything else.
119              
120             =head1 AUTHOR
121              
122             Nicholas Perez <nperez@cpan.org>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2010 by Infinity Interactive, Inc..
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut
132              
133              
134             __END__
135