1 package Reaction::Meta::InterfaceModel::Action::ParameterAttribute;
4 use Scalar::Util 'blessed';
6 class ParameterAttribute is 'Reaction::Meta::Attribute', which {
9 is => 'rw', # hack since clone_and_inherit hates me.
10 predicate => 'has_valid_values'
13 implements new => as { shift->SUPER::new(@_); }; # work around immutable
15 implements check_valid_value => as {
16 my ($self, $object, $value) = @_;
17 confess "Can't check_valid_value when no valid_values set"
18 unless $self->has_valid_values;
19 confess join " - ", blessed($object), $self->name
20 unless ref $self->valid_values;
21 my $valid = $self->valid_values->($object, $self);
22 if ($self->type_constraint
23 && ($self->type_constraint->name eq 'ArrayRef'
24 || $self->type_constraint->is_subtype_of('ArrayRef'))) {
25 confess "Parameter type is array ref but passed value isn't"
26 unless ref($value) eq 'ARRAY';
27 return [ map { $self->_check_single_valid($valid => $_) } @$value ];
29 return $self->_check_single_valid($valid => $value);
33 implements _check_single_valid => as {
34 my ($self, $valid, $value) = @_;
35 if (ref $valid eq 'ARRAY') {
36 return $value if grep { $_ eq $value } @$valid;
38 $value = $value->ident_condition if blessed($value);
39 return $valid->find($value);
41 return undef; # XXX this is an assumption that undef is never valid
44 implements all_valid_values => as {
45 my ($self, $object) = @_;
46 confess "Can't call all_valid_values on an attribute without valid_values"
47 unless $self->has_valid_values;
48 my $valid = $self->valid_values->($object, $self);
49 return ((ref $valid eq 'ARRAY')
54 implements valid_value_collection => as {
55 my ($self, $object) = @_;
56 confess "Can't call valid_value_collection on an attribute without valid_values"
57 unless $self->has_valid_values;
58 my $valid = $self->valid_values->($object, $self);
59 confess "valid_values returned an arrayref, not a collection"
60 if (ref $valid eq 'ARRAY');
70 Reaction::Meta::InterfaceModel::Action::ParamterAttribute
80 =head2 has_valid_values
82 =head2 check_valid_value
84 =head2 all_valid_values
86 =head2 valid_value_collection
94 L<Reaction::Meta::Attribute>
98 See L<Reaction::Class> for authors.
102 See L<Reaction::Class> for the license.