blib/lib/Gapp/Meta/Widget/Native/Trait/Form.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 169 CC 18 SCAR 21.6
CC
18
Cov
0%
_build_stash
20.0
do_apply_action
20.0
do_cancel_action
12.0
CRAP 8.7
1 package Gapp::Meta::Widget::Native::Trait::Form;
2 {
3 $Gapp::Meta::Widget::Native::Trait::Form::VERSION = '0.60';
4 }
5  
6 use Moose::Role;
7 use MooseX::SemiAffordanceAccessor;
8  
9 use Gapp::Form::Stash;
10 use Gapp::Types qw( FormContext FormField FormStash );
11 with 'Gapp::Meta::Widget::Native::Role::FormElement';
12  
13 use Gapp::Types qw(GappCallback);
14 use MooseX::Types::Moose qw(Undef CodeRef);
15  
16 use Gapp::Actions::Util qw( parse_action );
17  
18 # defines how to transfer values from form to object
19 has 'context' => (
20 is => 'rw',
21 isa => FormContext,
22 );
23  
24 # a registry of fields and values
25 has 'stash' => (
26 is => 'rw',
27 isa => FormStash,
28 lazy_build => 1,
29 );
30  
31 # whether or not to sync the *context* - the stash is always synced
32 has 'sync' => (
33 is => 'rw',
34 isa => 'Bool',
35 default => 0,
36 );
37  
38  
39 has [qw( apply_action cancel_action ok_action ) ] => (
40 is => 'rw',
41 isa => GappCallback|Undef,
42 );
43  
44 sub _build_stash {
45 my $self = shift;
46
47 my $stash = Gapp::Form::Stash->new;
48
49 # traverse the form and get field names to populate the stash with
50 # these field names must be in the stash in order to retrieve the
51 # starting values from the context
52 for my $w ( $self->find_fields ) {
53 next if ! $w->field;
54 $stash->store( $w->field, undef ) if ! $stash->contains( $w->field );
55 }
56
57 # update the values in the stash via the context
58 $stash->update( $self->context ) if $self->context;
59
60 $stash->set_modified( 0 );
61 return $stash;
62 }
63  
64 # update the user variables
65 sub apply {
66 my ( $self ) = @_;
67 $self->sync_stash;
68
69 $self->context->update( $self->stash ) if $self->context;
70 $self->do_apply_action;
71 }
72  
73 sub cancel {
74 my ( $self ) = @_;
75 return if $self->do_cancel_action;
76 $self->close;
77 }
78  
79 sub close {
80 my ( $self ) = @_;
81 $self->gobject->destroy;
82 }
83  
84 sub ok {
85 my ( $self ) = @_;
86 $self->apply;
87 $self->do_ok_action;
88 $self->close;
89 }
90  
91 sub do_apply_action {
92 my ( $self ) = @_;
93 return if ! $self->apply_action;
94
95 my ( $action, @args ) = parse_action ( $self->apply_action );
96 return if ! $action;
97
98 if ( is_CodeRef ( $action ) ) {
99 $action->( $self, \@args );
100 }
101 else {
102 $action->perform( $self, \@args );
103 }
104 }
105  
106 sub do_cancel_action {
107 my ( $self ) = @_;
108 return if ! $self->cancel_action;
109
110 my ( $action, @args ) = parse_action ( $self->cancel_action );
111
112 if ( is_CodeRef ($action) ) {
113 $action->( $self, \@args );
114 }
115 else {
116 $action->perform( $self, \@args );
117 }
118 }
119  
120  
121 sub do_ok_action {
122 my ( $self ) = @_;
123 return if ! $self->ok_action;
124
125 my ( $action, @args ) = parse_action ( $self->ok_action );
126
127 if ( is_CodeRef $action ) {
128 $action->( $self, \@args );
129 }
130 else {
131 $action->perform( $self, \@args );
132 }
133 }
134  
135  
136  
137  
138  
139 sub find_fields {
140 my ( $self ) = ( @ _ );
141
142 my @fields;
143 for my $w ( $self->find_descendants ) {
144 push @fields, $w if is_FormField( $w );
145 }
146
147 return @fields;
148 }
149  
150 sub _initialize_stash {
151 my ( $self ) = @_;
152
153 my $stash = $self->stash;
154
155 for my $w ( $self->find_fields ) {
156 $stash->store( $w->field, undef ) if ! $stash->contains( $w->field );
157 }
158
159 $stash->set_modified( 0 );
160 }
161  
162 # update the values in the form
163 sub update {
164 my ( $self ) = ( @_ );
165
166 # update the stash if we have a context
167 $self->stash->update( $self->context ) if $self->context;
168
169 # update the widgets from the stash
170 $self->update_from_stash;
171 }
172  
173 # update the values in the form (don't update the stash first)
174 sub update_from_stash {
175 my ( $self ) = @_;
176
177 # update the values in the form
178 for my $w ( $self->find_fields ) {
179 $w->set_is_updating( 1 );
180 $w->stash_to_widget( $self->stash ) if $w->field;
181 $w->set_is_updating( 0 );
182 }
183 }
184  
185  
186  
187  
188 sub update_stash {
189 my ( $self ) = shift;
190 warn '$form->update_stash deprecated, use $form->sync_stash';
191 $self->sync_stash( @_ );
192 }
193  
194 sub sync_stash {
195 my ( $self ) = @_;
196
197 my $stash = $self->stash;
198
199 for my $w ( $self->find_fields ) {
200 $w->widget_to_stash( $stash ) if $w->field;
201 }
202
203 #$stash->set_modified( 1 );
204 }
205  
206  
207  
208  
209 package Gapp::Meta::Widget::Custom::Trait::Form;
210 {
211 $Gapp::Meta::Widget::Custom::Trait::Form::VERSION = '0.60';
212 }
213 sub register_implementation { 'Gapp::Meta::Widget::Native::Trait::Form' };
214  
215 1;
216  
217  
218 __END__