better error when no predicate for required attribute
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Role::Mutable;
2
c8fbb8ad 3use Reaction::Role;
4
ddccc6a2 5use aliased 'Reaction::InterfaceModel::Action';
6use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7
81393881 8use namespace::clean -except => [ qw(meta) ];
9
10has model => (is => 'ro', isa => Action, required => 1);
11has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
12
13has value => (
14 is => 'rw', lazy_build => 1, trigger_adopt('value'),
15 clearer => 'clear_value',
16);
17has needs_sync => (is => 'rw', isa => 'Int', default => 0);
18#predicates are autmagically generated for lazy and non-required attrs
19has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
20
21after clear_value => sub {
22 my $self = shift;
23 $self->clear_message if $self->has_message;
24 $self->needs_sync(1);
25};
26sub adopt_value {
27 my ($self) = @_;
28 $self->clear_message if $self->has_message;
29 $self->needs_sync(1); # if $self->has_attribute;
30};
31sub can_sync_to_action {
32 my $self = shift;
33 return 1 unless $self->needs_sync;
34 my $attr = $self->attribute;
35
36 if ($self->has_value) {
37 my $value = $self->value;
38 if (my $tc = $attr->type_constraint) {
39 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
40 if (defined (my $error = $tc->validate($value))) {
41 $self->message($error);
42 return;
f25cb331 43 }
5ea9eefd 44 }
81393881 45 } else {
ecf7a860 46 if ( $self->model->attribute_is_required($attr) ) {
47 my $tc = $attr->type_constraint;
48 $self->message($tc->get_message) if $tc->has_message;
49 return;
50 }
81393881 51 }
52 return 1;
53};
54sub sync_to_action {
55 my ($self) = @_;
56 return unless $self->needs_sync;
57 return unless $self->can_sync_to_action;
58
59 my $attr = $self->attribute;
60
61 if ($self->has_value) {
62 my $value = $self->value;
63 if (my $tc = $attr->type_constraint) {
64 #this will go away when we have moose dbic. until then though...
65 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
ddccc6a2 66 }
81393881 67 my $writer = $attr->get_write_method;
68 confess "No writer for attribute" unless defined($writer);
69 $self->model->$writer($value);
70 } else {
71 my $predicate = $attr->get_predicate_method;
72 confess "No predicate for attribute" unless defined($predicate);
73 if ($self->model->$predicate) {
74 my $clearer = $attr->get_clearer_method;
75 confess "${predicate} returned true but no clearer for attribute"
76 unless defined($clearer);
77 $self->model->$clearer;
78 }
79 }
80 $self->needs_sync(0);
81};
82sub sync_from_action {
83 my ($self) = @_;
84 return unless !$self->needs_sync; # && $self->has_attribute;
85 if( !$self->has_message ){
86 if(my $error = $self->model->error_for($self->attribute) ){
87 $self->message( $error );
577fe414 88 }
81393881 89 }
90};
91
92around accept_events => sub { ('value', shift->(@_)) };
ddccc6a2 93
ddccc6a2 94
c8fbb8ad 95
961;
2dba7201 97
98=head1 NAME
99
100Reaction::UI::ViewPort::Role::Actions
101
102=head1 DESCRIPTION
103
104A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
105
106=head1 ATTRIBUTES
107
108=head2 needs_sync
109
110=head2 message
111
112=head2 model
113
114=head2 attribute
115
116=head2 value
117
118=head1 METHODS
119
120=head2 accept_events
121
122=head2 sync_from_action
123
124=head2 sync_to_action
125
126=head2 adopt_value
127
128=head1 AUTHORS
129
130See L<Reaction::Class> for authors.
131
132=head1 LICENSE
133
134See L<Reaction::Class> for the license.
135
136=cut