]> wagnertech.de Git - kivitendo-erp.git/blob - SL/DB/Helper/Presenter.pm
Presenter: Sub-Presenter auf Funktional geändert
[kivitendo-erp.git] / SL / DB / Helper / Presenter.pm
1 package SL::DB::Helper::Presenter;
2
3 use strict;
4
5 sub new {
6   # lightweight: 0: class, 1: object
7   bless [ $_[1], $_[2] ], $_[0];
8 }
9
10 sub AUTOLOAD {
11   our $AUTOLOAD;
12
13   my ($self, @args) = @_;
14
15   my $method = $AUTOLOAD;
16   $method    =~ s/.*:://;
17
18   return if $method eq 'DESTROY';
19
20   if (my $sub = $self->[0]->can($method)) {
21     return $sub->($self->[1], @args);
22   }
23 }
24
25 1;
26
27 __END__
28
29 =encoding utf-8
30
31 =head1 NAME
32
33 SL::DB::Helper::Presenter - proxy class to allow models to access presenters
34
35 =head1 SYNOPSIS
36
37   # assuming SL::Presenter::Part exists
38   # and contains a sub link_to($class, $object) {}
39   SL::DB::Part->new(%args)->presenter->link_to
40
41 =head1 DESCRIPTION
42
43 When coding controllers one often encounters objects that are not crucial to
44 the current task, but must be presented in some form to the user. Instead of
45 recreating that all the time the C<SL::Presenter> namepace was introduced to
46 hold such code.
47
48 Unfortunately the Presenter code is designed to be stateless and thus acts _on_
49 objects, but can't be instanced or wrapped. The early band-aid to that was to
50 export all sub-presenter calls into the main presenter namespace. Fixing it
51 would have meant accessing presenter functions like this:
52
53   SL::Presenter::Object->method($object, %additional_args)
54
55 which is extremely inconvenient.
56
57 This glue code allows C<SL::DB::Object> instances to access routines in their
58 presenter without additional boilerplate. C<SL::DB::Object> contains a
59 C<presenter> call for all objects, which will return an instance of this proxy
60 class. All calls on this will then be forwarded to the appropriate presenter.
61
62 =head1 INTERNAL STRUCTURE
63
64 The created proxy objects are lightweight blessed arrayrefs instead of the
65 usual blessed hashrefs. They only store two elements:
66
67 =over 4
68
69 =item * The presenter class
70
71 =item * The invocing object
72
73 =back
74
75 Further delegation is done with C<AUTOLOAD>.
76
77 =head1 BUGS
78
79 None yet :)
80
81 =head1 AUTHOR
82
83 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
84
85 =cut