added localization to core widgets
[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
81393881 6use namespace::clean -except => [ qw(meta) ];
7extends 'Reaction::Meta::Attribute';
8
9
10has valid_values => (
11 isa => 'CodeRef',
12 is => 'rw', # doesnt need of it anymore, maybe we should warn before change it
13 predicate => 'has_valid_values'
14);
15sub new { shift->SUPER::new(@_); }; # work around immutable
16sub check_valid_value {
17 my ($self, $object, $value) = @_;
18 confess "Can't check_valid_value when no valid_values set"
19 unless $self->has_valid_values;
20 confess join " - ", blessed($object), $self->name
21 unless ref $self->valid_values;
22 my $valid = $self->valid_values->($object, $self);
23 if ($self->type_constraint
24 && ($self->type_constraint->name eq 'ArrayRef'
25 || $self->type_constraint->is_subtype_of('ArrayRef'))) {
26 confess "Parameter type is array ref but passed value isn't"
27 unless ref($value) eq 'ARRAY';
28 return [ map { $self->_check_single_valid($valid => $_) } @$value ];
29 } else {
30 return $self->_check_single_valid($valid => $value);
31 }
32};
33sub _check_single_valid {
34 my ($self, $valid, $value) = @_;
35 return undef unless defined($value);
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};
44sub all_valid_values {
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')
50 ? @$valid
51 : $valid->all);
7adfd53f 52};
81393881 53sub valid_value_collection {
54 my ($self, $object) = @_;
55 confess "Can't call valid_value_collection on an attribute without valid_values"
56 unless $self->has_valid_values;
57 my $valid = $self->valid_values->($object, $self);
58 confess "valid_values returned an arrayref, not a collection"
59 if (ref $valid eq 'ARRAY');
60 return $valid;
61};
62
63__PACKAGE__->meta->make_immutable;
64
7adfd53f 65
661;
67
68=head1 NAME
69
70Reaction::Meta::InterfaceModel::Action::ParamterAttribute
71
72=head1 DESCRIPTION
73
74=head1 METHODS
75
76=head2 new
77
78=head2 valid_values
79
80=head2 has_valid_values
81
82=head2 check_valid_value
83
84=head2 all_valid_values
85
86=head2 valid_value_collection
87
88=head2 reader
89
90=head2 writer
91
92=head1 SEE ALSO
93
94L<Reaction::Meta::Attribute>
95
96=head1 AUTHORS
97
98See L<Reaction::Class> for authors.
99
100=head1 LICENSE
101
102See L<Reaction::Class> for the license.
103
104=cut