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