1NAME
2 Metabase::Fact - base class for Metabase Facts
3
4VERSION
5 version 0.025
6
7SYNOPSIS
8 # defining the fact class
9 package MyFact;
10 use Metabase::Fact::Hash;
11 our @ISA = qw/Metabase::Fact::Hash/;
12
13 # using the fact class
14 my $fact = TestReport->new(
15 resource => 'RJBS/Metabase-Fact-0.001.tar.gz',
16 content => {
17 status => 'FAIL',
18 time => 3029,
19 },
20 );
21
22 $client->send_fact($fact);
23
24DESCRIPTION
25 Metabase is a framework for associating content and metadata with
26 arbitrary resources. A Metabase can be used to store test reports,
27 reviews, coverage analysis reports, reports on static analysis of coding
28 style, or anything else for which datatypes are constructed.
29
30 Metabase::Fact is a base class for Facts (really opinions or analyses)
31 that can be sent to or retrieved from a Metabase repository.
32
33 Structure of a Fact object
34 A Fact object associates a "content" attribute with a "resource"
35 attribute and a "creator" attribute.
36
37 The "resource" attribute must be in a URI format that can be validated
38 via a Metabase::Resource subclass. The "content" attribute is an opaque
39 scalar with subclass-specific meaning. The "creator" attribute is a URI
40 with a "metabase:user" scheme and type (see
41 Metabase::Resource::metabase).
42
43 Facts have three sets of metadata associate with them. Metadata are
44 generally for use in indexing, searching and managing Facts.
45
46 * "core metadata" describe universal properties of all Facts and are
47 used to submit, store, manage and retrieve Facts within the Metabase
48 framework.
49
50 * "resource metadata" describe index properties derived from the
51 "resource" attribute. (As these can be regenerated from the
52 "resource" -- which is part of "core metadata" -- they are not
53 stored with a serialized Fact.)
54
55 * "content metadata" describe index properties derived from the
56 "content" attribute. (As these can be regenerated from the "content"
57 -- which is part of "core metadata" -- they are not stored with a
58 serialized Fact.)
59
60 Each of the three metadata sets has an associated accessor:
61 "core_metadata", "resource_metadata" and "content_metadata".
62
63 Each of the three sets also has an accessor that returns a hashref with
64 a data type for each possible element in the set: "core_metadata_types",
65 "resource_metadata_types" and "content_metadata_types".
66
67 Data types are loosely based on Data::RX. For example:
68
69 '//str' -- indicates a value that should be compared stringwise
70 '//num' -- indicates a value that should be compared numerically
71 '//bool' -- indicates a valut that is true or false
72
73 When searching on metadata, you must join the set name to the metadata
74 element name with a period character. For example:
75
76 core.guid
77 core.creator
78 core.resource
79 resource.scheme
80 content.size
81 content.score
82
83ATTRIBUTES
84 Unless otherwise noted, all attributes are read-only and are either
85 provided as arguments to the constructor or are generated during
86 construction. All attributes (except "content") are also part of "core
87 metadata".
88
89 Arguments provided to new
90 content (required)
91 A reference to the actual information associated with the fact. The
92 exact form of the content is up to each Fact class to determine.
93
94 resource (required)
95 The canonical resource (URI) the Fact relates to. For CPAN
96 distributions, this would be a "cpan:///distfile/..." URI. (See
97 URI::cpan.) The associated accessor returns a Metabase::Resource
98 subclass.
99
100 creator (optional)
101 A Metabase::User::Profile URI that indicates the creator of the Fact. If
102 not set during Fact creation, it will be set by the Metabase when a Fact
103 is submitted based on the submitter Profile. The "set_creator" mutator
104 may be called to set "creator", but only if it is not previously set.
105 The associated accessor returns a Metabase::Resource subclass or "undef"
106 if the creator has not been set.
107
108 guid (optional)
109 The Fact object's Globally Unique IDentifier. This is generated
110 automatically if not provided. Generally, users should not provide a
111 "guid" argument, but it is permitted for use in special cases where a
112 non-random "guid" is necessary.
113
114 Generated during construction
115 These attributes are generated automatically during the call to "new".
116
117 type
118 The class name, with double-colons converted to dashes to be more
119 URI-friendly. e.g. "Metabase::Fact" would be "Metabase-Fact".
120
121 schema_version
122 The "schema_version" of the Fact subclass that created the object. This
123 may or may not be the same as the current "schema_version" of the class
124 if newer versions of the class have been released since the object was
125 created.
126
127 creation_time
128 Fact creation time in UTC expressed in extended ISO 8601 format with a
129 "Z" (Zulu) suffix. For example:
130
131 2010-01-10T12:34:56Z
132
133 update_time
134 When the fact was created, stored or otherwise updated, expressed an ISO
135 8601 UTC format as with "creation_time". The "touch" method may be
136 called at any time to update the value to the current time. This
137 attribute generally only has local significance within a particular
138 Metabase repository. For example, it may be used to sort Facts by when
139 they were stored or changed in a Metabase.
140
141 valid
142 A boolean value indicating whether the fact is considered valid. It
143 defaults to true. The "set_valid" method may be called to change the
144 "valid" property, for example, to mark a fact invalid rather than
145 deleting it. The value of "valid" is always normalized to return "1" for
146 true and "0" for false.
147
148CONSTRUCTOR
149 new
150 $fact = MyFact->new(
151 resource => 'AUTHORID/Foo-Bar-1.23.tar.gz',
152 content => $content_structure,
153 );
154
155 Constructs a new Fact. The "resource" and "content" attributes are
156 required. No other attributes should be provided to "new" except
157 "creator".
158
159CLASS METHODS
160 type
161 $type = MyFact->type;
162
163 The "type" accessor may also be called as a class method.
164
165 class_from_type
166 $class = MyFact->class_from_type( $type );
167
168 A utility function to invert the operation of the "type" method.
169
170 upgrade_fact
171 MyFact->upgrade_fact( $struct );
172
173 This method will be called when initializing a fact from a data
174 structure that claims to be of a schema version other than the schema
175 version reported by the loaded class's "default_schema_version" method.
176 It will be passed the hashref of args being used to initialized the fact
177 object (generally the output of "as_struct" from an older version), and
178 should alter that hash in place.
179
180 default_schema_version
181 $version = MyFact->default_schema_version;
182
183 Defaults to 1. Subclasses should override this method if they make a
184 backwards-incompatible change to the internals of the content attribute.
185 Schema version numbers should be monotonically-increasing integers. The
186 default schema version is used to set an objects schema_version
187 attribution on creation.
188
189PERSISTENCE METHODS
190 The following methods are implemented by Metabase::Fact and subclasses
191 generally should not need to override them.
192
193 save
194 $fact->save($filename);
195
196 This method writes out the fact to a file in JSON format. If the file
197 cannot be written, an exception is raised. If the save is successful, a
198 true value is returned. Internally, it calls "as_json".
199
200 load
201 my $fact = Metabase::Fact->load($filename);
202
203 This method loads a fact from a JSON format file and returns it. If the
204 file cannot be read or is not valid JSON, and exception is thrown.
205 Internally, it calls "from_json".
206
207 as_json
208 This returns a JSON string containing the serialized object. Internally,
209 it calls "as_struct".
210
211 from_json
212 This method regenerates a fact from a JSON string generated by
213 "as_json". Internally, it calls "from_struct".
214
215 as_struct
216 This returns a simple data structure that represents the fact and can be
217 used for transmission over the wire. It serializes the content and core
218 metadata, but not other metadata, which should be recomputed by the
219 receiving end.
220
221 from_struct
222 my $fact = Metabase::Fact->from_struct( $struct );
223
224 This takes the output of the "as_struct" method and reconstitutes a Fact
225 object. If the class the struct represents is not loaded, "from_struct"
226 will attempt to load the class or will throw an error.
227
228OBJECT METHODS
229 The following methods are implemented by Metabase::Fact and subclasses
230 generally should not need to override them.
231
232 core_metadata
233 This returns a hashref containing the fact's core metadata. This
234 includes things like the guid, creation time, described resource, and so
235 on.
236
237 core_metadata_types
238 This returns a hashref of types for each core metadata element
239
240 resource_metadata
241 This method returns metadata describing the resource.
242
243 resource_metadata_types
244 This returns a hashref of types for each resource metadata element
245
246 set_creator
247 $fact->set_creator($profile_uri);
248
249 This method sets the "creator" core metadata for the core metadata for
250 the fact. If the fact's "creator" is already set, an exception will be
251 thrown.
252
253 set_valid
254 $fact->set_valid(0);
255
256 This method sets the "valid" core metadata to a boolean value.
257
258 touch
259 $fact->touch
260
261 This method sets the "update_time" core metadata for the core metadata
262 for the fact to the current time in ISO 8601 UTC format with a trailing
263 "Z" (Zulu) suffice.
264
265ABSTRACT METHODS
266 Methods marked as required must be implemented by a Fact subclass. (The
267 version in Metabase::Fact will die with an error if called.)
268
269 In the documentation below, the terms must, must not, should, etc. have
270 their usual RFC 2119 meanings.
271
272 These methods MUST throw an exception if an error occurs.
273
274 content_as_bytes
275 required
276
277 $string = $fact->content_as_bytes;
278
279 This method MUST serialize a Fact's content as bytes in a scalar and
280 return it. The method for serialization is up to the individual fact
281 class to determine. Some common subclasses are available to handle
282 serialization for common data types. See Metabase::Fact::Hash and
283 Metabase::Fact::String.
284
285 content_from_bytes
286 required
287
288 $content = $fact->content_from_bytes( $string );
289 $content = $fact->content_from_bytes( \$string );
290
291 Given a scalar, this method MUST regenerate and return the original
292 content data structure. It MUST accept either a string or string
293 reference as an argument. It MUST NOT overwrite the Fact's content
294 attribute directly.
295
296 content_metadata
297 optional
298
299 $content_meta = $fact->content_metadata;
300
301 If provided, this method MUST return a hash reference with
302 content-specific indexing metadata. The key MUST be the name of the
303 field for indexing and SHOULD provide dimensions to differentiate one
304 set of content from another. Values MUST be simple scalars, not
305 references.
306
307 Here is a hypothetical example of "content_metadata" for an image fact:
308
309 sub content_metadata {
310 my $self = shift;
311 return {
312 width => _compute_width ( $self->content ),
313 height => _compute_height ( $self->content ),
314 caption => _extract_caption( $self->content ),
315 }
316 }
317
318 Field names should be valid perl identifiers, consisting of alphanumeric
319 characters or underscores. Hyphens and periods are allowed, but are not
320 recommended.
321
322 content_metadata_types
323 optional
324
325 my $typemap = $fact->content_metadata_types;
326
327 This method is used to identify the datatypes of keys in the data
328 structure provided by "content_metadata". If provided, it MUST return a
329 hash reference. It SHOULD contain a key for every key that could appear
330 in the data structure generated by "content_metadata" and provide a
331 value corresponding to a datatype for each key. It MAY contain keys that
332 do not always appear in the result of "content_metadata".
333
334 Data types are loosely based on Data::RX. Type SHOULD be one of the
335 following:
336
337 '//str' -- indicates a value that should be compared stringwise
338 '//num' -- indicates a value that should be compared numerically
339 '//bool' -- indicates a boolean value where "1" is true and "0" is false
340
341 Here is a hypothetical example of "content_metadata_types" for an image
342 fact:
343
344 sub content_metadata_types {
345 return {
346 width => '//num',
347 height => '//num',
348 caption => '//str',
349 }
350 }
351
352 Consumers of "content_metadata_types" SHOULD assume that any
353 "content_metadata" key not found in the result of
354 "content_metadata_types" is a '//str' resource.
355
356 validate_content
357 required
358
359 eval { $fact->validate_content };
360
361 This method SHOULD check for the validity of content within the Fact. It
362 MUST throw an exception if the fact content is invalid. (The return
363 value is ignored.)
364
365 validate_resource
366 optional
367
368 eval { $fact->validate_resource };
369
370 This method SHOULD check whether the resource type is relevant for the
371 Fact subclass. It SHOULD use Metabase::Resource to create a resource
372 object and evaluate the resource object scheme and type. It MUST throw
373 an exception if the resource type is invalid. Otherwise, it MUST return
374 a valid Metabase::Resource subclass. For example:
375
376 sub validate_resource {
377 my ($self) = @_;
378 # Metabase::Resource->new dies if invalid
379 my $obj = Metabase::Resource->new($self->resource);
380 if ($obj->scheme eq 'cpan' && $obj->type eq 'distfile') {
381 return $obj;
382 }
383 else {
384 my $fact_type = $self->type;
385 Carp::confess("'$resource' does not apply to '$fact_type'");
386 }
387 }
388
389 The default "validate_resource" accepts any resource that can initialize
390 a "Metabase::Resource" object.
391
392BUGS
393 Please report any bugs or feature using the CPAN Request Tracker. Bugs
394 can be submitted through the web interface at
395 <http://rt.cpan.org/Dist/Display.html?Queue=Metabase-Fact>
396
397 When submitting a bug or request, please include a test-file or a patch
398 to an existing test-file that illustrates the bug or desired feature.
399
400SUPPORT
401 Bugs / Feature Requests
402 Please report any bugs or feature requests through the issue tracker at
403 <https://github.com/dagolden/Metabase-Fact/issues>. You will be notified
404 automatically of any progress on your issue.
405
406 Source Code
407 This is open source software. The code repository is available for
408 public review and contribution under the terms of the license.
409
410 <https://github.com/dagolden/Metabase-Fact>
411
412 git clone https://github.com/dagolden/Metabase-Fact.git
413
414AUTHORS
415 * David Golden <dagolden@cpan.org>
416
417 * Ricardo Signes <rjbs@cpan.org>
418
419 * H.Merijn Brand <hmbrand@cpan.org>
420
421CONTRIBUTORS
422 * David Steinbrunner <dsteinbrunner@pobox.com>
423
424 * Karen Etheridge <ether@cpan.org>
425
426 * Nathan Gary Glenn <nglenn@cpan.org>
427
428 * Randy Stauner <rwstauner@cpan.org>
429
430COPYRIGHT AND LICENSE
431 This software is Copyright (c) 2016 by David Golden.
432
433 This is free software, licensed under:
434
435 The Apache License, Version 2.0, January 2004
436
437
1README.hacking -- notes for contributors
2----------------------------------------
3
4CODING STYLE
5
6Not all old code has been cleaned up, but going forward, please follow these
7conventions:
8
9* 2-space indentation with spaces, not tabs
10
11DEPENDENCIES DURING DEVELOPMENT (AKA USING MYLIB)
12
13It can be annoying to manage dependencies during development when testing
14metabase distributions as they often depend on other metabase distributions
15also under development.
16
17There are a few options
18(1) set PERL5LIB to include all metabase project files
19(2) continually install all metabase project files while developing
20(3) use "-Mylib" and the .mylib file
21
22For (3), install the "ylib" module from CPAN. Then, when you invoke "perl
23-Mylib", all directories listed in the .mylib file will be added to @INC. For
24Makefile.PL, you'll need to make sure that -Mylib is used by perl as invoked
25in the Makefile:
26
27 $ perl -Mylib Makefile.PL PERL="`which perl` -Mylib"
28
29
30