File Coverage

blib/lib/Data/InputMonster/Util/Catalyst.pm
Criterion Covered Total %
statement 33 33 100.0
branch 4 6 66.6
condition n/a
subroutine 13 13 100.0
pod 4 4 100.0
total 54 56 96.4


line stmt bran cond sub pod time code
1 1     1   68146 use strict;
  1         9  
  1         23  
2 1     1   4 use warnings;
  1         2  
  1         34  
3             package Data::InputMonster::Util::Catalyst 0.006;
4             # ABSTRACT: InputMonster sources for common Catalyst sources
5              
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod This module exports a bunch of routines to make it easy to use
9             #pod Data::InputMonster with Catalyst. Each method, below, is also available as an
10             #pod exported subroutine, through the magic of Sub::Exporter.
11             #pod
12             #pod These sources will expect to receive the Catalyst object (C<$c>) as the
13             #pod C<$input> argument to the monster's C method.
14             #pod
15             #pod =cut
16              
17 1     1   4 use Carp ();
  1         2  
  1         14  
18 1     1   404 use Sub::Exporter::Util qw(curry_method);
  1         12142  
  1         7  
19 1         3 use Sub::Exporter -setup => {
20             exports => {
21             form_param => curry_method,
22             body_param => curry_method,
23             query_param => curry_method,
24             session_entry => curry_method,
25             }
26 1     1   220 };
  1         1  
27              
28             #pod =method form_param
29             #pod
30             #pod my $source = form_param($field_name);
31             #pod
32             #pod This source will look for form parameters (with C<< $c->req->params >>) with
33             #pod the given field name.
34             #pod
35             #pod =cut
36              
37             sub form_param {
38 1     1 1 6 my ($self, $field_name) = @_;
39             sub {
40 1 50   1   35 my $field_name = defined $field_name ? $field_name : $_[2]{field_name};
41 1         3 return $_[1]->req->params->{ $field_name };
42             }
43 1         5 }
44              
45             #pod =method body_param
46             #pod
47             #pod my $source = body_param($field_name);
48             #pod
49             #pod This source will look for form parameters (with C<< $c->req->body_params >>)
50             #pod with the given field name.
51             #pod
52             #pod =cut
53              
54             sub body_param {
55 1     1 1 5 my ($self, $field_name) = @_;
56             sub {
57 1 50   1   28 my $field_name = defined $field_name ? $field_name : $_[2]{field_name};
58 1         3 return $_[1]->req->body_params->{ $field_name };
59             }
60 1         8 }
61              
62             #pod =method query_param
63             #pod
64             #pod my $source = query_param($field_name);
65             #pod
66             #pod This source will look for form parameters (with C<< $c->req->query_params >>)
67             #pod with the given field name.
68             #pod
69             #pod =cut
70              
71             sub query_param {
72 2     2 1 92 my ($self, $field_name) = @_;
73             sub {
74 2 100   2   155 my $field_name = defined $field_name ? $field_name : $_[2]{field_name};
75 2         7 return $_[1]->req->query_params->{ $field_name };
76             }
77 2         15 }
78              
79             #pod =method session_entry
80             #pod
81             #pod my $source = session_entry($locator);
82             #pod
83             #pod This source will look for an entry in the session for the given locator, using
84             #pod the C utility from L.
85             #pod
86             #pod =cut
87              
88             sub session_entry {
89 3     3 1 16 my ($self, $locator) = @_;
90              
91 3         399 require Data::InputMonster::Util;
92 3         958 my $digger = Data::InputMonster::Util->dig($locator);
93              
94             return sub {
95 3     3   105 my ($monster, $input, $arg) = @_;
96 3         7 $monster->$digger($input->session, $arg);
97 3         51 };
98             }
99              
100             q{$C IS FOR CATALSYT, THAT'S GOOD ENOUGH FOR ME};
101              
102             __END__