rclass stuff ripped out of everything but widget classes
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Action.pm
1 package Reaction::InterfaceModel::Action;
2
3 use Reaction::Meta::InterfaceModel::Action::Class;
4 use metaclass 'Reaction::Meta::InterfaceModel::Action::Class';
5
6 use Reaction::Meta::Attribute;
7 use Reaction::Class;
8
9 use namespace::clean -except => [ qw(meta) ];
10
11
12 has target_model => (is => 'ro', required => 1,
13                      metaclass => 'Reaction::Meta::Attribute');
14
15 has ctx => (isa => 'Catalyst', is => 'ro', required => 1,
16               metaclass => 'Reaction::Meta::Attribute');
17 sub parameter_attributes {
18   shift->meta->parameter_attributes;
19 };
20 sub parameter_hashref {
21   my ($self) = @_;
22   my %params;
23   foreach my $attr ($self->parameter_attributes) {
24     my $reader = $attr->get_read_method;
25     my $predicate = $attr->get_predicate_method;
26     next if defined($predicate) && !$self->$predicate;
27     $params{$attr->name} = $self->$reader;
28   }
29   return \%params;
30 };
31 sub can_apply {
32   my ($self) = @_;
33   foreach my $attr ($self->parameter_attributes) {
34     my $predicate = $attr->get_predicate_method;
35     if ($self->attribute_is_required($attr)) {
36       return 0 unless $self->$predicate;
37     }
38     if ($attr->has_valid_values) {
39       unless ($predicate && !($self->$predicate)) {
40         my $reader = $attr->get_read_method;
41         return 0 unless $attr->check_valid_value($self, $self->$reader);
42       }
43     }
44   }
45   return 1;
46 };
47 sub error_for {
48   my ($self, $attr) = @_;
49   confess "No attribute passed to error_for" unless defined($attr);
50   unless (ref($attr)) {
51     my $meta = $self->meta->find_attribute_by_name($attr);
52     confess "Can't find attribute ${attr} on $self" unless $meta;
53     $attr = $meta;
54   }
55   return $self->error_for_attribute($attr);
56 };
57 sub error_for_attribute {
58   my ($self, $attr) = @_;
59   my $reader = $attr->get_read_method;
60   my $predicate = $attr->get_predicate_method;
61   if ($self->attribute_is_required($attr)) {
62     unless ($self->$predicate) {
63       return $attr->name." is required";
64     }
65   }
66   if ($self->$predicate && $attr->has_valid_values) {
67     unless ($attr->check_valid_value($self, $self->$reader)) {
68       return "Not a valid value for ".$attr->name;
69     }
70   }
71   return; # ok
72 };
73 sub attribute_is_required {
74   my ($self, $attr) = @_;
75   return $attr->is_required;
76 };
77
78 sub sync_all { }
79
80 __PACKAGE__->meta->make_immutable;
81
82
83 1;
84
85 =head1 NAME
86
87 Reaction::InterfaceModel::Action
88
89 =head1 SYNOPSIS
90
91 =head1 DESCRIPTION
92
93 =head2 target_model
94
95 =head2 ctx
96
97 =head2 parameter_attributes
98
99 =head1 SEE ALSO
100
101 L<Reaction::Meta::Attribute>
102
103 =head1 AUTHORS
104
105 See L<Reaction::Class> for authors.
106
107 =head1 LICENSE
108
109 See L<Reaction::Class> for the license.
110
111 =cut