move redirect_to to a role and deprecate it
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable / Simple.pm
1 package Reaction::UI::ViewPort::Field::Role::Mutable::Simple;
2
3 use Reaction::Role;
4
5 use aliased 'Reaction::UI::ViewPort::Field::Role::Mutable';
6
7 use namespace::clean -except => [ qw(meta) ];
8 with Mutable;
9
10 has value_string => (
11   is => 'rw', lazy_build => 1, trigger_adopt('value_string'),
12   clearer => 'clear_value',
13 );
14
15 has '+is_modified' => (default => 0);
16
17 around value_string => sub {
18   my $orig = shift;
19   my $self = shift;
20   if (@_) {
21     # recursive call. be VERY careful we don't go infinite here
22     my $old = $self->value_string;
23     my $new = $_[0];
24     if ((defined $old xor defined $new) || (defined $old && $old ne $new)) {
25       $self->_set_modified(1);
26     } else {
27       return;
28     }
29   }
30   if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq ''
31       && !$self->value_is_required) {
32     $self->clear_value;
33     return undef;
34   }
35   return $self->$orig(@_);
36 };
37
38 # the user needs to implement this because, honestly, you're always going
39 # to need to do something custom and the only common thing really is
40 # "you probably set $self->value at the end"
41 requires 'adopt_value_string';
42
43 around accept_events => sub { ('value_string', shift->(@_)) };
44
45 around force_events => sub { (value_string => '', shift->(@_)) };
46
47
48
49 1;