Revision 6a2c8b54
Von Sven Schöling vor etwa 1 Jahr hinzugefügt
SL/DB/DeliveryOrder.pm | ||
---|---|---|
12 | 12 |
use SL::DB::Helper::AttrSorted; |
13 | 13 |
use SL::DB::Helper::FlattenToForm; |
14 | 14 |
use SL::DB::Helper::LinkedRecords; |
15 |
use SL::DB::Helper::TypeDataProxy; |
|
15 | 16 |
use SL::DB::Helper::TransNumberGenerator; |
16 | 17 |
use SL::DB::Helper::RecordLink qw(RECORD_ID RECORD_TYPE_REF); |
17 | 18 |
|
... | ... | |
427 | 428 |
$self->date->to_kivitendo; |
428 | 429 |
} |
429 | 430 |
|
431 |
sub type_data { |
|
432 |
SL::DB::Helper::TypeDataProxy->new(ref $_[0], $_[0]->type); |
|
433 |
} |
|
434 |
|
|
430 | 435 |
1; |
431 | 436 |
__END__ |
432 | 437 |
|
SL/DB/Helper/TypeDataProxy.pm | ||
---|---|---|
1 |
package SL::DB::Helper::TypeDataProxy; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use Scalar::Util qw(weaken); |
|
5 |
|
|
6 |
sub new { |
|
7 |
my ($class, $record_class, $type) = @_; |
|
8 |
|
|
9 |
my $type_data_class = $record_class . '::TypeData'; |
|
10 |
eval "require $type_data_class" or die "invalid type data class '$type_data_class'"; |
|
11 |
|
|
12 |
bless [ |
|
13 |
$type, |
|
14 |
$type_data_class |
|
15 |
], $class; |
|
16 |
} |
|
17 |
|
|
18 |
# convenience methods for common topics in type data |
|
19 |
sub text { |
|
20 |
my $self = shift; |
|
21 |
$self->[1]->can("get3")->($self->[0], "text", @_); |
|
22 |
} |
|
23 |
|
|
24 |
sub properties { |
|
25 |
my $self = shift; |
|
26 |
$self->[1]->can("get3")->($self->[0], "properties", @_); |
|
27 |
} |
|
28 |
|
|
29 |
sub show_menu { |
|
30 |
my $self = shift; |
|
31 |
$self->[1]->can("get3")->($self->[0], "show_menu", @_); |
|
32 |
} |
|
33 |
|
|
34 |
sub rights { |
|
35 |
my $self = shift; |
|
36 |
$self->[1]->can("get3")->($self->[0], "rights", @_); |
|
37 |
} |
|
38 |
|
|
39 |
sub AUTOLOAD { |
|
40 |
our $AUTOLOAD; |
|
41 |
|
|
42 |
my ($self, @args) = @_; |
|
43 |
|
|
44 |
my $method = $AUTOLOAD; |
|
45 |
$method =~ s/.*:://; |
|
46 |
|
|
47 |
return if $method eq 'DESTROY'; |
|
48 |
|
|
49 |
if (my $sub = $self->[1]->can($method)) { |
|
50 |
return $sub->($self->[0], @args); |
|
51 |
} else { |
|
52 |
die "no method $method in $self->[1]"; |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
1; |
|
57 |
|
|
58 |
__END__ |
|
59 |
|
|
60 |
=encoding utf-8 |
|
61 |
|
|
62 |
=head1 NAME |
|
63 |
|
|
64 |
SL::DB::Helper::TypeDataProxy - proxy for accessing type data from a record instance |
|
65 |
|
|
66 |
=head1 SYNOPSIS |
|
67 |
|
|
68 |
# in a SL::DB::Record type |
|
69 |
sub type_data { |
|
70 |
SL::DB::Helper::TypeDataProxy->new(ref $_[0], $_[0]->type); |
|
71 |
} |
|
72 |
|
|
73 |
# in consuming code with a record object |
|
74 |
# the methods are only available if the TypeData class supports them |
|
75 |
$record->type_data->is_type(...) |
|
76 |
$record->type_data->valid_types |
|
77 |
$record->type_data->text('edit') |
|
78 |
$record->type_data->properties('is_customer') |
|
79 |
|
|
80 |
=head1 DESCRIPTION |
|
81 |
|
|
82 |
Wrap the static type information of a given record into an accessible sub object. |
|
83 |
Only works if a TypeData class exists that knows about the return values of the given type |
|
84 |
Since this does not operate on the live objects but rather wraps a snapshot, |
|
85 |
this will not get updated if the creating object changes. |
|
86 |
|
|
87 |
=head1 PROXIED METHODS |
|
88 |
|
|
89 |
=over 4 |
|
90 |
|
|
91 |
=item * C<valid_types> |
|
92 |
|
|
93 |
Returns the known sub types of this record |
|
94 |
|
|
95 |
=item * C<is_type> $type |
|
96 |
|
|
97 |
Checks whether this record is of the given type |
|
98 |
|
|
99 |
=item * C<is_valid_type> |
|
100 |
|
|
101 |
=item * C<validate_type> |
|
102 |
|
|
103 |
Check whether this record has a valid type. C<is_valid_type> returns boolish, C<validate_type> will throw if the validation fails. |
|
104 |
|
|
105 |
=item * C<text> $action |
|
106 |
|
|
107 |
Returns the translated text for this action and action |
|
108 |
|
|
109 |
=item * C<properties> $property |
|
110 |
|
|
111 |
Returns the named property for this type |
|
112 |
|
|
113 |
=item * C<show_menu> $action |
|
114 |
|
|
115 |
Rtuens whether the given menu action is valid for this type |
|
116 |
|
|
117 |
=item * C<rights> $action |
|
118 |
|
|
119 |
Returns the needed rights for this action with this type |
|
120 |
|
|
121 |
=back |
|
122 |
|
|
123 |
=head1 BUGS |
|
124 |
|
|
125 |
None yet. :) |
|
126 |
|
|
127 |
=head1 AUTHOR |
|
128 |
|
|
129 |
Sven Schöling $<lt>s.schoeling@googlemail.comE<gt> |
|
130 |
|
|
131 |
=cut |
SL/DB/Order.pm | ||
---|---|---|
18 | 18 |
use SL::DB::Helper::LinkedRecords; |
19 | 19 |
use SL::DB::Helper::PriceTaxCalculator; |
20 | 20 |
use SL::DB::Helper::PriceUpdater; |
21 |
use SL::DB::Helper::TypeDataProxy; |
|
21 | 22 |
use SL::DB::Helper::TransNumberGenerator; |
22 | 23 |
use SL::DB::Helper::Payment qw(forex); |
23 | 24 |
use SL::DB::Helper::RecordLink qw(RECORD_ID RECORD_TYPE_REF RECORD_ITEM_ID RECORD_ITEM_TYPE_REF); |
... | ... | |
689 | 690 |
} |
690 | 691 |
} |
691 | 692 |
|
693 |
sub type_data { |
|
694 |
SL::DB::Helper::TypeDataProxy->new(ref $_[0], $_[0]->type); |
|
695 |
} |
|
696 |
|
|
692 | 697 |
1; |
693 | 698 |
|
694 | 699 |
__END__ |
SL/DB/Reclamation.pm | ||
---|---|---|
17 | 17 |
use SL::DB::Helper::LinkedRecords; |
18 | 18 |
use SL::DB::Helper::PriceTaxCalculator; |
19 | 19 |
use SL::DB::Helper::PriceUpdater; |
20 |
use SL::DB::Helper::TypeDataProxy; |
|
20 | 21 |
use SL::DB::Helper::TransNumberGenerator; |
21 | 22 |
use SL::DB::Helper::RecordLink qw(RECORD_ID RECORD_TYPE_REF); |
22 | 23 |
use SL::Locale::String qw(t8); |
... | ... | |
478 | 479 |
$self->date->to_kivitendo; |
479 | 480 |
} |
480 | 481 |
|
482 |
sub type_data { |
|
483 |
SL::DB::Helper::TypeDataProxy->new(ref $_[0], $_[0]->type); |
|
484 |
} |
|
485 |
|
|
486 |
|
|
481 | 487 |
1; |
482 | 488 |
|
483 | 489 |
__END__ |
Auch abrufbar als: Unified diff
TypeData: proxy um $record->type_data benutzen zu können