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