Simple/Type/Node.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 101 CC 3 SCAR 13.9
CC
3
Cov
0%
new
12.0
name
2.0
super_types
2.0
CRAP 4.0
1 package Repository::Simple::Type::Node;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.06';
7  
8 use Carp;
9 use Repository::Simple::Util qw( dirname );
10 use Scalar::Util qw( weaken );
11  
12 our @CARP_NOT = qw( Repository::Simple::Util );
13  
14 =head1 NAME
15  
16 Repository::Simple::Type::Node - Types for content repository nodes
17  
18 =head1 SYNOPSIS
19  
20 An example using this subroutine can be found in F<ex/print_types.pl> of the original source distribution.
21  
22 sub print_node_type {
23 my $node = shift;
24
25 my $type = $node->type;
26
27 print $node->name, ' : ', $type->name;
28 print ' [AC]' if $type->auto_created;
29 print ' [UP]' if $type->updatable;
30 print ' [RM]' if $type->removable;
31 print "\n";
32
33 my %property_types = $type->property_types;
34 while (my ($name, $ptype_name) = each %property_types) {
35 my $ptype = $node->repository->property_type($ptype_name);
36
37 printf ' * %-10s : %-16s', $name, $ptype->name;
38
39 print ' [AC]' if $ptype->auto_created;
40 print ' [UP]' if $ptype->updatable;
41 print ' [RM]' if $ptype->removable;
42 print "\n";
43 }
44 }
45  
46 =head1 DESCRIPTION
47  
48 Node types are used to determine information about what kind of information is expected and required to be part of a node instance. A node type may also inherit features from one or more node types.
49  
50 Most developers will not need to create node types, but may use node types to discover information about how a node can be manipulated and what kind of information can be expected from a given node.
51  
52 =head2 METHODS
53  
54 =over
55  
56 =item $type = Repository::Simple::Type::Node-E<gt>new(%args)
57  
58 Create a new node type with the given arguments, C<%args>.
59  
60 The following arguments are used:
61  
62 =over
63  
64 =item engine (required)
65  
66 This is the storage engine to which the node type belongs.
67  
68 =item name (required)
69  
70 This is the short identifying name for the type. This is should be a fully qualified name, e.g., "ns:name".
71  
72 =item super_types
73  
74 This option may be set to an array of node type names representing the node types that this node type inherits from. Only the possible/required child node types and property types are inheritable
75  
76 If this option is not given, then the node type inherits nothing.
77  
78 =item node_types
79  
80 This option is set to a hash where the keys are node names and the values are either node type names or arrays of node type names. The string "*" is special for the keys, it means that a node of any name may be contained with the given type.
81  
82 For example,
83  
84 node_types => {
85 foo => 'my:typeX',
86 bar => [ 'my:typeY', 'my:typeZ' ],
87 '*' => [ 'my:typeX', 'my:typeZ' ],
88 },
89  
90 allows the nodes of the defined node type to have a child node named "foo" with type "my:typeX", a node named "bar" with either the type "my:typeY" or the type "my:typeZ", and any number of other nodes named anything with type "my:typeX" or "my:typeZ".
91  
92 =item property_types
93  
94 This option is set to a hash where the keys are property names and the values are either property type names or arrays of property type names.
95  
96 =item auto_created
97  
98 This option is set to true if this node will be created automatically when its parent is created.
99  
100 By default, this value is false.
101  
102 =item updatable
103  
104 This is a property for all node types stating whether or not the node may be updated, i.e., renamed. This only affects the node itself and does not affect any of its properties or child nodes.
105  
106 By default, this value is false.
107  
108 =item removable
109  
110 When this property is set to a true value, this node may not be removed from its parent node.
111  
112 By default, this value is false.
113  
114 =back
115  
116 =cut
117  
118 sub new {
119 my $class = shift;
120 my %args = @_;
121  
122 if (!defined $args{engine}) {
123 croak 'The "engine" argument must be given.';
124 }
125  
126 weaken $args{engine};
127  
128 if (!defined $args{name}) {
129 croak 'The "name" argument must be given.';
130 }
131  
132 $args{super_types} ||= [];
133  
134 $args{node_types} ||= {};
135 $args{property_types} ||= {};
136  
137 $args{auto_created} ||= 0;
138 $args{updatable} ||= 0;
139 $args{removable} ||= 0;
140  
141 return bless \%args, $class;
142 }
143  
144 =item $name = $type-E<gt>name
145  
146 This method returns the name of the type.
147  
148 =cut
149  
150 sub name {
151 my $self = shift;
152 return $self->{name};
153 }
154  
155 =item @super_types = $type-E<gt>super_types
156  
157 This method returns the direct super types of the type or an empty list if there are no direct super types.
158  
159 =cut
160  
161 sub super_types {
162 my $self = shift;
163 return @{ $self->{super_types} };
164 }
165  
166 =item %node_types = $type-E<gt>node_types
167  
168 Returns all the child nodes of this node type, including all nodes inherited from super_types. The keys of the returned nodes will be the node names. The values will be the names of the node type that node is expected to have.
169  
170 =cut
171  
172 sub node_types {
173 my $self = shift;
174
175 my %node_types;
176 for my $super_type (@{ $self->{super_types} }) {
177 %node_types = (
178 %node_types,
179 $self->{engine}->node_type_named($super_type)->node_types
180 );
181 }
182  
183 %node_types = (%node_types, %{ $self->{node_types} });
184  
185 return %node_types;
186 }
187  
188 =item %property_types = $type-E<gt>property_types
189  
190 This method returns all properties that may be added to this node, including those inherited from super_types. The keys of the returned hash represent the names of those properties and the values represent the property types of those nodes.
191  
192 =cut
193  
194 sub property_types {
195 my $self = shift;
196
197 my %property_types;
198 for my $supertype (@{ $self->{super_types} }) {
199 %property_types = (
200 %property_types,
201 $self->{engine}->node_type_named($supertype)->property_types
202 );
203 }
204  
205 %property_types = (%property_types, %{ $self->{property_types} });
206  
207 return %property_types;
208 }
209  
210 =item $auto_created = $type-E<gt>auto_created
211  
212 This method returns true if nodes of this type should be automatically created with their parent.
213  
214 =cut
215  
216 sub auto_created {
217 my $self = shift;
218 return $self->{auto_created};
219 }
220  
221 =item $updatable = $type-E<gt>updatable
222  
223 This method returns true if nodes of this type may be changed. A nodes mutability or immutability doesn't have any bearing on the mutability of child nodes or child properties.
224  
225 =cut
226  
227 sub updatable {
228 my $self = shift;
229 return $self->{updatable};
230 }
231  
232 =item $removable = $type-E<gt>removable
233  
234 This method returns true if nodes of this type may be removed from their parent.
235  
236 =cut
237  
238 sub removable {
239 my $self = shift;
240 return $self->{removable};
241 }
242  
243 =back
244  
245 =head1 AUTHOR
246  
247 Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt>
248  
249 =head1 LICENSE AND COPYRIGHT
250  
251 Copyright 2005 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All
252 Rights Reserved.
253  
254 This module is free software; you can redistribute it and/or modify it under
255 the same terms as Perl itself. See L<perlartistic>.
256  
257 This program is distributed in the hope that it will be useful, but WITHOUT
258 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
259 FOR A PARTICULAR PURPOSE.
260  
261 =cut
262  
263 1