lazy_build automatically defines clear_$attr, so clearer was overwriting
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Field / Role / Mutable.pm
CommitLineData
ddccc6a2 1package Reaction::UI::ViewPort::Field::Role::Mutable;
2
22a2b243 3use MooseX::Role::Parameterized;
c8fbb8ad 4
ddccc6a2 5use aliased 'Reaction::InterfaceModel::Action';
6use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute';
7b5e71ad 7use MooseX::Types::Moose qw/Int Str/;
81393881 8use namespace::clean -except => [ qw(meta) ];
9
22a2b243 10=pod
11
1215:24 mst:» I'm not sure I understand why the +foo is overwriting my after'ed clear_value
1315:24 mst:» but I would argue it shouldn't do that
1415:25 @doy:» because has '+foo' is creating an entirely new attribute
1515:25 @doy:» and just copying the meta-attribute's attributes into it
1615:25 @doy:» so it's creating a new clearer sub in the subclass
1715:27 rafl:» mst: for that case, i tend to just parameterize the role on whatever i might want to override in its attribute definitions
18
19=cut
20
21parameter value_type => (
22 predicate => 'has_value_type'
23);
24
25role {
26
27my $p = shift;
28
81393881 29has model => (is => 'ro', isa => Action, required => 1);
30has attribute => (is => 'ro', isa => ParameterAttribute, required => 1);
31
32has value => (
22a2b243 33 is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value },
22a2b243 34 $p->has_value_type? (isa => $p->value_type) : ()
81393881 35);
22a2b243 36
7b5e71ad 37has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool?
114916fc 38
7b5e71ad 39has message => (is => 'rw', isa => Str, clearer => 'clear_message');
81393881 40
7b5e71ad 41has is_modified => ( #sould be bool?
22a2b243 42 is => 'ro', writer => '_set_modified',
029c34a8 43 required => 1, default => 1, init_arg => undef
44);
45
81393881 46after clear_value => sub {
47 my $self = shift;
48 $self->clear_message if $self->has_message;
49 $self->needs_sync(1);
50};
114916fc 51
81393881 52sub adopt_value {
53 my ($self) = @_;
54 $self->clear_message if $self->has_message;
55 $self->needs_sync(1); # if $self->has_attribute;
114916fc 56}
57
28a7e0a7 58
81393881 59sub can_sync_to_action {
60 my $self = shift;
28a7e0a7 61
62 # if field is already sync'ed, it can be sync'ed again
63 # this will make sync_to_action no-op if needs_sync is 0
64 return 1 unless $self->needs_sync;
81393881 65 my $attr = $self->attribute;
66
67 if ($self->has_value) {
68 my $value = $self->value;
69 if (my $tc = $attr->type_constraint) {
70 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
71 if (defined (my $error = $tc->validate($value))) {
72 $self->message($error);
73 return;
f25cb331 74 }
5ea9eefd 75 }
81393881 76 } else {
86db4be7 77 if( $self->model->attribute_is_required($attr) ){
78 if(my $error = $self->model->error_for($self->attribute) ){
79 $self->message( $error );
80 }
81 return;
82 }
81393881 83 }
84 return 1;
85};
28a7e0a7 86
87
81393881 88sub sync_to_action {
89 my ($self) = @_;
28a7e0a7 90
91 # don't sync if we're already synced
92 return unless $self->needs_sync;
93
94 # if we got here, needs_sync is 1
95 # can_sync_to_action will do coercion checks, etc.
81393881 96 return unless $self->can_sync_to_action;
97
98 my $attr = $self->attribute;
99
100 if ($self->has_value) {
101 my $value = $self->value;
102 if (my $tc = $attr->type_constraint) {
103 #this will go away when we have moose dbic. until then though...
104 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
ddccc6a2 105 }
81393881 106 my $writer = $attr->get_write_method;
107 confess "No writer for attribute" unless defined($writer);
108 $self->model->$writer($value);
109 } else {
110 my $predicate = $attr->get_predicate_method;
111 confess "No predicate for attribute" unless defined($predicate);
112 if ($self->model->$predicate) {
113 my $clearer = $attr->get_clearer_method;
114 confess "${predicate} returned true but no clearer for attribute"
115 unless defined($clearer);
116 $self->model->$clearer;
117 }
118 }
119 $self->needs_sync(0);
120};
121sub sync_from_action {
122 my ($self) = @_;
5ad52d3a 123 return if $self->needs_sync;
81393881 124 if( !$self->has_message ){
125 if(my $error = $self->model->error_for($self->attribute) ){
126 $self->message( $error );
577fe414 127 }
81393881 128 }
129};
130
131around accept_events => sub { ('value', shift->(@_)) };
ddccc6a2 132
22a2b243 133};
ddccc6a2 134
c8fbb8ad 135
1361;
2dba7201 137
138=head1 NAME
139
140Reaction::UI::ViewPort::Role::Actions
141
142=head1 DESCRIPTION
143
144A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
145
146=head1 ATTRIBUTES
147
148=head2 needs_sync
149
150=head2 message
151
152=head2 model
153
154=head2 attribute
155
156=head2 value
157
158=head1 METHODS
159
160=head2 accept_events
161
162=head2 sync_from_action
163
164=head2 sync_to_action
165
166=head2 adopt_value
167
168=head1 AUTHORS
169
170See L<Reaction::Class> for authors.
171
172=head1 LICENSE
173
174See L<Reaction::Class> for the license.
175
176=cut