Commit | Line | Data |
ddccc6a2 |
1 | package Reaction::UI::ViewPort::Field::Mutable::ChooseOne; |
2 | |
3 | use Reaction::Class; |
9757d0cf |
4 | use Scalar::Util (); |
ddccc6a2 |
5 | |
6 | class ChooseOne is 'Reaction::UI::ViewPort::Field', which { |
7 | |
c8fbb8ad |
8 | does 'Reaction::UI::ViewPort::Field::Role::Mutable'; |
9 | does 'Reaction::UI::ViewPort::Field::Role::Choices'; |
ddccc6a2 |
10 | |
11 | around value => sub { |
12 | my $orig = shift; |
13 | my $self = shift; |
14 | return $orig->($self) unless @_; |
15 | my $value = shift; |
16 | if (defined $value) { |
17 | $value = $self->str_to_ident($value) if (!ref $value); |
aac30a0d |
18 | my $attribute = $self->attribute; |
19 | my $checked = $attribute->check_valid_value($self->model, $value); |
20 | unless (defined $checked) { |
21 | require Data::Dumper; |
22 | my $serialised = Data::Dumper->new([ $value ])->Indent(0)->Dump; |
23 | $serialised =~ s/^\$VAR1 = //; $serialised =~ s/;$//; |
24 | confess "${serialised} is not a valid value for ${\$attribute->name} on " |
25 | ."${\$attribute->associated_class->name}"; |
26 | } |
ddccc6a2 |
27 | $value = $checked; |
28 | } |
29 | $orig->($self, $value); |
30 | }; |
31 | |
cc44a337 |
32 | implements _build_value_string => as { |
33 | my $self = shift; |
70db8298 |
34 | my $value = $self->value; |
9757d0cf |
35 | return $self->obj_to_name($value->{value}) if Scalar::Util::blessed($value); |
70db8298 |
36 | $value; |
cc44a337 |
37 | }; |
38 | |
ddccc6a2 |
39 | implements is_current_value => as { |
40 | my ($self, $check_value) = @_; |
41 | my $our_value = $self->value; |
42 | return unless ref($our_value); |
43 | $check_value = $self->obj_to_str($check_value) if ref($check_value); |
44 | return $self->obj_to_str($our_value) eq $check_value; |
45 | }; |
46 | |
47 | |
48 | }; |
49 | |
50 | 1; |