| File: | lib/Railsish/Controller.pm |
| Coverage: | 54.1% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Railsish::Controller; | ||||||
| 2 | # ABSTRACT: base class for webapp controllers. | ||||||
| 3 | 8 8 8 | 73 25 85 | use strict; | ||||
| 4 | 8 8 8 | 78 25 64 | use warnings; | ||||
| 5 | |||||||
| 6 | 8 8 8 | 545 33 101 | use Railsish::CoreHelpers; | ||||
| 7 | 8 8 8 | 654 37 37 | use Railsish::ViewHelpers (); | ||||
| 8 | 8 8 8 | 688 104 39 | use Railsish::ControllerHelpers (); | ||||
| 9 | 8 8 8 | 549 33 119 | use Encode; | ||||
| 10 | 8 8 8 | 704 35 124 | use YAML qw(Dump); | ||||
| 11 | |||||||
| 12 | our ($request, $response, $controller, $action, $format, $params, $session); | ||||||
| 13 | |||||||
| 14 | 0 | 0 | 0 | sub request() { $request } | |||
| 15 | 10 | 0 | 72 | sub response() { $response } | |||
| 16 | 0 | 0 | 0 | sub controller() { $controller } | |||
| 17 | 0 | 0 | 0 | sub action() { $action } | |||
| 18 | 0 | 0 | 0 | sub format() { $format } | |||
| 19 | 7 | 0 | 57 | sub session() { $session } | |||
| 20 | |||||||
| 21 | sub params { | ||||||
| 22 | 6 | 0 | 25 | my $name = shift; | |||
| 23 | 6 | 104 | return defined($name) ? $params->{$name} : $params; | ||||
| 24 | } | ||||||
| 25 | |||||||
| 26 | sub import { | ||||||
| 27 | 7 | 42 | my $class = shift; | ||||
| 28 | 7 | 36 | my $caller = caller; | ||||
| 29 | 8 8 8 | 106 25 61 | no strict; | ||||
| 30 | |||||||
| 31 | 7 7 | 24 195 | push @{"$caller\::ISA"}, $class; | ||||
| 32 | |||||||
| 33 | 7 | 49 | for(qw(request response controller action format params | ||||
| 34 | session render render_json render_xml redirect_to)) { | ||||||
| 35 | 77 77 77 | 132 616 254 | *{"$caller\::$_"} = *{"$_"}; | ||||
| 36 | } | ||||||
| 37 | |||||||
| 38 | 7 | 41 | for (@Railsish::ControllerHelpers::EXPORT) { | ||||
| 39 | 7 7 7 | 22 91 70 | *{"$caller\::$_"} = *{"Railsish::ControllerHelpers::$_"}; | ||||
| 40 | } | ||||||
| 41 | } | ||||||
| 42 | |||||||
| 43 | 8 8 8 | 657 38 164 | use Template; | ||||
| 44 | 8 8 8 | 92 25 113 | use File::Spec::Functions; | ||||
| 45 | 8 8 8 | 628 494 152 | use Binding 0.04; | ||||
| 46 | 8 8 8 | 662 37 146 | use Perl6::Junction qw(any); | ||||
| 47 | 8 8 8 | 697 33 34 | use Railsish::PathHelpers (); | ||||
| 48 | |||||||
| 49 | sub build_stash { | ||||||
| 50 | 0 | 0 | 0 | my $caller_vars = Binding->of_caller(2)->our_vars; | |||
| 51 | 0 | 0 | my $stash = {}; | ||||
| 52 | 0 | 0 | for my $varname (keys %$caller_vars) { | ||||
| 53 | 0 | 0 | my $val = $caller_vars->{$varname}; | ||||
| 54 | 0 | 0 | $varname =~ s/^[\$%@]//; | ||||
| 55 | 0 | 0 | $val = $$val if ref($val) eq any('SCALAR', 'REF'); | ||||
| 56 | 0 | 0 | $stash->{$varname} = $val; | ||||
| 57 | } | ||||||
| 58 | 0 | 0 | return $stash; | ||||
| 59 | } | ||||||
| 60 | |||||||
| 61 | sub render { | ||||||
| 62 | 0 | 0 | 0 | my (%variables) = @_; | |||
| 63 | 0 | 0 | my $stash = build_stash; | ||||
| 64 | |||||||
| 65 | 0 | 0 | for (keys %$stash) { | ||||
| 66 | 0 | 0 | $variables{$_} = $stash->{$_}; | ||||
| 67 | } | ||||||
| 68 | |||||||
| 69 | 0 | 0 | if (defined($format)) { | ||||
| 70 | 0 | 0 | my $renderer = __PACKAGE__->can("render_${format}"); | ||||
| 71 | 0 | 0 | if ($renderer) { | ||||
| 72 | 0 | 0 | $renderer->(%variables); | ||||
| 73 | 0 | 0 | return; | ||||
| 74 | } | ||||||
| 75 | 0 | 0 | $response->status(500); | ||||
| 76 | 0 | 0 | $response->body("Unknown format: $format"); | ||||
| 77 | } | ||||||
| 78 | |||||||
| 79 | 0 | 0 | $variables{controller} = \&controller; | ||||
| 80 | 0 | 0 | $variables{action} = \&action; | ||||
| 81 | |||||||
| 82 | 0 | 0 | for (@Railsish::ViewHelpers::EXPORT) { | ||||
| 83 | 0 0 | 0 0 | $variables{$_} = \&{"Railsish::ViewHelpers::$_"}; | ||||
| 84 | } | ||||||
| 85 | |||||||
| 86 | 0 | 0 | my $path_helpers = Railsish::PathHelpers->as_hash; | ||||
| 87 | 0 | 0 | for (keys %$path_helpers) { | ||||
| 88 | 0 | 0 | $variables{$_} = $path_helpers->{$_} | ||||
| 89 | } | ||||||
| 90 | |||||||
| 91 | 0 | 0 | $variables{title} ||= ucfirst($controller) . " :: " .ucfirst($action); | ||||
| 92 | 0 | 0 | $variables{layout} ||= "layouts/application.html.tt2"; | ||||
| 93 | 0 | 0 | $variables{template} ||= "${controller}/${action}.html.tt2"; | ||||
| 94 | |||||||
| 95 | 0 | 0 | my $tt = Template->new({ | ||||
| 96 | INCLUDE_PATH => [ catdir(app_root, "app", "views") ], | ||||||
| 97 | PROCESS => $variables{layout}, | ||||||
| 98 | ENCODING => 'utf8' | ||||||
| 99 | }); | ||||||
| 100 | |||||||
| 101 | 0 | 0 | my $output = ""; | ||||
| 102 | 0 | 0 | $tt->process($variables{template}, \%variables, \$output) | ||||
| 103 | || die $tt->error(); | ||||||
| 104 | |||||||
| 105 | 0 | 0 | $response->body(Encode::encode_utf8($output)); | ||||
| 106 | } | ||||||
| 107 | |||||||
| 108 | |||||||
| 109 | 8 8 8 | 729 34 119 | use JSON -convert_blessed_universally; | ||||
| 110 | sub render_json { | ||||||
| 111 | 0 | 0 | 0 | my %variables = @_; | |||
| 112 | |||||||
| 113 | 0 | 0 | my $json = JSON->new; | ||||
| 114 | 0 | 0 | $json->allow_blessed(1); | ||||
| 115 | |||||||
| 116 | 0 | 0 | my $out = $json->encode(\%variables); | ||||
| 117 | |||||||
| 118 | 0 | 0 | $response->headers->header('Content-Type' => 'text/x-json'); | ||||
| 119 | 0 | 0 | $response->body( Encode::encode_utf8($out) ); | ||||
| 120 | } | ||||||
| 121 | |||||||
| 122 | 8 8 8 | 782 35 96 | use XML::Simple; | ||||
| 123 | sub render_xml { | ||||||
| 124 | # still not write testing | ||||||
| 125 | |||||||
| 126 | 6 | 0 | 24 | my %variables = @_; | |||
| 127 | |||||||
| 128 | 0 | 0 | my $out = XMLout(\%variables); | ||||
| 129 | |||||||
| 130 | 0 | 0 | $response->header->header("Content-Type" => "text/xml"); | ||||
| 131 | 0 | 0 | $response->body( Encode::encode_utf8($out) ); | ||||
| 132 | } | ||||||
| 133 | |||||||
| 134 | 8 8 8 | 603 49 112 | use Railsish::TextHelpers; | ||||
| 135 | |||||||
| 136 | sub redirect_to { | ||||||
| 137 | 2 | 0 | 15 | my @args = @_; | |||
| 138 | 2 | 8 | my $url; | ||||
| 139 | 2 | 9 | my $current_controller = caller; | ||||
| 140 | |||||||
| 141 | 2 | 14 | $current_controller = underscore($current_controller); | ||||
| 142 | 2 | 249 | $current_controller =~ s/_controller$//; | ||||
| 143 | |||||||
| 144 | 2 | 18 | if (@args == 1) { | ||||
| 145 | 1 | 5 | $url = $args[0]; | ||||
| 146 | } | ||||||
| 147 | elsif (@args % 2 == 0) { | ||||||
| 148 | 1 | 8 | my %args = (@args); | ||||
| 149 | 1 | 8 | $args{controller} ||= $current_controller; | ||||
| 150 | 1 | 14 | $url = Railsish::Router->uri_for(%args); | ||||
| 151 | } | ||||||
| 152 | else { | ||||||
| 153 | 0 | 0 | die("Unknown redirect_to parameters: @args"); | ||||
| 154 | } | ||||||
| 155 | 2 | 21 | $response->status(302); | ||||
| 156 | 2 | 114 | $response->header(Location => $url); | ||||
| 157 | } | ||||||
| 158 | |||||||
| 159 | # Provide a default 'index' | ||||||
| 160 | sub index { | ||||||
| 161 | 0 | 0 | render; | ||||
| 162 | } | ||||||
| 163 | |||||||
| 164 | 1; | ||||||