cd3c3dbcdb1cb5660f3d3980fa112157e5967430
[catagits/Reaction.git] / lib / Reaction / InterfaceModel / Object.pm
1 package Reaction::InterfaceModel::Object;
2
3 use metaclass 'Reaction::Meta::InterfaceModel::Object::Class';
4 use Reaction::Meta::Attribute;
5 use Reaction::Class;
6
7 use namespace::clean -except => [ qw(meta) ];
8
9
10 has _action_class_map =>
11   (is => 'rw', isa => 'HashRef', required => 1, default => sub{ {} },
12    metaclass => 'Reaction::Meta::Attribute');
13
14 has _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
23 sub _build__default_action_class_prefix {
24   my $self = shift;
25   ref $self || $self;
26 };
27
28 #just a little convenience
29 sub parameter_attributes {
30   shift->meta->parameter_attributes;
31 };
32
33 #just a little convenience
34 sub domain_models {
35   shift->meta->domain_models;
36 };
37 sub _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 };
47 sub _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 };
55 sub 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
69 sub _default_action_args_for { {} };
70 sub _override_action_args_for { {} };
71
72 __PACKAGE__->meta->make_immutable;
73
74
75 1;
76
77 __END__;
78
79
80 =head1 NAME
81
82 Reaction::Class::InterfaceModel::Object
83
84 =head1 SYNOPSIS
85
86 =head1 DESCRIPTION
87
88 InterfaceModel Object base class.
89
90 =head1 Attributes
91
92 =head2 _action_class_map
93
94 RW, isa HashRef - Returns an empty hashref by default. It will hold a series of actions
95 as keys with their corresponding action classes as values.
96
97 =head2 _default_action_class_prefix
98
99 RO, isa Str - Default action class prefix. Lazy build by default to the value
100 returned 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
108 Shortcuts for these same subs in meta. They will return attribute objects that are of
109 the correct type, L<Reaction::Meta::InterfaceModel::Object::ParameterAttribute> and
110 L<Reaction::Meta::InterfaceModel::Object::DomainModelAttribute>
111
112 =head2 _default_action_class_for $action
113
114 Provides the default package name for the C<$action> action-class.
115 It defaults to the value of C<_default_action_class_prefix> followed by
116 C<::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
123 Return the action class for an action name. Will search
124 C<_action_class_map> or, if not found, use the value of
125 C<_default_action_class_for>
126
127 =head2 action_for $action, %args
128
129 Will 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
134 By default will return an empty hashref
135
136 =head2 _override_action_args_for
137
138 Returns empty hashref by default.
139
140 =head1 SEE ALSO
141
142 L<Reaction::InterfaceModel::ObjectClass>
143
144 =head1 AUTHORS
145
146 See L<Reaction::Class> for authors.
147
148 =head1 LICENSE
149
150 See L<Reaction::Class> for the license.
151
152 =cut