source: LATMOS-Accounts/lib/LATMOS/Accounts.pm @ 34

Last change on this file since 34 was 34, checked in by nanardon, 15 years ago
  • _load_base must load the base, as name said
  • Property svn:keywords set to Id Rev
File size: 2.7 KB
Line 
1package LATMOS::Accounts;
2
3use 5.010000;
4use strict;
5use warnings;
6use base qw(Config::IniFiles);
7use LATMOS::Accounts::Bases;
8
9our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
10
11sub new {
12    my ($class, $config) = @_;
13
14    $config ||= '/etc/latmos-account.ini';
15
16    my $self = Config::IniFiles->new(
17        -file => $config
18    );
19
20    bless($self, $class)
21}
22
23sub base {
24    my ($self, $section) = @_;
25    # this method perform a cache
26    $self->{_bases}{$section} and return $self->{_bases}{$section};
27    $self->load_base($section) ? $self->{_bases}{$section} : undef;
28}
29
30sub default_base {
31    my ($self) = @_;
32    my $default = $self->default_base_name or return;
33    $self->base($default);
34}
35
36# load or a if need base
37sub load_base {
38    my ($self, $section) = @_;
39    return ($self->{_bases}{$section} ||= $self->_load_base($section))
40        ? 1
41        : 0;
42}
43
44# do the bad work
45sub _load_base {
46    my ($self, $section) = @_;
47    my $type = $self->val($section, 'type') or return;
48    my %params = map { $_ => ($self->val($section, $_)) } $self->Parameters($section);
49    my $base = LATMOS::Accounts::Bases->new($type, %params);
50    $base->load or return;
51    $base;
52}
53
54sub default_base_name {
55    my ($self) = @_;
56    $self->val('_default_', 'base', ($self->list_bases)[0]);
57}
58
59sub list_bases {
60    my ($self) = @_;
61    grep {
62        !m/^_.*_$/
63    } $self->Sections
64}
65
66sub load_all_base {
67    my ($self) = @_;
68    foreach ($self->list_bases) {
69        $self->load_base($_) or do {
70            warn "Cannot load base $_\n";
71            return 0;
72        };
73    }
74    1;
75}
76
771;
78
79__END__
80# Below is stub documentation for your module. You'd better edit it!
81
82=head1 NAME
83
84LATMOS::Accounts - Perl extension for blah blah blah
85
86=head1 SYNOPSIS
87
88  use LATMOS::Accounts;
89  blah blah blah
90
91=head1 DESCRIPTION
92
93Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the
94author of the extension was negligent enough to leave the stub
95unedited.
96
97Blah blah blah.
98
99=head2 EXPORT
100
101None by default.
102
103
104
105=head1 SEE ALSO
106
107Mention other useful documentation such as the documentation of
108related modules or operating system documentation (such as man pages
109in UNIX), or any relevant external documentation such as RFCs or
110standards.
111
112If you have a mailing list set up for your module, mention it here.
113
114If you have a web site set up for your module, mention it here.
115
116=head1 AUTHOR
117
118Thauvin Olivier, E<lt>olivier.thauvin.ipsl.fr@localdomainE<gt>
119
120=head1 COPYRIGHT AND LICENSE
121
122Copyright (C) 2009 by Thauvin Olivier
123
124This library is free software; you can redistribute it and/or modify
125it under the same terms as Perl itself, either Perl version 5.10.0 or,
126at your option, any later version of Perl 5 you may have available.
127
128
129=cut
Note: See TracBrowser for help on using the repository browser.