Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 972b8771

Von Sven Schöling vor fast 8 Jahren hinzugefügt

  • ID 972b87711cb3c7543d585ceedca0eda2058aa6d7
  • Vorgänger 5162c8cc
  • Nachfolger 02235d37

Model-Presenter Bindung mit Proxyobjekten

Unterschiede anzeigen:

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
  return $self->[0]->$method($self->[1], @args);
21
}
22

  
23
1;
24

  
25
__END__
26

  
27
=encoding utf-8
28

  
29
=head1 NAME
30

  
31
SL::DB::Helper::Presenter - proxy class to allow models to access presenters
32

  
33
=head1 SYNOPSIS
34

  
35
  # assuming SL::Presemter::Part exists
36
  # and contains a sub link_to($class, $object) {}
37
  SL::DB::Part->new(%args)->presenter->link_to
38

  
39
=head1 DESCRIPTION
40

  
41
When coding controller one often encounters objects that are not crucial to the
42
current task, but must be presented in some form to the user. Instead of
43
recreating that all the time the C<SL::Presenter> namepace was introduced to
44
hold such code.
45

  
46
Unfortunately the Presenter code is designed to be stateless and thus acts _on_
47
objects, but can't be instanced or wrapped. The early band-aid to that was to
48
export all sub-presenter calls into the main presenter namespace. Fixing it
49
would have meant to access presenter functions like this:
50

  
51
  SL::Presenter::Object->method($object, %additional_args)
52

  
53
which is  extremely inconvenient.
54

  
55
This glue code allows C<SL::DB::Object> instances to access routines in their
56
presenter without additional boilerplate. C<SL::DB::Object> contains a
57
C<presenter> call for all objects, which will return an instance of this proxy
58
class. All calls on this will then be forwarded to the appropriate presenter.
59

  
60
=head1 INTERNAL STRUCTURE
61

  
62
The proxy objects created are lightweight blessed arrayrefs instead of the usual blessed
63
hashrefs. They only store two elements:
64

  
65
=over 4
66

  
67
=item * The presenter class
68

  
69
=item * The invocing object
70

  
71
=back
72

  
73
Further delegation is done with C<AUTOLOAD>.
74

  
75
=head1 BUGS
76

  
77
None yet :)
78

  
79
=head1 AUTHOR
80

  
81
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
82

  
83
=cut
SL/DB/Object.pm
12 12
use SL::DB::Helper::Attr;
13 13
use SL::DB::Helper::Metadata;
14 14
use SL::DB::Helper::Manager;
15
use SL::DB::Helper::Presenter;
15 16
use SL::DB::Object::Hooks;
16 17

  
17 18
use base qw(Rose::DB::Object);
......
238 239
  return $clone;
239 240
}
240 241

  
242
sub presenter {
243
  my ($class_or_self) = @_;
244

  
245
  if (ref $class_or_self) {
246
    my $class = ref $class_or_self;
247
    $class =~ s{^SL::DB::}{SL::Presenter::};
248
    return SL::DB::Helper::Presenter->new($class, $class_or_self);
249
  } else {
250
    $class_or_self =~ s{^SL::DB::}{SL::Presenter::};
251
    return $class_or_self;
252
  }
253
}
254

  
241 255
1;
242 256

  
243 257
__END__
......
361 375
will also skip setting the following fields if such columns exist for
362 376
C<$self>: C<itime>, C<mtime>.
363 377

  
378
=item C<presenter>
379

  
380
Returns a proxy wrapper that will dispatch all method calls to the presenter
381
with the same name as the class of the involking object.
382

  
383
For the full documentation about its capabilites see
384
L<SL::DB::Helper::Presenter>
385

  
364 386
=back
365 387

  
366 388
=head1 AUTHOR

Auch abrufbar als: Unified diff