« lib/Reaction/InterfaceModel/Action/DBIC/Role/CheckUniques.pm
↑ Summary
lib/Reaction/UI/Widget/ListView.pm »
| 1 | package Reaction::UI::ViewPort::Field::Role::Mutable; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use Reaction::Role::Parameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use aliased 'Reaction::InterfaceModel::Action'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use aliased 'Reaction::Meta::InterfaceModel::Action::ParameterAttribute'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use MooseX::Types::Moose qw/Int Str/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use namespace::clean -except => [ qw(meta) ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =pod | ||||||||||
| 11 | |||||||||||
| 12 | 15:24 mst:» I'm not sure I understand why the +foo is overwriting my after'ed clear_value | ||||||||||
| 13 | 15:24 mst:» but I would argue it shouldn't do that | ||||||||||
| 14 | 15:25 @doy:» because has '+foo' is creating an entirely new attribute | ||||||||||
| 15 | 15:25 @doy:» and just copying the meta-attribute's attributes into it | ||||||||||
| 16 | 15:25 @doy:» so it's creating a new clearer sub in the subclass | ||||||||||
| 17 | 15: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 | |||||||||||
| 21 | 4 | parameter value_type => ( | |||||||||
| 22 | predicate => 'has_value_type' | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 45 | ▶ | role { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| |||||||||||
| 26 | |||||||||||
| 27 | 45 | my $p = shift; | |||||||||
| 28 | |||||||||||
| 29 | 45 | has model => (is => 'ro', isa => Action, required => 1); | |||||||||
| 30 | 45 | has attribute => (is => 'ro', isa => ParameterAttribute, required => 1); | |||||||||
| 31 | |||||||||||
| 32 | has value => ( | ||||||||||
| 33 | 6 | ▶ | is => 'rw', lazy_build => 1, trigger => sub { shift->adopt_value }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 45 | ▶ | $p->has_value_type? (isa => $p->value_type) : () | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 45 | has needs_sync => (is => 'rw', isa => Int, default => 0); #should be bool? | |||||||||
| 38 | |||||||||||
| 39 | 45 | has message => (is => 'rw', isa => Str, clearer => 'clear_message'); | |||||||||
| 40 | |||||||||||
| 41 | 45 | has is_modified => ( #sould be bool? | |||||||||
| 42 | is => 'ro', writer => '_set_modified', | ||||||||||
| 43 | required => 1, default => 1, init_arg => undef | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | after clear_value => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 47 | 0 | my $self = shift; | |||||||||
| 48 | 0 | ▶ | $self->clear_message if $self->has_message; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | $self->needs_sync(1); | |||||||||
| 50 | 45 | }; | |||||||||
| 51 | |||||||||||
| 52 | 6 | ▶ | sub adopt_value { | ||||||||
|
Subroutine
adopt_value: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 53 | 6 | my ($self) = @_; | |||||||||
| 54 | 6 | ▶ | $self->clear_message if $self->has_message; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 6 | $self->needs_sync(1); # if $self->has_attribute; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | 4 | ▶ | sub can_sync_to_action { | ||||||||
|
Subroutine
can_sync_to_action: called CC=8 SCAR=28.2
Pod
undocumented
| |||||||||||
| 60 | 4 | my $self = shift; | |||||||||
| 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 | 4 | ▶ | return 1 unless $self->needs_sync; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 4 | my $attr = $self->attribute; | |||||||||
| 66 | |||||||||||
| 67 | 4 | ▶ | if ($self->has_value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | my $value = $self->value; | |||||||||
| 69 | 4 | ▶ | if (my $tc = $attr->type_constraint) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 4 | ▶ | $value = $tc->coercion->coerce($value) if ($tc->has_coercion); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 4 | ▶ | if (defined (my $error = $tc->validate($value))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $self->message($error); | |||||||||
| 73 | 0 | return; | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | } else { | ||||||||||
| 77 | 0 | ▶ | if( $self->model->attribute_is_required($attr) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | ▶ | if(my $error = $self->model->error_for($self->attribute) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | $self->message( $error ); | |||||||||
| 80 | } | ||||||||||
| 81 | 0 | return; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | 4 | return 1; | |||||||||
| 85 | }; | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | 8 | ▶ | sub sync_to_action { | ||||||||
|
Subroutine
sync_to_action: called CC=10 SCAR=30.2
Pod
documented
| |||||||||||
| 89 | 8 | my ($self) = @_; | |||||||||
| 90 | |||||||||||
| 91 | # don't sync if we're already synced | ||||||||||
| 92 | 8 | ▶ | return unless $self->needs_sync; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | # if we got here, needs_sync is 1 | ||||||||||
| 95 | # can_sync_to_action will do coercion checks, etc. | ||||||||||
| 96 | 4 | ▶ | return unless $self->can_sync_to_action; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | 4 | my $attr = $self->attribute; | |||||||||
| 99 | |||||||||||
| 100 | 4 | ▶ | if ($self->has_value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 4 | my $value = $self->value; | |||||||||
| 102 | 4 | ▶ | if (my $tc = $attr->type_constraint) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | #this will go away when we have moose dbic. until then though... | ||||||||||
| 104 | 4 | ▶ | $value = $tc->coercion->coerce($value) if ($tc->has_coercion); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | } | ||||||||||
| 106 | 4 | my $writer = $attr->get_write_method; | |||||||||
| 107 | 4 | ▶ | confess "No writer for attribute" unless defined($writer); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 4 | $self->model->$writer($value); | |||||||||
| 109 | } else { | ||||||||||
| 110 | 0 | my $predicate = $attr->get_predicate_method; | |||||||||
| 111 | 0 | ▶ | confess "No predicate for attribute" unless defined($predicate); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | ▶ | if ($self->model->$predicate) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | my $clearer = $attr->get_clearer_method; | |||||||||
| 114 | 0 | ▶ | confess "${predicate} returned true but no clearer for attribute" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | unless defined($clearer); | ||||||||||
| 116 | 0 | $self->model->$clearer; | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | 4 | $self->needs_sync(0); | |||||||||
| 120 | }; | ||||||||||
| 121 | 8 | ▶ | sub sync_from_action { | ||||||||
|
Subroutine
sync_from_action: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||
| 122 | 8 | my ($self) = @_; | |||||||||
| 123 | 8 | ▶ | return if $self->needs_sync; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 8 | ▶ | if( !$self->has_message ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 8 | ▶ | if(my $error = $self->model->error_for($self->attribute) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | $self->message( $error ); | |||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | }; | ||||||||||
| 130 | |||||||||||
| 131 | 45 | ▶ | around accept_events => sub { ('value', shift->(@_)) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 132 | |||||||||||
| 133 | 4 | }; | |||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | 4 | 1; | |||||||||
| 137 | |||||||||||
| 138 | =head1 NAME | ||||||||||
| 139 | |||||||||||
| 140 | Reaction::UI::ViewPort::Role::Actions | ||||||||||
| 141 | |||||||||||
| 142 | =head1 DESCRIPTION | ||||||||||
| 143 | |||||||||||
| 144 | A 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 | |||||||||||
| 170 | See L<Reaction::Class> for authors. | ||||||||||
| 171 | |||||||||||
| 172 | =head1 LICENSE | ||||||||||
| 173 | |||||||||||
| 174 | See L<Reaction::Class> for the license. | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||