File Coverage

blib/lib/TM/ObjectAble.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package TM::ObjectAble;
2              
3 1     1   674 use strict;
  1         2  
  1         41  
4 1     1   7 use warnings;
  1         3  
  1         42  
5              
6 1     1   7 use Data::Dumper;
  1         1  
  1         59  
7              
8 1     1   540 use Class::Trait 'base';
  0            
  0            
9              
10             =pod
11              
12             =head1 NAME
13              
14             TM::Synchronizable - Topic Maps, trait for storing objects into backends
15              
16             =head1 SYNOPSIS
17              
18             my $tm = .... # get a topic map from somewhere
19             use Class::Trait;
20             Class::Trait->apply ($tm, "TM::ObjectAble");
21              
22             my %store; # find yourself a proper store, can be anything HASHish
23             # append it to the list of stores, or ....
24             push @{ $tm->storages }, \%store;
25             # prepend it to the list of stores
26             unshift @{ $tm->storages }, \%store;
27              
28             # store it (the proper storage will take it)
29             $tm->objectify ('tm:some-topic', "whatever object or data");
30              
31             # get it back
32             my @objects = $tm->object ('tm:some-topic', 'tm:some-topic2');
33              
34             # get rid of it
35             $tm->deobjectify ('tm:some-topic');
36              
37             =cut
38              
39             =head1 DESCRIPTION
40              
41             This trait implements functionality to store arbitrary data on a per-topic basis.
42              
43             Conceptually, the storage can be thought as one large hash, as keys being use the internal topic
44             identifiers, as values the object data. But to allow different topics to store their object data in
45             different places, this interface works with a list of such hashes. Each hash (native or tied to some
46             implementation) in the list is visited (starting from the start of the list) and can take over the
47             storage. Whether this is based on the topic id, on some other topic information, or on the MIME type
48             of the data (if it has one), is up to the implementation to decide.
49              
50             =head1 INTERFACE
51              
52             =head2 Methods
53              
54             =over
55              
56             =item B
57              
58             I<$listref> = I<$tm>->storages
59              
60              
61             This method returns an array reference. You can C or C your storage implementation
62             onto this list.
63              
64             Example:
65              
66             my %store1;
67             push @{ $tm->storages }, \%store1
68              
69              
70             =cut
71              
72             sub storages {
73             my $self = shift;
74             $self->{'.storages'} //= [];
75             return $self->{'.storages'}
76             }
77              
78             =pod
79              
80             =item B
81              
82             I<$tm>->objectify (I<$tid> => I<$some_data>, ...);
83              
84             This method stores actually the data. It takes a hash, with the topic id as keys and according
85             values and tries to find for each of the pairs an appropriate storage. If none can be found, it will
86             raise an exception.
87              
88             B: Yes, this is a stupid name.
89              
90             =cut
91              
92             sub objectify {
93             my $self = shift;
94             my $storages = $self->{'.storages'};
95             my %bs = @_;
96              
97             OBJECT:
98             while (my ($tid, $obj) = each %bs) { # go through the parameter list
99             foreach my $store (@$storages) { # now look at all registered storages
100             next OBJECT if $store->{$tid} = $obj; # find the one which actually stores it (by returning the value)
101             }
102             die "no storage dispatched for $tid"; # if no storage found itself => exception raised
103             }
104             }
105              
106             =pod
107              
108             =item B
109              
110             I<$tm>->deobjectify (I<$tid>, ...)
111              
112             This method removes any data stored for the provided topic(s). If no data can be found in the
113             appropriate storage, an exception will be raised.
114              
115             =cut
116              
117             sub deobjectify {
118             my $self = shift;
119             my $storages = $self->{'.storages'};
120              
121             OBJECT:
122             foreach my $tid (@_) { # go through the parameter list
123             foreach my $store (@$storages) { # now look at all registered storages
124             next OBJECT if delete $store->{$tid}; # find the one which actually stored it (by returning the value)
125             }
126             die "no storage dispatched for $tid"; # if no storage found itself => exception raised
127             }
128              
129             }
130              
131             =pod
132              
133             =item B
134              
135             I<@objects> = I<$tm>->object (I<$tid>, ...)
136              
137             This method returns any data stored for the provided objects. If no data can be found for a
138             particular topic, then C will be returned.
139              
140             =cut
141              
142             sub object {
143             my $self = shift;
144             my $storages = $self->{'.storages'};
145              
146             my @os;
147             foreach my $tid (@_) {
148             my $o;
149             foreach my $store (@$storages) { # now look at all registered storages
150             ($o = $store->{$tid}) and last; # find the one which actually stored it (by returning the value)
151             }
152             push @os, $o;
153             }
154             return @os;
155             }
156              
157             =pod
158              
159             =back
160              
161             =head1 SEE ALSO
162              
163             L
164              
165             =head1 AUTHOR INFORMATION
166              
167             Copyright 20(10), Robert Barta , All rights reserved.
168              
169             This library is free software; you can redistribute it and/or modify it under the same terms as Perl
170             itself. http://www.perl.com/perl/misc/Artistic.html
171              
172             =cut
173              
174             our $VERSION = 0.1;
175              
176             1;
177              
178             __END__