aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/ViewPort/Field/Mutable/ChooseOne.pm
blob: f16ea664d4c24ec7e17101b188a6c684755c41a7 (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
package Reaction::UI::ViewPort::Field::Mutable::ChooseOne;

use Reaction::Class;
use Scalar::Util ();

class ChooseOne is 'Reaction::UI::ViewPort::Field', which {

  does 'Reaction::UI::ViewPort::Field::Role::Mutable';
  does 'Reaction::UI::ViewPort::Field::Role::Choices';

  around value => sub {
    my $orig = shift;
    my $self = shift;
    return $orig->($self) unless @_;
    my $value = shift;
    if (defined $value) {
      $value = $self->str_to_ident($value) if (!ref $value);
      my $attribute = $self->attribute;
      my $checked = $attribute->check_valid_value($self->model, $value);
      unless (defined $checked) {
        require Data::Dumper; 
        my $serialised = Data::Dumper->new([ $value ])->Indent(0)->Dump;
        $serialised =~ s/^\$VAR1 = //; $serialised =~ s/;$//;
        confess "${serialised} is not a valid value for ${\$attribute->name} on "
                ."${\$attribute->associated_class->name}";
      }
      $value = $checked;
    }
    $orig->($self, $value);
  };

  around _value_string_from_value => sub {
    my $orig = shift;
    my $self = shift;
    my $value = $self->$orig(@_);
    return $self->obj_to_name($value->{value}) if Scalar::Util::blessed($value);
    return $self->obj_to_name($value) if blessed $value;
    return "$value"; # force stringify. might work. probably won't.
  };

  implements is_current_value => as {
    my ($self, $check_value) = @_;
    return unless $self->_model_has_value;
    my $our_value = $self->value;
    return unless ref($our_value);
    $check_value = $self->obj_to_str($check_value) if ref($check_value);
    return $self->obj_to_str($our_value) eq $check_value;
  };


};

1;