just check_valid_value if the attr_is_required
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Object.pm
CommitLineData
7adfd53f 1package Reaction::InterfaceModel::Object;
2
3use metaclass 'Reaction::Meta::InterfaceModel::Object::Class';
4use Reaction::Meta::Attribute;
5use Reaction::Class;
6
7class Object which {
8
9 has _action_class_map =>
10 (is => 'rw', isa => 'HashRef', required => 1, default => sub{ {} },
11 metaclass => 'Reaction::Meta::Attribute');
12
13 has _default_action_class_prefix =>
14 (
15 is => 'ro',
16 isa => 'Str',
17 lazy_build => 1,
18 metaclass => 'Reaction::Meta::Attribute',
19 );
20
21 #DBIC::Collection would override this to use result_class for example
89939ff9 22 implements _build__default_action_class_prefix => as {
7adfd53f 23 my $self = shift;
24 ref $self || $self;
25 };
26
27 #just a little convenience
28 implements parameter_attributes => as {
29 shift->meta->parameter_attributes;
30 };
31
32 #just a little convenience
33 implements domain_models => as {
34 shift->meta->domain_models;
35 };
36
37 implements '_default_action_class_for' => as {
38 my ($self, $action) = @_;
f670cfd0 39 confess("Wrong arguments") unless $action;
7adfd53f 40 #little trick in case we call it in class context!
41 my $prefix = ref $self ?
42 $self->_default_action_class_prefix :
89939ff9 43 $self->_build__default_action_class_prefix;
7adfd53f 44
45 return join "::", $prefix, 'Action', $action;
46 };
47
48 implements '_action_class_for' => as {
49 my ($self, $action) = @_;
f670cfd0 50 confess("Wrong arguments") unless $action;
7adfd53f 51 if (defined (my $class = $self->_action_class_map->{$action})) {
52 return $class;
53 }
54 return $self->_default_action_class_for($action);
55 };
56
57 implements 'action_for' => as {
58 my ($self, $action, %args) = @_;
f670cfd0 59 confess("Wrong arguments") unless $action;
7adfd53f 60 my $class = $self->_action_class_for($action);
61 %args = (
62 %{$self->_default_action_args_for($action)},
63 %args,
64 %{$self->_override_action_args_for($action)},
65 );
66 return $class->new(%args);
67 };
68
69 #this really needs to be smarter, fine for CRUD, shit for anything else
70 # massive fucking reworking needed here, really
71 implements _default_action_args_for => as { {} };
72 implements _override_action_args_for => as { {} };
73
74};
75
761;
77
78__END__;
79
80
81=head1 NAME
82
83Reaction::Class::InterfaceModel::Object
84
85=head1 SYNOPSIS
86
87=head1 DESCRIPTION
88
89InterfaceModel Object base class.
90
91=head1 Attributes
92
93=head2 _action_class_map
94
95RW, isa HashRef - Returns an empty hashref by default. It will hold a series of actions
96as keys with their corresponding action classes as values.
97
98=head2 _default_action_class_prefix
99
100RO, isa Str - Default action class prefix. Lazy build by default to the value
101returned by C<_build_default_action_class_prefix> which is C<ref $self || $self>.
102
103=head1 Methods
104
105=head2 parameter_attributes
106
107=head2 domain_models
108
109Shortcuts for these same subs in meta. They will return attribute objects that are of
110the correct type, L<Reaction::Meta::InterfaceModel::Object::ParameterAttribute> and
111L<Reaction::Meta::InterfaceModel::Object::DomainModelAttribute>
112
113=head2 _default_action_class_for $action
114
115Provides the default package name for the C<$action> action-class.
116It defaults to the value of C<_default_action_class_prefix> followed by
117C<::Action::$action>
118
119 #for MyApp::Foo, returns MyApp::Foo::Action::Create
120 $obj->_default_action_class_for('Create');
121
122=head2 _action_class_for $action
123
124Return the action class for an action name. Will search
125C<_action_class_map> or, if not found, use the value of
126C<_default_action_class_for>
127
128=head2 action_for $action, %args
129
130Will return a new instance of C<$action>. If specified,
131 %args will be passed through to C<new> as is.
132
133=head2 _default_action_args_for
134
135By default will return an empty hashref
136
137=head2 _override_action_args_for
138
139Returns empty hashref by default.
140
141=head1 SEE ALSO
142
143L<Reaction::InterfaceModel::ObjectClass>
144
145=head1 AUTHORS
146
147See L<Reaction::Class> for authors.
148
149=head1 LICENSE
150
151See L<Reaction::Class> for the license.
152
153=cut