Commit | Line | Data |
62ffa273 |
1 | package Reaction::UI::ViewPort::Field::Role::Mutable::Simple; |
2 | |
9be81e19 |
3 | use Reaction::Role::Parameterized; |
62ffa273 |
4 | |
5 | use aliased 'Reaction::UI::ViewPort::Field::Role::Mutable'; |
6 | |
81393881 |
7 | use namespace::clean -except => [ qw(meta) ]; |
22a2b243 |
8 | |
9 | parameter value_type => ( |
10 | predicate => 'has_value_type' |
11 | ); |
12 | |
13 | role { |
14 | |
15 | my $p = shift; |
16 | |
17 | with Mutable, $p->has_value_type ? { value_type => $p->value_type } : (); |
81393881 |
18 | |
19 | has value_string => ( |
22a2b243 |
20 | is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value_string }, |
81393881 |
21 | ); |
22 | |
e5f405d6 |
23 | # FIXME - Copied from Reaction::UI::ViewPort::Field::Role::Mutable |
24 | # as we broke has '+attr' in Moose. |
25 | has is_modified => ( #sould be bool? |
26 | is => 'ro', writer => '_set_modified', |
27 | required => 1, default => 0, init_arg => undef |
28 | ); |
029c34a8 |
29 | |
81393881 |
30 | around value_string => sub { |
31 | my $orig = shift; |
32 | my $self = shift; |
029c34a8 |
33 | if (@_) { |
34 | # recursive call. be VERY careful we don't go infinite here |
35 | my $old = $self->value_string; |
36 | my $new = $_[0]; |
37 | if ((defined $old xor defined $new) || (defined $old && $old ne $new)) { |
38 | $self->_set_modified(1); |
39 | } else { |
40 | return; |
41 | } |
42 | } |
81393881 |
43 | if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq '' |
44 | && !$self->value_is_required) { |
45 | $self->clear_value; |
46 | return undef; |
47 | } |
48 | return $self->$orig(@_); |
49 | }; |
62ffa273 |
50 | |
81393881 |
51 | # the user needs to implement this because, honestly, you're always going |
52 | # to need to do something custom and the only common thing really is |
53 | # "you probably set $self->value at the end" |
54 | requires 'adopt_value_string'; |
62ffa273 |
55 | |
81393881 |
56 | around accept_events => sub { ('value_string', shift->(@_)) }; |
62ffa273 |
57 | |
81393881 |
58 | around force_events => sub { (value_string => '', shift->(@_)) }; |
62ffa273 |
59 | |
22a2b243 |
60 | }; |
62ffa273 |
61 | |
62 | 1; |