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