r20898@agaton (orig r755): wreis | 2008-07-24 00:32:56 +0100
[catagits/Reaction.git] / lib / Reaction / Meta / InterfaceModel / Action / ParameterAttribute.pm
CommitLineData
7adfd53f 1package Reaction::Meta::InterfaceModel::Action::ParameterAttribute;
2
3use Reaction::Class;
4use Scalar::Util 'blessed';
5
6class ParameterAttribute is 'Reaction::Meta::Attribute', which {
7 has valid_values => (
8 isa => 'CodeRef',
905a0946 9 is => 'rw', # doesnt need of it anymore, maybe we should warn before change it
7adfd53f 10 predicate => 'has_valid_values'
11 );
12
13 implements new => as { shift->SUPER::new(@_); }; # work around immutable
14
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;
89939ff9 19 confess join " - ", blessed($object), $self->name
20 unless ref $self->valid_values;
7adfd53f 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 ];
28 } else {
29 return $self->_check_single_valid($valid => $value);
30 }
31 };
32
33 implements _check_single_valid => as {
34 my ($self, $valid, $value) = @_;
f6ec638f 35 return undef unless defined($value);
7adfd53f 36 if (ref $valid eq 'ARRAY') {
37 return $value if grep { $_ eq $value } @$valid;
38 } else {
39 $value = $value->ident_condition if blessed($value);
40 return $valid->find($value);
41 }
42 return undef; # XXX this is an assumption that undef is never valid
43 };
44
45 implements all_valid_values => as {
46 my ($self, $object) = @_;
47 confess "Can't call all_valid_values on an attribute without valid_values"
48 unless $self->has_valid_values;
49 my $valid = $self->valid_values->($object, $self);
50 return ((ref $valid eq 'ARRAY')
51 ? @$valid
52 : $valid->all);
53 };
54
55 implements valid_value_collection => as {
56 my ($self, $object) = @_;
57 confess "Can't call valid_value_collection on an attribute without valid_values"
58 unless $self->has_valid_values;
59 my $valid = $self->valid_values->($object, $self);
60 confess "valid_values returned an arrayref, not a collection"
61 if (ref $valid eq 'ARRAY');
62 return $valid;
63 };
64
65};
66
671;
68
69=head1 NAME
70
71Reaction::Meta::InterfaceModel::Action::ParamterAttribute
72
73=head1 DESCRIPTION
74
75=head1 METHODS
76
77=head2 new
78
79=head2 valid_values
80
81=head2 has_valid_values
82
83=head2 check_valid_value
84
85=head2 all_valid_values
86
87=head2 valid_value_collection
88
89=head2 reader
90
91=head2 writer
92
93=head1 SEE ALSO
94
95L<Reaction::Meta::Attribute>
96
97=head1 AUTHORS
98
99See L<Reaction::Class> for authors.
100
101=head1 LICENSE
102
103See L<Reaction::Class> for the license.
104
105=cut