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