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 | # ------------------------------------------------------------------------------ |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | # ------------------------------------------------------------------------------ |
---|
22 | package FCM::Context::Keyword; |
---|
23 | use base qw{FCM::Class::HASH}; |
---|
24 | |
---|
25 | use 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 | |
---|
31 | use 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 | |
---|
39 | sub 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 | # ------------------------------------------------------------------------------ |
---|
60 | package FCM::Context::Keyword::BrowserConfig; |
---|
61 | use base qw{FCM::Class::HASH}; |
---|
62 | |
---|
63 | __PACKAGE__->class({comp_pat => undef, loc_tmpl => '$', rev_tmpl => '$'}); |
---|
64 | |
---|
65 | # ------------------------------------------------------------------------------ |
---|
66 | package FCM::Context::Keyword::Entry; |
---|
67 | use base qw{FCM::Class::HASH}; |
---|
68 | |
---|
69 | __PACKAGE__->class({ |
---|
70 | key => {w => 0, isa => '$'}, |
---|
71 | value => {w => 0, isa => '$'}, |
---|
72 | }); |
---|
73 | |
---|
74 | # ------------------------------------------------------------------------------ |
---|
75 | package FCM::Context::Keyword::Entry::Location; |
---|
76 | use base qw{FCM::Context::Keyword::Entry}; |
---|
77 | |
---|
78 | my $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 |
---|
102 | sub is_implied { |
---|
103 | $_[0]->{implied}; |
---|
104 | } |
---|
105 | |
---|
106 | # ------------------------------------------------------------------------------ |
---|
107 | 1; |
---|
108 | __END__ |
---|
109 | |
---|
110 | =head1 NAME |
---|
111 | |
---|
112 | FCM::Context::Keyword |
---|
113 | |
---|
114 | =head1 SYNOPSIS |
---|
115 | |
---|
116 | use FCM::Context::Keyword; |
---|
117 | |
---|
118 | =head1 DESCRIPTION |
---|
119 | |
---|
120 | Provides a context object for the FCM keyword utility. All the classes described |
---|
121 | below are sub-classes of L<FCM::Class::HASH|FCM::Class::HASH>. |
---|
122 | |
---|
123 | =head1 OBJECTS |
---|
124 | |
---|
125 | =head2 FCM::Context::Keyword |
---|
126 | |
---|
127 | An object of this class is used to store a list of keyword entries. It has the |
---|
128 | following methods: |
---|
129 | |
---|
130 | =over 4 |
---|
131 | |
---|
132 | =item $instance->add_entry($key,$value,\%attrib) |
---|
133 | |
---|
134 | Creates and adds a new entry. |
---|
135 | |
---|
136 | =item $instance->add_entry($entry) |
---|
137 | |
---|
138 | Adds a new entry. |
---|
139 | |
---|
140 | =item $instance->get_entry_class() |
---|
141 | |
---|
142 | Returns the class of the entry stored by this context. |
---|
143 | |
---|
144 | =item $instance->get_entry_by_key() |
---|
145 | |
---|
146 | Returns a HASH reference to map the entry keys with the entry objects. |
---|
147 | |
---|
148 | =item $instance->get_entry_by_value() |
---|
149 | |
---|
150 | Returns a HASH reference to map the entry values with the entry objects. |
---|
151 | |
---|
152 | =back |
---|
153 | |
---|
154 | =head2 FCM::Context::Keyword::BrowserConfig |
---|
155 | |
---|
156 | An object of this class is used to store the configuration for mapping a |
---|
157 | location to a browser URL. It has the following attributes: |
---|
158 | |
---|
159 | =over 4 |
---|
160 | |
---|
161 | =item comp_pat |
---|
162 | |
---|
163 | The pattern for extracting components from a locator, for putting into the |
---|
164 | browser location template. |
---|
165 | |
---|
166 | =item loc_tmpl |
---|
167 | |
---|
168 | The browser location template. |
---|
169 | |
---|
170 | =item rev_tmpl |
---|
171 | |
---|
172 | The browser revision template. |
---|
173 | |
---|
174 | =back |
---|
175 | |
---|
176 | =head2 FCM::Context::Keyword::Entry |
---|
177 | |
---|
178 | This is used to store a simple keyword entry (e.g. for revision keywords). It |
---|
179 | has 2 attributes, the I<key> and the I<value>. |
---|
180 | |
---|
181 | =head2 FCM::Context::Keyword::Entry::Location |
---|
182 | |
---|
183 | This is a sub-class of FCM::Context::Keyword::Entry, and is used to store a |
---|
184 | location keyword entry. It has the following additional attributes: |
---|
185 | |
---|
186 | =over 4 |
---|
187 | |
---|
188 | =item browser_config |
---|
189 | |
---|
190 | The configuration L</FCM::Context::Keyword::BrowserConfig> for mapping this |
---|
191 | location to a browser URL. |
---|
192 | |
---|
193 | =item ctx_of_implied |
---|
194 | |
---|
195 | The context </FCM::Context::Keyword> object of the implied entries, if this |
---|
196 | entry is a primary location. |
---|
197 | |
---|
198 | =item ctx_of_rev |
---|
199 | |
---|
200 | The context </FCM::Context::Keyword> object of the revision keywords. |
---|
201 | |
---|
202 | =item implied |
---|
203 | |
---|
204 | A flag to indicate that this is an entry implied by a primary location. |
---|
205 | |
---|
206 | =item loaded_rev_prop |
---|
207 | |
---|
208 | A flag to indicate that a previous attempt is made to load revision keywords |
---|
209 | from the I<property> of the location. |
---|
210 | |
---|
211 | =item type |
---|
212 | |
---|
213 | The location type. |
---|
214 | |
---|
215 | =back |
---|
216 | |
---|
217 | =head1 COPYRIGHT |
---|
218 | |
---|
219 | (C) Crown copyright Met Office. All rights reserved. |
---|
220 | |
---|
221 | =cut |
---|