Commit | Line | Data |
62ffa273 |
1 | package Reaction::UI::ViewPort::Field::Role::Mutable::Simple; |
2 | |
22a2b243 |
3 | use MooseX::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 | clearer => 'clear_value', |
22 | ); |
23 | |
029c34a8 |
24 | has '+is_modified' => (default => 0); |
25 | |
81393881 |
26 | around value_string => sub { |
27 | my $orig = shift; |
28 | my $self = shift; |
029c34a8 |
29 | if (@_) { |
30 | # recursive call. be VERY careful we don't go infinite here |
31 | my $old = $self->value_string; |
32 | my $new = $_[0]; |
33 | if ((defined $old xor defined $new) || (defined $old && $old ne $new)) { |
34 | $self->_set_modified(1); |
35 | } else { |
36 | return; |
37 | } |
38 | } |
81393881 |
39 | if (@_ && defined($_[0]) && !ref($_[0]) && $_[0] eq '' |
40 | && !$self->value_is_required) { |
41 | $self->clear_value; |
42 | return undef; |
43 | } |
44 | return $self->$orig(@_); |
45 | }; |
62ffa273 |
46 | |
81393881 |
47 | # the user needs to implement this because, honestly, you're always going |
48 | # to need to do something custom and the only common thing really is |
49 | # "you probably set $self->value at the end" |
50 | requires 'adopt_value_string'; |
62ffa273 |
51 | |
81393881 |
52 | around accept_events => sub { ('value_string', shift->(@_)) }; |
62ffa273 |
53 | |
81393881 |
54 | around force_events => sub { (value_string => '', shift->(@_)) }; |
62ffa273 |
55 | |
22a2b243 |
56 | }; |
62ffa273 |
57 | |
58 | 1; |