Simple/Engine/Memory.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 169 CC 9 SCAR 18.1
CC
9
Cov
0%
path_exists
20.0
check_lookup
12.0
get_handle
12.0
CRAP 6.1
1 package Repository::Simple::Engine::Memory;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.05';
7  
8 use Carp;
9 use IO::Scalar;
10 use Repository::Simple::Engine qw( :exists_constants );
11 use Repository::Simple::Type::Node;
12 use Repository::Simple::Type::Property;
13 use Repository::Simple::Type::Value::Scalar;
14  
15 use base 'Repository::Simple::Engine';
16  
17 =head1 NAME
18  
19 Repository::Simple::Engine::Memory - Transient repository storage in memory
20  
21 =head1 SYNOPSIS
22  
23 use Repository::Simple;
24 my $mem = Repository::Simple->attach('Memory');
25  
26 =head1 DESCRIPTION
27  
28 This repository uses a transient memory store to store all nodes and properties. This is very simple and cnfigurable.
29  
30 =head1 OPTIONS
31  
32 You may create this repository with no options to accept all defaults. This will give you an empty repository (i.e., containing only a root node, with no properties). Until writes are implemented, this isn't at all useful, so you will probably want to issue a "root" option to at least specify a structure for the repository.
33  
34 If you do specify options, you may specify any of the following:
35  
36 =over
37  
38 =item namespaces
39  
40 This option accepts a hash of namespace prefixes to namespace URIs. This allows you to customize the namespaces used by the storage engine. By default, this contains a single entry "mem".
41  
42 =item node_types
43  
44 This option allows you to create an arbitrary set of node types for storing information in the repository. This should be an array of hashes. Each hash will be passed to the constructor of L<Repository::Simple::Type::Node> (with the "engine" argument added to refer to the newly created engine).
45  
46 By default, a single node type, "mem:generic-node" is created, which provides very few constraints.
47  
48 =item property_types
49  
50 This option allows you to create an arbitrary set of property types for storing information in the repository. This should be an array of hashes. Each hash will be passed to the constructor of L<Repository::Simple::Type::Property> (with the "engine" argument added to refer to the newly created engine).
51  
52 By default, a single node type, "mem:generic-property" is created, which provides very few constraints.
53  
54 =item root
55  
56 This option establishes what nodes are initially in the repository. If this option is not included, the repository will be empty, except for the root. If you specify a custom set of node types and do not include "mem:generic-node", you must define this option as the default setting will attempt to create a node with that node type.
57  
58 The option should be set to a hash reference representing the root node. It must have the "node_type" key set to the name of the node type the root node should have. It may, optionally, also have a "nodes" and a "properties" key.
59  
60 The "nodes" key, if present, should point to a hash reference where each key is the name of each child node and each hash will have the same form as the root value. Thus, this structure is recursive, each node will be represented by a hash containing a "node_type" key and optionally "nodes" and "properties" keys.
61  
62 The "properties" key, if present, should point to a hash reference where each eky is the name of each child property. Each hash should contain a "property_type" pointing to the name of the property_type for that value. It may also have a "value" key pointing to the properties value, stored as a scalar value or reference.
63  
64 For example,
65  
66 my %settings = (
67 root => {
68 node_type => 'mem:generic',
69 properties => {
70 'foo' => {
71 property_type => 'mem:generic-property',
72 value => 1,
73 },
74 'bar' => {
75 property_type => 'mem:generic-property',
76 value => 2,
77 },
78 },
79 nodes => {
80 'baz' => {
81 node_type => 'mem:generic',
82 nodes => {
83 'qux' => {
84 node_type => 'mem:generic',
85 properties => {
86 quux => {
87 property_type => 'mem:generic-property',
88 value => 3,
89 },
90 },
91 },
92 },
93 },
94 },
95 },
96 );
97 my $repository = Repository::Simple->attach(Memory => %settings);
98  
99 =back
100  
101 =cut
102  
103 my %default_settings = (
104 namespaces => {
105 mem => 'http://contentment.org/Repository/Simple/Engine/Memory',
106 },
107 node_types => [
108 {
109 name => 'mem:generic-node',
110 property_types => {
111 '*' => [ 'mem:generic-property' ],
112 },
113 node_types => {
114 '*' => [ 'mem:generic-node' ],
115 },
116 auto_created => 0,
117 updatable => 1,
118 removable => 1,
119 },
120 ],
121 property_types => [
122 {
123 name => 'mem:generic-property',
124 auto_created => 0,
125 updatable => 1,
126 removable => 1,
127 value_type => Repository::Simple::Type::Value::Scalar->new,
128 },
129 ],
130 root => {
131 node_type => 'mem:generic',
132 },
133 );
134  
135 sub new {
136 my $class = shift;
137 my %settings = (%default_settings, @_);
138  
139 my $self = $class->SUPER::new(%settings);
140  
141 # This crazy thing turns the array into a hash and converts each element
142 # into a Repository::Simple::Type::Node
143 $self->{node_types}
144 = { map
145 {
146 ($_->{name}
147 => Repository::Simple::Type::Node->new(
148 %$_, engine => $self
149 )
150 )
151 } @{ $self->{node_types} } };
152  
153 # This crazy thing turns the array into a hash and converts each element
154 # into a Repository::Simple::Type::Property
155 $self->{property_types}
156 = { map
157 {
158 ($_->{name}
159 => Repository::Simple::Type::Property->new(
160 %$_, engine => $self
161 )
162 )
163 } @{ $self->{property_types} } };
164  
165 return $self;
166 }
167  
168 sub node_type_named {
169 my ($self, $name) = @_;
170 return $self->{node_types}{$name};
171 }
172  
173 sub property_type_named {
174 my ($self, $name) = @_;
175 return $self->{property_types}{$name};
176 }
177  
178 sub lookup {
179 my ($self, $path) = @_;
180 my @names = split '/', $path;
181 shift @names; # Get rid of the first ''
182  
183 my $item = $self->{root};
184 for my $name (@names) {
185 # use Data::Dumper;
186 # print STDERR Dumper($name, $item);
187 last unless defined $item;
188  
189 $item = $item->{nodes}{$name} || $item->{properties}{$name};
190 }
191  
192 return $item;
193 }
194  
195  
196 sub path_exists {
197 my ($self, $path) = @_;
198
199 my $item = $self->lookup($path);
200  
201 return !defined $item ? $NOT_EXISTS
202 : defined $item->{node_type} ? $NODE_EXISTS
203 : defined $item->{property_type} ? $PROPERTY_EXISTS
204 : croak qq(invalid item at path "$path");
205 }
206  
207 sub check_lookup {
208 my ($self, $type, $path) = @_;
209  
210 my $item = $self->lookup($path);
211  
212 croak qq(no $type found at "$path") unless defined $item;
213 croak qq(item at path "$path" is not a $type)
214 unless defined $item->{"${type}_type"};
215  
216 return $item;
217 }
218  
219 sub node_type_of {
220 my ($self, $path) = @_;
221  
222 my $item = $self->check_lookup('node', $path);
223
224 return $self->node_type_named($item->{node_type});
225 }
226  
227 sub property_type_of {
228 my ($self, $path) = @_;
229  
230 my $item = $self->check_lookup('property', $path);
231  
232 return $self->property_type_named($item->{property_type});
233 }
234  
235 sub nodes_in {
236 my ($self, $path) = @_;
237  
238 my $item = $self->check_lookup('node', $path);
239  
240 return keys %{ $item->{nodes} };
241 }
242  
243 sub properties_in {
244 my ($self, $path) = @_;
245  
246 my $item = $self->check_lookup('node', $path);
247  
248 return keys %{ $item->{properties} };
249 }
250  
251 sub get_scalar {
252 my ($self, $path) = @_;
253  
254 my $item = $self->check_lookup('property', $path);
255  
256 return $item->{value};
257 }
258  
259 sub get_handle {
260 my ($self, $path, $mode) = @_;
261  
262 my $item = $self->check_lookup('property', $path);
263  
264 # Make the scalar into a file handle
265 my $fh = IO::Scalar->new(\$item->{value});
266  
267 $mode ||= '<';
268  
269 # If we're in read or overwrite mode, set the position to the start
270 if ($mode eq '>') {
271 $item->{value} = '';
272 }
273  
274 elsif ($mode eq '+>') {
275 die 'read/overwrite mode for file handles is not supported';
276 }
277  
278 return $fh;
279 }
280  
281 sub namespaces {
282 my $self = shift;
283  
284 return $self->{namespaces};
285 }
286  
287 sub has_permission { 1 }
288  
289 sub set_scalar {
290 my ($self, $path, $scalar) = @_;
291  
292 my $item = $self->check_lookup('property', $path);
293  
294 $item->{value} = $scalar;
295 }
296  
297 sub set_handle {
298 my ($self, $path, $handle) = @_;
299  
300 my $item = $self->check_lookup('property', $path);
301  
302 $item->{value} = join '', readline($handle);
303 }
304  
305 sub save_property {
306 my ($self, $path) = @_;
307  
308 my $item = $self->check_lookup('property', $path);
309 }
310  
311 =head1 AUTHOR
312  
313 Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt>
314  
315 =head1 LICENSE AND COPYRIGHT
316  
317 Copyright 2006 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All
318 Rights Reserved.
319  
320 This module is free software; you can redistribute it and/or modify it under
321 the same terms as Perl itself. See L<perlartistic>.
322  
323 This program is distributed in the hope that it will be useful, but WITHOUT
324 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
325 FOR A PARTICULAR PURPOSE.
326  
327 =cut
328  
329 1