1 package Reaction::UI::ViewPort::Field::Role::Choices;
5 use Scalar::Util 'blessed';
9 has valid_values => (isa => 'ArrayRef', is => 'ro', lazy_build => 1);
10 has value_choices => (isa => 'ArrayRef', is => 'ro', lazy_build => 1);
11 has value_map_method => (
12 isa => 'Str', is => 'ro', required => 1, default => sub { 'display_name' },
15 implements str_to_ident => as {
16 my ($self, $str) = @_;
17 my $u = URI->new('','http');
19 return { $u->query_form };
22 implements obj_to_str => as {
23 my ($self, $obj) = @_;
24 return $obj unless ref($obj);
25 confess "${obj} not an object" unless blessed($obj);
26 my $ident = $obj->ident_condition; #XXX DBIC ism that needs to go away
27 my $u = URI->new('', 'http');
28 $u->query_form(%$ident);
32 implements obj_to_name => as {
33 my ($self, $obj) = @_;
34 return $obj unless ref($obj);
35 confess "${obj} not an object" unless blessed($obj);
36 my $meth = $self->value_map_method;
40 implements _build_valid_values => as {
42 return [ $self->attribute->all_valid_values($self->action) ];
45 implements _build_value_choices => sub{
47 my @pairs = map{{value => $self->obj_to_str($_), name => $self->obj_to_name($_)}}
48 @{ $self->valid_values };
49 return [ sort { $a->{name} cmp $b->{name} } @pairs ];