source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Accreq.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 Rev
File size: 4.4 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Accreq;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use LATMOS::Accounts::Utils;
8use LATMOS::Accounts::Log;
9use base qw(LATMOS::Accounts::Bases::Sql::objects);
10use YAML;
11
12our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
13
14=head1 NAME
15
16LATMOS::Accounts::Bases::Sql::Accreq - Accreq object in LATMOS::Accounts system
17
18=head1 DESCRIPTION
19
20The C<Accreq> object is a form handle information about form allowing user to
21request admin for change
22
23=head1 FUNCTIONS
24
25=cut
26
27sub _yaml_attr { qw(description notifyMail) }
28
29sub _object_table { 'accreq' }
30
31sub _key_field { 'name' }
32
33sub _has_extended_attributes { 1 }
34
35sub _get_attr_schema {
36    my ($class, $base) = @_;
37
38    $class->SUPER::_get_attr_schema($base,
39        {
40            unexported  => { inline => 1, formtype => 'CHECKBOX',},
41            name        => { inline => 1, ro => 1, },
42            create      => { inline => 1, ro => 1, },
43            date        => { inline => 1, ro => 1, },
44            attributes  => {
45                ro => 1,
46                multiple => 1,
47                get => sub {
48                    my ($self) = @_;
49                    my $ref = _parse_form($self,
50                        $self->object->get_attributes('form'));
51                    my @attributes = ();
52                    my @attrs = @{ $ref->{attrs} };
53                    while (my $attr = shift(@attrs)) {
54                        my $attrname = ref $attr
55                        ? $attr->{name}
56                        : $attr;
57                        push(@attributes, $attrname);
58                    }
59                    \@attributes
60                },
61            },
62            oType       => {
63                can_values => sub {
64                    return $base->list_supported_objects
65                },
66                mandatory => 1,
67            },
68            requireObject => { formtype => 'CHECKBOX', },
69            form => {
70                get => sub {
71                    my ($self) = @_;
72                    if (my $obj = $self->object) {
73                        my $res = $obj->parse_form;
74                        foreach (_yaml_attr()) {
75                            $res->{$_} = $obj->get_field($_);
76                        }
77                        return YAML::freeze($res);
78                    } else {
79                        return;
80                    }
81                },
82                set => sub {
83                    my ($self, $yaml) = @_;
84                    my $res = _parse_form($self, $yaml)
85                        or return 0;
86                    my %attrs;
87                    foreach (_yaml_attr()) {
88                        $attrs{$_} = $res->{$_} if (exists($res->{$_}));
89                    }
90                    $self->object->set_fields(%attrs, form => $yaml);
91                },
92            },
93        }
94    )
95}
96
97=head2 parse_form
98
99Load C<form> attribute and return reference to data read from YAML data
100
101=cut 
102
103sub parse_form {
104    my ($self) = @_;
105    if (my $text = $self->get_field('form')) {
106        $self->_parse_form($text);
107    } else {
108        return { attrs => [] };
109    }
110}
111
112sub _parse_form {
113    my ($self, $yaml) = @_;
114
115    my $ref = YAML::thaw($yaml) or return 0;
116
117    my @attributes = ();
118    my @attrs = @{ $ref->{attrs} };
119    while (my $attr = shift(@attrs)) {
120        my $attrname = ref $attr
121            ? $attr->{name}
122            : $attr;
123
124        if (!$attrname) {
125            $self->base->log('LA_ERR', 'No attribute name supplied');
126            return;
127        }
128    }
129
130    $ref
131}
132
133=head2 attr_info($attr)
134
135Return information for attribute C<$attr> from form.
136
137=cut
138
139sub attr_info {
140    my ($self, $wanted_attr) = @_;
141
142    my $ref = $self->parse_form;
143
144    my @attrs = @{ $ref->{attrs} };
145    while (my $attr = shift(@attrs)) {
146        my $attrname = ref $attr
147            ? $attr->{name}
148            : $attr;
149        if ($wanted_attr eq $attrname) {
150            if (ref $attr) {
151                return $attr;
152            } else {
153                return;
154            }
155        }
156    }
157    return;
158}
159
160
1611;
162
163__END__
164
165=head1 SEE ALSO
166
167L<LATMOS::Accounts::Bases::Sql>
168
169=head1 AUTHOR
170
171Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
172
173=head1 COPYRIGHT AND LICENSE
174
175Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
176
177This library is free software; you can redistribute it and/or modify
178it under the same terms as Perl itself, either Perl version 5.10.0 or,
179at your option, any later version of Perl 5 you may have available.
180
181=cut
Note: See TracBrowser for help on using the repository browser.