New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Keyword.pm in vendors/lib/FCM/Context – NEMO

source: vendors/lib/FCM/Context/Keyword.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 5.8 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
3#
4# This file is part of FCM, tools for managing and building source code.
5#
6# FCM is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# FCM is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with FCM. If not, see <http://www.gnu.org/licenses/>.
18# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21# ------------------------------------------------------------------------------
22package FCM::Context::Keyword;
23use base qw{FCM::Class::HASH};
24
25use constant {
26    BROWSER_CONFIG    => 'FCM::Context::Keyword::BrowserConfig',
27    ENTRY             => 'FCM::Context::Keyword::Entry',
28    ENTRY_OF_LOCATION => 'FCM::Context::Keyword::Entry::Location',
29};
30
31use Scalar::Util qw{blessed};
32
33__PACKAGE__->class({
34    entry_class    => {w => 0, isa => '$', default => ENTRY_OF_LOCATION},
35    entry_by_key   => {w => 0, isa => '%'},
36    entry_by_value => {w => 0, isa => '%'},
37});
38
39sub add_entry {
40    my $self = shift();
41    my ($key, $value, $entry);
42    if (blessed($_[0])) {
43        $entry = $_[0];
44        $key   = $entry->get_key();
45        $value = $entry->get_value();
46    }
47    else {
48        ($key, $value, my $attrib_ref) = @_;
49        $attrib_ref ||= {};
50        $entry = $self->get_entry_class()->new({
51            key => lc($key), value => $value, %{$attrib_ref},
52        });
53    }
54    $self->{entry_by_key}{lc($key)} = $entry;
55    $self->{entry_by_value}{$value} = $entry;
56    return $entry;
57}
58
59# ------------------------------------------------------------------------------
60package FCM::Context::Keyword::BrowserConfig;
61use base qw{FCM::Class::HASH};
62
63__PACKAGE__->class({comp_pat => undef, loc_tmpl => '$', rev_tmpl => '$'});
64
65# ------------------------------------------------------------------------------
66package FCM::Context::Keyword::Entry;
67use base qw{FCM::Class::HASH};
68
69__PACKAGE__->class({
70    key   => {w => 0, isa => '$'},
71    value => {w => 0, isa => '$'},
72});
73
74# ------------------------------------------------------------------------------
75package FCM::Context::Keyword::Entry::Location;
76use base qw{FCM::Context::Keyword::Entry};
77
78my $CTX = 'FCM::Context::Keyword';
79
80__PACKAGE__->class(
81    {   browser_config  => $CTX->BROWSER_CONFIG,
82        ctx_of_implied  => $CTX,
83        ctx_of_rev      => $CTX,
84        implied         => {isa => '$', default => 0},
85        key             => {isa => '$', w => 0},
86        loaded_rev_prop => '$',
87        type            => '$',
88        value           => {isa => '$', w => 0},
89    },
90    {   init => sub {
91            my ($self) = @_;
92            if (!$self->get_implied()) {
93                $self->{browser_config} = $CTX->BROWSER_CONFIG->new();
94                $self->{ctx_of_implied} = $CTX->new();
95                $self->{ctx_of_rev} = $CTX->new({entry_class => $CTX->ENTRY});
96            }
97        },
98    },
99);
100
101# Returns true if this is an implied entry
102sub is_implied {
103    $_[0]->{implied};
104}
105
106# ------------------------------------------------------------------------------
1071;
108__END__
109
110=head1 NAME
111
112FCM::Context::Keyword
113
114=head1 SYNOPSIS
115
116    use FCM::Context::Keyword;
117
118=head1 DESCRIPTION
119
120Provides a context object for the FCM keyword utility. All the classes described
121below are sub-classes of L<FCM::Class::HASH|FCM::Class::HASH>.
122
123=head1 OBJECTS
124
125=head2 FCM::Context::Keyword
126
127An object of this class is used to store a list of keyword entries. It has the
128following methods:
129
130=over 4
131
132=item $instance->add_entry($key,$value,\%attrib)
133
134Creates and adds a new entry.
135
136=item $instance->add_entry($entry)
137
138Adds a new entry.
139
140=item $instance->get_entry_class()
141
142Returns the class of the entry stored by this context.
143
144=item $instance->get_entry_by_key()
145
146Returns a HASH reference to map the entry keys with the entry objects.
147
148=item $instance->get_entry_by_value()
149
150Returns a HASH reference to map the entry values with the entry objects.
151
152=back
153
154=head2 FCM::Context::Keyword::BrowserConfig
155
156An object of this class is used to store the configuration for mapping a
157location to a browser URL. It has the following attributes:
158
159=over 4
160
161=item comp_pat
162
163The pattern for extracting components from a locator, for putting into the
164browser location template.
165
166=item loc_tmpl
167
168The browser location template.
169
170=item rev_tmpl
171
172The browser revision template.
173
174=back
175
176=head2 FCM::Context::Keyword::Entry
177
178This is used to store a simple keyword entry (e.g. for revision keywords). It
179has 2 attributes, the I<key> and the I<value>.
180
181=head2 FCM::Context::Keyword::Entry::Location
182
183This is a sub-class of FCM::Context::Keyword::Entry, and is used to store a
184location keyword entry. It has the following additional attributes:
185
186=over 4
187
188=item browser_config
189
190The configuration L</FCM::Context::Keyword::BrowserConfig> for mapping this
191location to a browser URL.
192
193=item ctx_of_implied
194
195The context </FCM::Context::Keyword> object of the implied entries, if this
196entry is a primary location.
197
198=item ctx_of_rev
199
200The context </FCM::Context::Keyword> object of the revision keywords.
201
202=item implied
203
204A flag to indicate that this is an entry implied by a primary location.
205
206=item loaded_rev_prop
207
208A flag to indicate that a previous attempt is made to load revision keywords
209from the I<property> of the location.
210
211=item type
212
213The location type.
214
215=back
216
217=head1 COPYRIGHT
218
219(C) Crown copyright Met Office. All rights reserved.
220
221=cut
Note: See TracBrowser for help on using the repository browser.