1 | package LATMOS::Accounts; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use base qw(Config::IniFiles); |
---|
7 | use LATMOS::Accounts::Bases; |
---|
8 | |
---|
9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | sub 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 | |
---|
23 | sub 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 | |
---|
30 | sub 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 |
---|
37 | sub 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 |
---|
45 | sub _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, label => $section); |
---|
50 | $base->load or return; |
---|
51 | $base; |
---|
52 | } |
---|
53 | |
---|
54 | sub default_base_name { |
---|
55 | my ($self) = @_; |
---|
56 | $self->val('_default_', 'base', ($self->list_bases)[0]); |
---|
57 | } |
---|
58 | |
---|
59 | sub list_bases { |
---|
60 | my ($self) = @_; |
---|
61 | grep { |
---|
62 | !m/^_.*_$/ |
---|
63 | } $self->Sections |
---|
64 | } |
---|
65 | |
---|
66 | sub 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 | |
---|
77 | sub config_sync_base { |
---|
78 | my ($self, $from) = @_; |
---|
79 | return split(/ /, $self->val($from, 'push', '')); |
---|
80 | } |
---|
81 | |
---|
82 | 1; |
---|
83 | |
---|
84 | __END__ |
---|
85 | # Below is stub documentation for your module. You'd better edit it! |
---|
86 | |
---|
87 | =head1 NAME |
---|
88 | |
---|
89 | LATMOS::Accounts - Perl extension for blah blah blah |
---|
90 | |
---|
91 | =head1 SYNOPSIS |
---|
92 | |
---|
93 | use LATMOS::Accounts; |
---|
94 | blah blah blah |
---|
95 | |
---|
96 | =head1 DESCRIPTION |
---|
97 | |
---|
98 | Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the |
---|
99 | author of the extension was negligent enough to leave the stub |
---|
100 | unedited. |
---|
101 | |
---|
102 | Blah blah blah. |
---|
103 | |
---|
104 | =head2 EXPORT |
---|
105 | |
---|
106 | None by default. |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | =head1 SEE ALSO |
---|
111 | |
---|
112 | Mention other useful documentation such as the documentation of |
---|
113 | related modules or operating system documentation (such as man pages |
---|
114 | in UNIX), or any relevant external documentation such as RFCs or |
---|
115 | standards. |
---|
116 | |
---|
117 | If you have a mailing list set up for your module, mention it here. |
---|
118 | |
---|
119 | If you have a web site set up for your module, mention it here. |
---|
120 | |
---|
121 | =head1 AUTHOR |
---|
122 | |
---|
123 | Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt> |
---|
124 | |
---|
125 | =head1 COPYRIGHT AND LICENSE |
---|
126 | |
---|
127 | Copyright (C) 2009 by Thauvin Olivier |
---|
128 | |
---|
129 | This library is free software; you can redistribute it and/or modify |
---|
130 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
131 | at your option, any later version of Perl 5 you may have available. |
---|
132 | |
---|
133 | |
---|
134 | =cut |
---|