new component to make m2ms introspectables so we can hint the reflector
[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 class 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
22   implements _build__default_action_class_prefix => as {
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) = @_;
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
48   implements '_action_class_for' => as {
49     my ($self, $action) = @_;
50     confess("Wrong arguments") unless $action;
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) = @_;
59     confess("Wrong arguments") unless $action;
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
76 1;
77
78 __END__;
79
80
81 =head1 NAME
82
83 Reaction::Class::InterfaceModel::Object
84
85 =head1 SYNOPSIS
86
87 =head1 DESCRIPTION
88
89 InterfaceModel Object base class.
90
91 =head1 Attributes
92
93 =head2 _action_class_map
94
95 RW, isa HashRef - Returns an empty hashref by default. It will hold a series of actions
96 as keys with their corresponding action classes as values.
97
98 =head2 _default_action_class_prefix
99
100 RO, isa Str - Default action class prefix. Lazy build by default to the value
101 returned 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
109 Shortcuts for these same subs in meta. They will return attribute objects that are of
110 the correct type, L<Reaction::Meta::InterfaceModel::Object::ParameterAttribute> and
111 L<Reaction::Meta::InterfaceModel::Object::DomainModelAttribute>
112
113 =head2 _default_action_class_for $action
114
115 Provides the default package name for the C<$action> action-class.
116 It defaults to the value of C<_default_action_class_prefix> followed by
117 C<::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
124 Return the action class for an action name. Will search
125 C<_action_class_map> or, if not found, use the value of
126 C<_default_action_class_for>
127
128 =head2 action_for $action, %args
129
130 Will 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
135 By default will return an empty hashref
136
137 =head2 _override_action_args_for
138
139 Returns empty hashref by default.
140
141 =head1 SEE ALSO
142
143 L<Reaction::InterfaceModel::ObjectClass>
144
145 =head1 AUTHORS
146
147 See L<Reaction::Class> for authors.
148
149 =head1 LICENSE
150
151 See L<Reaction::Class> for the license.
152
153 =cut