[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.1

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

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