aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm
blob: 66fa464da1c13da19a0ffbec00630075584f7fce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Reaction::UI::ViewPort::Field::Role::Mutable::Simple;

use Reaction::Role;

use aliased 'Reaction::UI::ViewPort::Field::Role::Mutable';

role Simple which {

  does Mutable;

  has value_string => (
    is => 'rw', lazy_build => 1, trigger_adopt('value_string'),
    clearer => 'clear_value',
  );

  around value_string => sub {
    my $orig = shift;
    my $self = shift;
    if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq ''
        && !$self->value_is_required) {
      $self->clear_value;
      return undef;
    }
    return $self->$orig(@_);
  };

  # the user needs to implement this because, honestly, you're always going
  # to need to do something custom and the only common thing really is
  # "you probably set $self->value at the end"
  requires 'adopt_value_string';

  around accept_events => sub { ('value_string', shift->(@_)) };

};

1;