aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/Field/Role/Mutable/Simple.pm
blob: d8ca47328259ca63fb702214b2b5f8babf04e455 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Reaction::UI::ViewPort::Field::Role::Mutable::Simple;

use MooseX::Role::Parameterized;

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

use namespace::clean -except => [ qw(meta) ];

parameter value_type => (
    predicate => 'has_value_type'
);

role {

my $p = shift;

with Mutable, $p->has_value_type ? { value_type => $p->value_type } : ();

has value_string => (
  is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value_string },
  clearer => 'clear_value',
);

has '+is_modified' => (default => 0);

around value_string => sub {
  my $orig = shift;
  my $self = shift;
  if (@_) {
    # recursive call. be VERY careful we don't go infinite here
    my $old = $self->value_string;
    my $new = $_[0];
    if ((defined $old xor defined $new) || (defined $old && $old ne $new)) {
      $self->_set_modified(1);
    } else {
      return;
    }
  }
  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->(@_)) };

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

};

1;