[BACK]Return to Config.pm CVS log [TXT][DIR] Up to [local] / RT / Invoicing / lib / RTI

Annotation of RT/Invoicing/lib/RTI/Config.pm, Revision 1.3

1.1       andrew      1: package RTI::Config;
                      2: use strict;
                      3: use warnings;
                      4:
                      5: use 5.010;
1.3     ! andrew      6: use experimental 'switch';
1.1       andrew      7:
1.2       andrew      8: use YAML::XS qw/ LoadFile Dump Load /;
1.1       andrew      9: use File::Spec;
                     10:
                     11: sub new {
                     12:     my ( $class, $args ) = @_;
                     13:
                     14:     my $self = { file => '', };
                     15:     bless $self, $class;
                     16:
                     17:     my $file = $args->{file} || $self->_find_config;
                     18:     $self->read_config($file);
                     19:
                     20:     return $self;
                     21: }
                     22:
                     23: sub _find_config {
                     24:     my ($self) = @_;
                     25:
                     26:     # XXX This needs to be better
                     27:     foreach my $file (qw/ rt_invoice.conf rt_invoice.cfg .rt_invoicerc /) {
                     28:         foreach my $dir ( '.', $ENV{HOME} . '/.rt_invoicing', $ENV{HOME} ) {
                     29:             my $path = join '/', $dir, $file;
                     30:             return $path if -e $path;
                     31:         }
                     32:     }
                     33:     return;
                     34: }
                     35:
                     36: sub read_config {
                     37:     my ( $self, $file ) = @_;
                     38:
                     39:     $file ||= $self->{file};
                     40:     die "$file: no such file\n" unless -e $file;
                     41:
                     42:     my $c = LoadFile($file) or die "Unable to load $file\n";
                     43:
                     44:     $c->{customers} ||= [];
                     45:     if ( $c->{default} ) {
                     46:         foreach my $cust ( @{ $c->{customers} } ) {
                     47:             foreach my $k ( keys %{ $c->{default} } ) {
                     48:                 $cust->{$k} //= Load( Dump( $c->{default}->{$k} ) );
                     49:             }
                     50:         }
                     51:     }
                     52:
                     53:     $self->{_config} = $c;
                     54:     $self->{file}    = $file;
                     55: }
                     56:
                     57: sub get {
                     58:     my ( $self, $key ) = @_;
                     59:     my $value = Load( Dump( $self->{_config}->{$key} ) );
                     60:
                     61:     my ( $volume, $directories, $file )
                     62:         = File::Spec->splitpath( File::Spec->rel2abs( $self->{file} ) );
                     63:
                     64:     given ($key) {
                     65:         when ('state') {
                     66:             if (!$value) {
                     67:                 $value = $self->{file};
                     68:                 $value =~ s/(?:\.[^.]+)?$/\.state/;
                     69:             }
                     70:         }
                     71:         when ('invoice_dir') {
                     72:             $value //= File::Spec->catdir( $volume, $directories, 'invoices' );
                     73:         }
                     74:         when ('template_dir') {
                     75:             $value //= File::Spec->catdir( $volume, $directories );
                     76:         }
                     77:         when ('invoice_template') {
                     78:             $value //= 'invoice.tex.tt';
                     79:         }
                     80:         when ('logo') {
                     81:             $value //= File::Spec->catfile( $volume, $directories, 'Logo.pdf' );
                     82:         }
                     83:         when ('customers') {
                     84:             $value = $self->_customers($value);
                     85:         }
                     86:     }
                     87:
                     88:     return $value;
                     89: }
                     90:
                     91: sub _customers {
                     92:     my ( $self, $customers ) = @_;
                     93:     my $default_cust = $self->new_customer;
                     94:
                     95:     foreach my $cust ( @{$customers} ) {
                     96:         foreach (keys %{ $default_cust }) {
                     97:             $cust->{$_} = $default_cust->{$_} unless exists $cust->{$_}
                     98:         }
                     99:     }
                    100:
                    101:     return $customers;
                    102: }
                    103:
                    104: sub new_customer {
                    105:     my ($self) = @_;
                    106:
                    107:     my $cust = $self->get('default');
                    108:
                    109:     return {
                    110:         day       => 0,
                    111:         per       => 'week',
                    112:         frequency => 1,
                    113:         hours     => { default => 0 },
                    114:         rates     => {},
                    115:         %{$cust}
                    116:     };
                    117: }
                    118:
                    119: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>