Config.pm 4.1 KB

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