source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Attributes.pm @ 1023

Last change on this file since 1023 was 1023, checked in by nanardon, 12 years ago
  • complete POD

This patch a basic documentation to all functions.
It also add two test to ensure all POD syntax are correct and coverage is full.

  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1package LATMOS::Accounts::Bases::Attributes;
2
3# $Id$
4
5use strict;
6use warnings;
7use LATMOS::Accounts::Log;
8
9=head1 NAME
10
11    LATMOS::Accounts::Bases::Attributes - Object to handle attribute schema
12
13=head1 FUNCTIONS
14
15=head2 new($attributes, $base_or_object, $maybe_otype)
16
17Instanciate a new Attributes object.
18
19=cut
20
21sub new {
22    my ($class, $attributes, $base_or_object, $maybe_otype) = @_;
23   
24    my ($object, $base, $otype) =
25    $base_or_object->isa('LATMOS::Accounts::Bases::Objects')
26        ? ($base_or_object, $base_or_object->base, $base_or_object->type)
27        : (undef, $base_or_object, $maybe_otype);
28
29    if (ref $attributes) {
30        $attributes->{_base} = $base;
31        $attributes->{_object} = $object;
32        $attributes->{_name} = $attributes->{name};
33        $attributes->{_otype} = $otype;
34        return bless($attributes, $class);
35    } else {
36        my $attr_info = $base->get_attr_schema($otype, $attributes) or return;
37
38        $attr_info->{_base} = $base;
39        $attr_info->{_object} = $object;
40        $attr_info->{_name} = $attributes;
41        $attr_info->{_otype} = $otype;
42
43        return bless($attr_info, $class);
44    }
45}
46
47=head2 base
48
49Return base handle
50
51=cut
52
53sub base  { $_[0]->{_base}  }
54
55=head2 name
56
57Return attribute name
58
59=cut
60
61sub name  { $_[0]->{_name}  }
62
63=head2 otype
64
65Return object type for this attribute
66
67=cut
68
69
70sub otype { $_[0]->{_otype} }
71
72=head2 mandatory
73
74Return true if attribute is mandatory
75
76=cut
77
78sub mandatory { $_[0]->{mandatory} || 0 }
79
80=head2 object
81
82Return handle object if any
83
84=cut
85
86sub object { $_[0]->{_object} }
87
88=head2 reference
89
90A object type this attribute refer to
91
92=cut
93
94sub reference {
95    my ($self) = @_;
96    if ($self->{reference} &&
97        $self->base->is_supported_object($self->{reference})) {
98        return $self->{reference};
99    } else {
100        return;
101    }
102}
103
104=head2 iname
105
106Return internal name of attributes
107
108=cut
109
110sub iname { $_[0]->{iname} || $_[0]->name }
111
112=head2 label
113
114Return the label to display
115
116=cut
117
118sub label { $_[0]->{label} || $_[0]->{_name} }
119
120=head2 has_values_list
121
122Return true if the attribute have a fixed list of accepted values
123
124=cut
125
126sub has_values_list {
127    my ($self) = @_;
128    if ($self->base->obj_attr_allowed_values(
129        $self->otype,
130        $self->name) ||
131        $self->{can_values} ||
132        $self->reference) {
133        return 1;
134    } else {
135        return 0;
136    }
137}
138
139=head2 can_values
140
141Return possible value allowed by this attribute
142
143=cut
144
145sub can_values {
146    my ($self) = @_;
147    if (my @values = $self->base->obj_attr_allowed_values(
148            $self->otype,
149            $self->name)) {
150        return @values;
151    } elsif ($self->{can_values}) {
152        if (ref $self->{can_values} eq 'ARRAY') {
153            return @{$self->{can_values}};
154        } elsif (ref $self->{can_values} eq 'CODE') {
155            $self->{can_values}->($self, $self->object);
156        } else {
157            return;
158        }
159    } elsif (my $ref = $self->reference) {
160        return $self->base->list_objects($ref);
161    } else { return }
162}
163
164=head2 display ($value)
165
166Return the well formated value according attribute
167
168=cut
169
170sub display {
171    my ($self, $value) = @_;
172    if ($self->{display}) {
173        return $self->{display}->($self, $value);
174    } else {
175        return $value;
176    }
177}
178
179=head2 input ($value)
180
181Return well formated single value for insert into base
182
183=cut
184
185sub input {
186    my ($self, $value) = @_;
187    if ($self->{input}) {
188        return $self->{input}->($value);
189    } else {
190        return $value;
191    }
192}
193
194=head2 readable
195
196Return true if attribute can be read
197
198=cut
199
200sub readable {
201    my ($self) = @_;
202    if (ref $self->{readable} eq 'CODE') {
203        return $self->{readable}->($self->object) || 0;
204    } else {
205        return defined($_[0]->{readable}) ? $_[0]->{readable} : 1;
206    }
207}
208
209=head2 ro
210
211Return true if the attribute cannot be write by design
212
213=cut
214
215sub ro {
216    my ($self) = @_;
217    if (ref $self->{ro} eq 'CODE') {
218        return $self->{ro}->($self->object) || 0;
219    } else {
220        return $_[0]->{ro} || 0 
221    }
222}
223
224=head2 readonly
225
226Return true if attribute cannot be read according acls or attributes state
227
228=cut
229
230sub readonly { 
231    my ($self) = @_;
232    return 1 if ($self->ro);
233   
234    return ! $self->check_acl('w');
235}
236
237=head2 check_acl ($mode)
238
239Return true is access to C<$mode> is granted
240
241=cut
242
243sub check_acl {
244    my ($self, $mode) = @_;
245
246    return 1 if ($self->{_noacl});
247
248    return $self->base->check_acl($self->object
249        ? ($self->object, $self->name, $mode)
250        : ($self->otype, '@CREATE', $mode));
251}
252
253=head2 real_form_type
254
255Return the way the fields must be show in GUI.
256For each type option maybe given by form_option
257
258=head3 LABEL
259
260=over 4
261
262=item length
263
264The length to use to show the attribute
265
266=back
267
268=head3 TEXT
269
270=head3 TEXTAREA
271
272=head3 DATE
273
274=head3 LIST
275
276=head3 CHECKBOX
277
278=over 4
279
280=item rawvalue
281
282The real value of the attribute must be show
283
284=back
285
286=cut
287
288sub real_form_type { $_[0]->{formtype} || 'TEXT' }
289
290=head2 form_type
291
292Return the way the attribute must be displayed
293
294=cut
295
296sub form_type {
297    $_[0]->readonly ? 'LABEL' :
298    $_[0]->{formtype} ? $_[0]->{formtype} :
299    $_[0]->has_values_list ? 'LIST' :
300    $_[0]->real_form_type
301}
302
303=head2 form_option ($option)
304
305Return the form option C<$option>
306
307=cut
308
309sub form_option {
310    my ($self, $option) = @_;
311    return $self->{formopts}{$option}
312}
313
314=head2 uniq
315
316Return true is attribute value must be uniq
317
318=cut
319
320sub uniq { $_[0]->{uniq} || 0 }
321
322=head2 multiple
323
324Return true is attribute value can be set several times
325
326=cut
327
328sub multiple { $_[0]->{multiple} || 0 }
329
330=head2 hidden
331
332Return true if attributes must not appear in list by can be query.
333
334=cut
335
336sub hidden { $_[0]->{hide} || 0 }
337
338=head2 delayed
339
340Return true if attribute must be set after object creation during
341synchronisation
342
343=cut
344
345sub delayed { $_[0]->{delayed} || 0 }
346
347=head2 get($attr)
348
349Return the value for this attribute
350
351=cut
352
353sub get {
354    my ($self) = @_;
355
356    if (ref $self->{get} eq 'CODE') {
357        return $self->{get}->($self);
358    } else {
359        return $self->object->get_field($self->iname);
360    }
361}
362
363=head2 set ($values)
364
365Set attribute value to attached object
366
367=cut
368
369sub set {
370    my ($self, $values) = @_;
371
372    if (ref $self->{set} eq 'CODE') {
373        return $self->{set}->($self,
374            ref $values
375            ? [ map { $self->input($_) } @$values ]
376            : $values ? $self->input($values) : undef);
377    } else {
378        return $self->object->set_fields(
379            $self->iname,
380            ref $values
381                ? [ map { $self->input($_) } @$values ]
382                : $values ? $self->input($values) : undef);
383    }
384}
385
386=head2 default_value
387
388Return default value for this attribute
389
390=cut
391
392sub default_value {
393    my ($self) = @_;
394    return grep { $_ } (ref $self->{default}
395        ? @{ $self->{default} }
396        : $self->{default});
397}
398
3991;
400
401__END__
402
403=head1 SEE ALSO
404
405L<LATMOS::Accounts::Bases>
406
407=head1 AUTHOR
408
409Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
410
411=head1 COPYRIGHT AND LICENSE
412
413Copyright (C) 2012 CNRS SA/CETP/LATMOS
414
415This library is free software; you can redistribute it and/or modify
416it under the same terms as Perl itself, either Perl version 5.10.0 or,
417at your option, any later version of Perl 5 you may have available.
418
419=cut
Note: See TracBrowser for help on using the repository browser.