| 1 | package WWW::Pipeline::Services::RunModes; | ||
| 2 | $VERSION = '0.1'; | ||
| 3 | |||
| 4 | #-- pragmas ---------------------------- | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | =head1 WWW::Pipeline::Services::RunModes | ||
| 9 | |||
| 10 | This plugin is an implementation of the Run Mode paradigm as seen in Jesse | ||
| 11 | Erlbaum's CGI::Application module. | ||
| 12 | |||
| 13 | The quick summary of the Run Mode concept is that a web application will | ||
| 14 | typically need one function for each screen of output. Which screen to produce | ||
| 15 | is determined by the data in the request received. Rather than writing a very | ||
| 16 | large set of if/else statements or a switch statement, the Run Mode concept uses | ||
| 17 | a hash table of named callbacks to represent the possible responses, and a | ||
| 18 | decision mechanism generates the key to the hash which determines which run mode | ||
| 19 | is run. The decision mechanism may be as simple as a CGI parameter, or as | ||
| 20 | complex as parsing different parts of the request, paired with authorization | ||
| 21 | data. | ||
| 22 | |||
| 23 | One thing that this plugin does not replicate, however, is the run mode decision | ||
| 24 | mechanism. Deciding which run mode to use can be done in many ways, and would | ||
| 25 | best be done by a handler (or two - in the case where the run mode may be | ||
| 26 | changed according to user authorization) installed during the ParseRequest phase | ||
| 27 | of the Pipeline. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | #== Plugin ===================================================================== | ||
| 32 | |||
| 33 | sub load { | ||
| 34 | my( $class, $pipeline ) = @_; | ||
| 35 | |||
| 36 | my $self = bless {}, $class; | ||
| 37 | |||
| 38 | $pipeline->addServices( | ||
| 39 | run_modes => sub { $self->run_modes(@_) }, | ||
| 40 | mode => sub { $self->mode(@_) } | ||
| 41 | ); | ||
| 42 | |||
| 43 | $pipeline->addHandler( 'GenerateResponse', \&executeRunMode ); | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | #== Services =================================================================== | ||
| 49 | |||
| 50 | =head2 Services | ||
| 51 | |||
| 52 | =head3 run_modes | ||
| 53 | |||
| 54 | #set: | ||
| 55 | $self->run_modes( | ||
| 56 | one => \&doSomething, | ||
| 57 | two => 'doSomethingElse' | ||
| 58 | ); | ||
| 59 | |||
| 60 | #retrieve | ||
| 61 | my $method = $self->run_modes('one'); | ||
| 62 | |||
| 63 | #reassign: | ||
| 64 | $self->run_modes('one' => \&someOtherMethod ); | ||
| 65 | |||
| 66 | #delete: | ||
| 67 | $self->run_modes( two => undef ); | ||
| 68 | |||
| 69 | This service stores and manipulates a list of run modes to be used by the | ||
| 70 | plugin's GenerateResponse handler. Run Modes may either be specified by name | ||
| 71 | or by subroutine reference. Specifying by name makes way for inheritence, while | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub run_modes { | ||
| 76 | |||
| 77 | my( $self, $pipeline, @params ) = @_; | ||
| 78 | |||
| 79 | if( @params == 1 ) { | ||
| 80 | return defined $self->{_run_modes}{$params[0]} | ||
| 81 | ? $self->{_run_modes}{$params[0]} | ||
| 82 | : undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | die "run_modes expects a hash of run modes if you pass it more than one parameter" | ||
| 86 | if @params % 2 != 0; | ||
| 87 | |||
| 88 | my %modes = @params; | ||
| 89 | while( my( $key,$mode ) = each %modes ) { | ||
| 90 | if( defined $mode ) { | ||
| 91 | $self->{_run_modes}{$key} = $mode; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | delete $self->{_run_modes}{$key}; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | #------------------------------------------------------------------------------- | ||
| 100 | |||
| 101 | =head3 mode | ||
| 102 | |||
| 103 | #set | ||
| 104 | $self->mode('one'); | ||
| 105 | |||
| 106 | #retrieve | ||
| 107 | $self->mode(); | ||
| 108 | |||
| 109 | This service stores the decision made by the application as to which run mode is | ||
| 110 | to be run during the GenerateResponse phase. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub mode { | ||
| 115 | |||
| 116 | my( $self, $pipeline, $value ) = @_; | ||
| 117 | |||
| 118 | if( @_ == 3 ) { | ||
| 119 | return $self->{_mode} = $value; | ||
| 120 | } | ||
| 121 | |||
| 122 | return $self->{_mode}; | ||
| 123 | } | ||
| 124 | |||
| 125 | #== Handlers =================================================================== | ||
| 126 | |||
| 127 | =head2 Handlers | ||
| 128 | |||
| 129 | =head3 executeRunMode | ||
| 130 | |||
| 131 | this handler, installed during the GenerateResponse phase, takes the information | ||
| 132 | provided to the C<run_modes> and C<mode> services and determines what method to | ||
| 133 | execute. It then runs that run mode, capturing its output. It expects that the | ||
| 134 | output the method returns is the content it wants to return to the user, and | ||
| 135 | stores it via the application's C<response> service for later retrieval. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub executeRunMode { | ||
| 140 | my $pipeline = shift; | ||
| 141 | my $method = $pipeline->run_modes( $pipeline->mode() ); | ||
| 142 | my $response = defined $pipeline->response() | ||
| 143 | ? $pipeline->response() | ||
| 144 | : ''; | ||
| 145 | |||
| 146 | if( ref $method eq 'CODE' ) { | ||
| 147 | |||
| 148 | $response .= $pipeline->$method(); | ||
| 149 | } | ||
| 150 | elsif($pipeline->can($method)) { | ||
| 151 | $response .= $pipeline->$method(); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | die "pipeline can't run method '$method'"; | ||
| 155 | } | ||
| 156 | |||
| 157 | $pipeline->response($response); | ||
| 158 | } | ||
| 159 | |||
| 160 | #======== | ||
| 161 | 1; | ||
| 162 | |||
| 163 | =head2 Authors | ||
| 164 | |||
| 165 | Stephen Howard <stephen@thunkit.com> | ||
| 166 | |||
| 167 | =head2 License | ||
| 168 | |||
| 169 | This module may be distributed under the same terms as Perl itself. | ||
| 170 | |||
| 171 | =cut |