changed search api to less magic but working version
[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);
114916fc 18
81393881 19has message => (is => 'rw', isa => 'Str', clearer => 'clear_message');
20
029c34a8 21has is_modified => (
22 is => 'ro', writer => '_set_modified',
23 required => 1, default => 1, init_arg => undef
24);
25
81393881 26after clear_value => sub {
27 my $self = shift;
28 $self->clear_message if $self->has_message;
29 $self->needs_sync(1);
30};
114916fc 31
81393881 32sub adopt_value {
33 my ($self) = @_;
34 $self->clear_message if $self->has_message;
35 $self->needs_sync(1); # if $self->has_attribute;
114916fc 36}
37
28a7e0a7 38
81393881 39sub can_sync_to_action {
40 my $self = shift;
28a7e0a7 41
42 # if field is already sync'ed, it can be sync'ed again
43 # this will make sync_to_action no-op if needs_sync is 0
44 return 1 unless $self->needs_sync;
81393881 45 my $attr = $self->attribute;
46
47 if ($self->has_value) {
48 my $value = $self->value;
49 if (my $tc = $attr->type_constraint) {
50 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
51 if (defined (my $error = $tc->validate($value))) {
52 $self->message($error);
53 return;
f25cb331 54 }
5ea9eefd 55 }
81393881 56 } else {
86db4be7 57 if( $self->model->attribute_is_required($attr) ){
58 if(my $error = $self->model->error_for($self->attribute) ){
59 $self->message( $error );
60 }
61 return;
62 }
81393881 63 }
64 return 1;
65};
28a7e0a7 66
67
81393881 68sub sync_to_action {
69 my ($self) = @_;
28a7e0a7 70
71 # don't sync if we're already synced
72 return unless $self->needs_sync;
73
74 # if we got here, needs_sync is 1
75 # can_sync_to_action will do coercion checks, etc.
81393881 76 return unless $self->can_sync_to_action;
77
78 my $attr = $self->attribute;
79
80 if ($self->has_value) {
81 my $value = $self->value;
82 if (my $tc = $attr->type_constraint) {
83 #this will go away when we have moose dbic. until then though...
84 $value = $tc->coercion->coerce($value) if ($tc->has_coercion);
ddccc6a2 85 }
81393881 86 my $writer = $attr->get_write_method;
87 confess "No writer for attribute" unless defined($writer);
88 $self->model->$writer($value);
89 } else {
90 my $predicate = $attr->get_predicate_method;
91 confess "No predicate for attribute" unless defined($predicate);
92 if ($self->model->$predicate) {
93 my $clearer = $attr->get_clearer_method;
94 confess "${predicate} returned true but no clearer for attribute"
95 unless defined($clearer);
96 $self->model->$clearer;
97 }
98 }
99 $self->needs_sync(0);
100};
101sub sync_from_action {
102 my ($self) = @_;
5ad52d3a 103 return if $self->needs_sync;
81393881 104 if( !$self->has_message ){
105 if(my $error = $self->model->error_for($self->attribute) ){
106 $self->message( $error );
577fe414 107 }
81393881 108 }
109};
110
111around accept_events => sub { ('value', shift->(@_)) };
ddccc6a2 112
ddccc6a2 113
c8fbb8ad 114
1151;
2dba7201 116
117=head1 NAME
118
119Reaction::UI::ViewPort::Role::Actions
120
121=head1 DESCRIPTION
122
123A role to ease attaching actions to L<Reaction::InterfaceModel::Object>s
124
125=head1 ATTRIBUTES
126
127=head2 needs_sync
128
129=head2 message
130
131=head2 model
132
133=head2 attribute
134
135=head2 value
136
137=head1 METHODS
138
139=head2 accept_events
140
141=head2 sync_from_action
142
143=head2 sync_to_action
144
145=head2 adopt_value
146
147=head1 AUTHORS
148
149See L<Reaction::Class> for authors.
150
151=head1 LICENSE
152
153See L<Reaction::Class> for the license.
154
155=cut