Jifty/Param/Schema.pm

filter: Statementstmt 44.6% 42 / 94 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 51.8% 14 / 27 Podpod n/a totaltotal 38.1% 56 / 147 CC 17 SCAR 29.7
CC
17
Cov
36%
merge_params
56.0
schema
47.3
__ANON__
6.0
CRAP 19.5
« Jifty/Plugin/Halo/Mason.pm ↑ Summary Jifty/Web/Session/ClientSide.pm »
1 package Jifty::Param::Schema;
2  
3 =head1 NAME
4  
5 Jifty::Param::Schema - Declare parameters of a Jifty action with ease.
6  
7 =head1 SYNOPSIS
8  
9 package MyApp::Action::Login;
10 use Jifty::Param::Schema;
11 use Jifty::Action schema {
12  
13 param email =>
14 label is 'Email address',
15 is mandatory,
16 ajax validates;
17  
18 param password =>
19 type is 'password',
20 label is 'Password',
21 is mandatory;
22  
23 param remember =>
24 type is 'checkbox',
25 label is 'Remember me?',
26 hints is 'If you want, your browser can remember your login for you',
27 default is 0;
28  
29 };
30  
31 =head1 DESCRIPTION
32  
33 This module provides a simple syntax to declare action parameters.
34  
35 It re-exports C<defer> and C<lazy> from L<Scalar::Defer>, for setting
36 parameter fields that must be recomputed at request-time:
37  
38 param name =>
39 default is defer { Jifty->web->current_user->name };
40  
41 See L<Scalar::Defer> for more information about C<defer>.
42  
43 =head2 schema
44  
45 The C<schema> block from a L<Jifty::Action> subclass describes an action
46 for a Jifty application.
47  
48 Within the C<schema> block, the localization function C<_> is redefined
49 with C<defer>, so that it resolves into a dynamic value that will be
50 recalculated upon each request, according to the user's current language
51 preference.
52  
53 =head2 param
54  
55 Each C<param> statement inside the C<schema> block sets out the name
56 and attributes used to describe one named parameter, which is then used
57 to build a L<Jifty::Param> object. That class defines possible field names
58 to use in the declarative syntax here.
59  
60 The C<param> function is not available outside the C<schema> block.
61  
62 =head1 ALIASES
63  
64 In addition to the labels provided by L<Jifty::Web::Form::Field> and
65 L<Jifty::Param>, this module offers the following aliases:
66  
67 ajax validates, # ajax_validates is 1
68 ajax canonicalizes, # ajax_canonicalizes is 1
69 order is -1, # sort_order is -1
70 default is 0, # default_value is 0
71 valid are qw( 1 2 3 ), # valid_values are qw( 1 2 3 )
72 available are qw( 1 2 3 ), # available_values are qw( 1 2 3 )
73 render as 'select', # render_as is 'select'
74  
75 =head1 SEE ALSO
76  
77 L<Object::Declare>, L<Scalar::Defer>
78  
79 =cut
80  
81 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 113 ▶use Jifty::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 113 ▶use Jifty::Param;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 113 ▶use Scalar::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 113 ▶use SUPER;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 113 ▶use Object::Declare (
Subroutine BEGIN: called CC=1 SCAR=0.0
89 113 mapping => {
90 param => 'Jifty::Param',
91 },
92 aliases => {
93 default => 'default_value',
94 available => 'available_values',
95 valid => 'valid_values',
96 render => 'render_as',
97 order => 'sort_order',
98 },
99 copula => {
100 is => '',
101 are => '',
102 as => '',
103 ajax => 'ajax_',
104 }
105 113 );
106 113 ▶use Exporter::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 113 ▶use Class::Data::Inheritable;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 our @EXPORT = qw( defer lazy param schema );
110  
111 0 ▶sub schema (&) {
Subroutine schema: not called CC=8 SCAR=38.6
112 0 my $code = shift;
113 0 my $from = caller;
114  
115 113 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
116
117 # See the perldoc for an explanation of why we're redefining
118 # the localization method _().
119 0 ▶ local *_ = sub { my $args = \@_; defer { _(@$args) } };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
120  
121 0 Class::Data::Inheritable::mk_classdata($from => qw/PARAMS/);
122 0 my @params = &declare($code);
123  
124 # The .99 here is a flag for Jifty::Action::Record to mark autogenerated orders
125 0 my $count = 10000.99;
126 0 foreach my $param (@params) {
127 0 ▶ next if !ref($param) or defined($param->sort_order);
Branch: 1 branch
BranchTrueFalseTotal
if not ref $param or defined $param->sort_order 0 0 0
Condition0%0/3
not ref $param or defined $param->sort_order
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $param or defined $param->sort_order
not ref $param defined $param->sort_order
Truth table0%0/3
not(A) or B
A= not ref $paramB= defined $param->sort_order
ABresult
1X1
011
000
128 0 $param->sort_order($count);
129 0 $count += 10;
130 }
131  
132 0 ▶ if ( my $super_params = $from->super('PARAMS') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $super_params = $from->super('PARAMS')) { } 0 0 0
133 0 my $super = $super_params->();
134 # XXX: skip the merge_params if the parent class' PARAMS is
135 # empty to avoid the currently kludgy merge_params
136 # implementation to pollute scalar::defer with undetermined
137 # behaviour
138 0 ▶ $from->PARAMS(
Branch: 1 branch
BranchTrueFalseTotal
$super && keys %$super ? : 0 0 0
Condition0%0/3
$super and keys %$super
!ll&&!rl&&r
000
MC/DC0%0/2
$super and keys %$super
$super keys %$super
Truth table0%0/3
A and B
A= $superB= keys %$super
ABresult
0X0
100
111
139 ($super && keys %$super)
140 ? merge_params( $super, {@params} )
141 : {@params} );
142 }
143 else {
144 0 $from->PARAMS( {@params} );
145 }
146  
147 113 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
148 0 ▶ push @{$from . '::ISA'}, 'Jifty::Action'
Branch: 1 branch
BranchTrueFalseTotal
unless $from->isa("Jifty::Action") 0 0 0
149 unless $from->isa('Jifty::Action');
150 0 return;
151 }
152  
153 113 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
154 113 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 113 ▶use constant MERGE_PARAM_BEHAVIOUR => {
Subroutine BEGIN: called CC=1 SCAR=4.9
156 SCALAR => {
157 0 ▶ SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
158 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 0 ▶ HASH => sub { $_[1] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 ARRAY => {
161 0 ▶ SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
162 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ HASH => sub { $_[1] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 HASH => {
165 0 ▶ SCALAR => sub { length($_[1]) ? $_[1] : $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_[1] ? : 0 0 0
166 0 ▶ ARRAY => sub { [ @{$_[1]} ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 ▶ HASH => sub { Hash::Merge::_merge_hashes( $Hash::Merge::context, $_[0], $_[1] ) } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 113 };
169  
170 my $prev_behaviour = Hash::Merge::get_behavior();
171 # the behavior name must be upper-case
172 Hash::Merge::specify_behavior( MERGE_PARAM_BEHAVIOUR, 'MERGE_PARAMS' );
173 Hash::Merge::set_behavior( $prev_behaviour );
174 my $merge = Hash::Merge->new('MERGE_PARAMS');
175  
176 =head2 merge_params HASHREF HASHREF
177  
178 Takes two hashrefs. Merges them together and returns the merged hashref.
179  
180 - Empty fields in subclasses don't override nonempty fields in superclass anymore.
181 - Arrays don't merge; e.g. if parent class's valid_values is [1,2,3,4], and
182 subclass's valid_values() is [1,2], they don't somehow become [1,2,3,4,1,2].
183  
184 BUG: This should either be a private routine or factored out into Jifty::Util
185  
186  
187  
188 =cut
189  
190 0 ▶sub merge_params {
Subroutine merge_params: not called CC=7 SCAR=40.3
191 # We pull this deref and re-ref trick to un-bless any
192 # Jifty::Params which might exist; Hash::Merge pre-0.10 merged
193 # objects and hahrefs with no complaint, but 0.10 doesn't. This
194 # is a horrible, horrible hack, and will hopeflly be able to be
195 # backed out if and when Hash::Merge reverts to the old behavior.
196 0 my $field_type = {};
197 0 my @types;
198 0 for my $m (@_) {
199 0 my @t;
200 0 for (keys %{$m}) {
201 0 push @t, ref $m->{$_};
202 0 $field_type->{$_} = ref $m->{$_};
203 0 bless $m->{$_}, "HASH";
204 }
205 0 push @types, \@t;
206 }
207 0 my $rv = $merge->merge(@_);
208 0 for my $m (@_) {
209 0 my @t = @{shift @types};
210 0 for (keys %{$m}) {
211 0 bless $m->{$_}, shift @t;
212 }
213 }
214  
215 0 for ( keys %$rv ) {
216 bless $rv->{$_}, $field_type->{$_}
217 0 ▶ if $field_type->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $field_type->{$_} 0 0 0
218 }
219 0 return $rv;
220 }
221  
222 1;
« Jifty/Plugin/Halo/Mason.pm ↑ Summary Jifty/Web/Session/ClientSide.pm »