[1980] | 1 | # ------------------------------------------------------------------------------ |
---|
| 2 | # (C) Crown copyright Met Office. All rights reserved. |
---|
| 3 | # For further details please refer to the file COPYRIGHT.txt |
---|
| 4 | # which you should have received as part of this distribution. |
---|
| 5 | # ------------------------------------------------------------------------------ |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
| 9 | package Fcm::Keyword::Config; |
---|
| 10 | |
---|
| 11 | use Carp; |
---|
| 12 | use Fcm::Keyword::Entries; |
---|
| 13 | use Fcm::Keyword::Exception; |
---|
| 14 | use Fcm::Util::ClassLoader; |
---|
| 15 | |
---|
| 16 | our %CONFIG_OF = ( |
---|
| 17 | LOCATION_ENTRIES => { |
---|
| 18 | entry_class => 'Fcm::Keyword::Entry::Location', |
---|
| 19 | loaders => [ |
---|
| 20 | { |
---|
| 21 | class => 'Fcm::Keyword::Loader::Config::Location', |
---|
| 22 | }, |
---|
| 23 | ], |
---|
| 24 | }, |
---|
| 25 | REVISION_ENTRIES => { |
---|
| 26 | entry_class => 'Fcm::Keyword::Entry', |
---|
| 27 | loaders => [ |
---|
| 28 | { |
---|
| 29 | class => 'Fcm::Keyword::Loader::Config::Revision', |
---|
| 30 | options => [{key => 'namespace', valuekey => 'key'}], |
---|
| 31 | }, |
---|
| 32 | { |
---|
| 33 | class => 'Fcm::Keyword::Loader::VC::Revision', |
---|
| 34 | options => [{key => 'source', valuekey => 'value'}], |
---|
| 35 | }, |
---|
| 36 | ], |
---|
| 37 | }, |
---|
| 38 | ); |
---|
| 39 | |
---|
| 40 | ################################################################################ |
---|
| 41 | # Returns a Fcm::Keyword::Entries object for given configuration |
---|
| 42 | sub get_entries { |
---|
| 43 | my ($context, $args_ref) = @_; |
---|
| 44 | if (!exists($CONFIG_OF{$context})) { |
---|
| 45 | croak(Fcm::Keyword::Exception->new({message => sprintf( |
---|
| 46 | "%s: keyword configuration not found", $context, |
---|
| 47 | )})); |
---|
| 48 | } |
---|
| 49 | my $config_ref = $CONFIG_OF{$context}; |
---|
| 50 | my @loaders; |
---|
| 51 | if (exists($config_ref->{loaders})) { |
---|
| 52 | for my $loader_config (@{$config_ref->{loaders}}) { |
---|
| 53 | my $class = $loader_config->{class}; |
---|
| 54 | Fcm::Util::ClassLoader::load($class); |
---|
| 55 | my %options; |
---|
| 56 | if (exists($loader_config->{options})) { |
---|
| 57 | for my $option_ref (@{$loader_config->{options}}) { |
---|
| 58 | my $key = $option_ref->{key}; |
---|
| 59 | my $value; |
---|
| 60 | if (exists($option_ref->{value})) { |
---|
| 61 | $value = $option_ref->{value}; |
---|
| 62 | } |
---|
| 63 | elsif ( |
---|
| 64 | exists($option_ref->{valuekey}) |
---|
| 65 | && $args_ref |
---|
| 66 | && ref($args_ref) eq 'HASH' |
---|
| 67 | && exists($args_ref->{$option_ref->{valuekey}}) |
---|
| 68 | ) { |
---|
| 69 | $value = $args_ref->{$option_ref->{valuekey}}; |
---|
| 70 | } |
---|
| 71 | $options{$key} = $value; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | push @loaders, $class->new(\%options); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | my %entries_options = ( |
---|
| 78 | (@loaders ? (loaders => \@loaders) : ()), |
---|
| 79 | ( |
---|
| 80 | exists($config_ref->{entry_class}) |
---|
| 81 | ? (entry_class => $config_ref->{entry_class}) |
---|
| 82 | : () |
---|
| 83 | ), |
---|
| 84 | ); |
---|
| 85 | return Fcm::Keyword::Entries->new(\%entries_options); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | 1; |
---|
| 89 | __END__ |
---|
| 90 | |
---|
| 91 | =head1 NAME |
---|
| 92 | |
---|
| 93 | Fcm::Keyword::Config |
---|
| 94 | |
---|
| 95 | =head1 SYNOPSIS |
---|
| 96 | |
---|
| 97 | use Fcm::Keyword::Config; |
---|
| 98 | |
---|
| 99 | =head1 DESCRIPTION |
---|
| 100 | |
---|
| 101 | This module stores the default configuration used by modules in the |
---|
| 102 | L<Fcm::Keyword> family. |
---|
| 103 | |
---|
| 104 | =head1 FUNCTIONS |
---|
| 105 | |
---|
| 106 | =over 4 |
---|
| 107 | |
---|
| 108 | =item get_entries($context,$args_ref) |
---|
| 109 | |
---|
| 110 | Returns a L<Fcm::Keyword::Entries|Fcm::Keyword::Entries> object for a given |
---|
| 111 | $context. If there is no matching $context in the configuration, croak() with a |
---|
| 112 | L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>. $args_ref is an optional |
---|
| 113 | argument, which should be a reference to a hash containing a I<key> and a |
---|
| 114 | I<value> element. It can be used by this function to set up the constructor |
---|
| 115 | options in the loaders of the returned |
---|
| 116 | L<Fcm::Keyword::Entries|Fcm::Keyword::Entries> object. |
---|
| 117 | |
---|
| 118 | =back |
---|
| 119 | |
---|
| 120 | =head1 DIAGNOSTICS |
---|
| 121 | |
---|
| 122 | =head1 TO DO |
---|
| 123 | |
---|
| 124 | Allow configuration to be changed in runtime. |
---|
| 125 | |
---|
| 126 | Convert this module to OO? |
---|
| 127 | |
---|
| 128 | Separate configuration from logic if this module becomes any bigger. |
---|
| 129 | |
---|
| 130 | Unit tests. |
---|
| 131 | |
---|
| 132 | =head1 SEE ALSO |
---|
| 133 | |
---|
| 134 | L<Fcm::Keyword|Fcm::Keyword>, |
---|
| 135 | L<Fcm::Keyword::Entries|Fcm::Keyword::Entries>, |
---|
| 136 | L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>, |
---|
| 137 | L<Fcm::Keyword::Entry::Location|Fcm::Keyword::Entry::Location> |
---|
| 138 | |
---|
| 139 | =head1 COPYRIGHT |
---|
| 140 | |
---|
| 141 | E<169> Crown copyright Met Office. All rights reserved. |
---|
| 142 | |
---|
| 143 | =cut |
---|