| 1 | package Repository::Simple::Node; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Readonly; | ||
| 8 | use Repository::Simple::Permission; | ||
| 9 | use Repository::Simple::Property; | ||
| 10 | use Repository::Simple::Util qw( basename dirname normalize_path ); | ||
| 11 | |||
| 12 | our $VERSION = '0.06'; | ||
| 13 | |||
| 14 | our @CARP_NOT = qw( Repository::Simple::Util ); | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Repository::Simple::Node - Repository nodes | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | The following code can be found in F<ex/print_nodes.pl>: | ||
| 23 | |||
| 24 | use Repository::Simple; | ||
| 25 | |||
| 26 | sub print_node; | ||
| 27 | |||
| 28 | my $repository = Repository::Simple->attach( | ||
| 29 | FileSystem => root => $ARGV[0], | ||
| 30 | ); | ||
| 31 | |||
| 32 | my $node = $repository->root_node; | ||
| 33 | print_node($node, 0); | ||
| 34 | |||
| 35 | sub print_node { | ||
| 36 | my ($node, $depth) = @_; | ||
| 37 | |||
| 38 | print "\t" x $depth, " * ", $node->name, "\n"; | ||
| 39 | |||
| 40 | for my $child ($node->nodes) { | ||
| 41 | print_node($child, $depth + 1); | ||
| 42 | } | ||
| 43 | |||
| 44 | for my $p ($node->properties) { | ||
| 45 | print "\t" x $depth, "\t * ", $p->name, " = ", $p->value, "\n"; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | Each instance of this class describes a node in a repository. A node is basically a unit of information described by a path. | ||
| 52 | |||
| 53 | To retrieve an instance of this type, you never construct this object directly. Instead, use one of the node access methods in L<Repository::Simple> and L<Repository::Simple::Node>: | ||
| 54 | |||
| 55 | my $root = $repository->root_node; | ||
| 56 | my @children = $root->nodes; | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | # $node = Repository::Simple::Node->new($repository, $path) | ||
| 61 | # | ||
| 62 | # Create a new node object. | ||
| 63 | # | ||
| 64 | sub new { | ||
| 65 | my ($class, $repository, $path) = @_; | ||
| 66 | |||
| 67 | return bless { | ||
| 68 | repository => $repository, | ||
| 69 | path => $path, | ||
| 70 | }, $class; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 METHODS | ||
| 74 | |||
| 75 | =over | ||
| 76 | |||
| 77 | =item $repository = $node-E<gt>repository | ||
| 78 | |||
| 79 | Returns the L<Repository::Simple> object to which this node belongs. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub repository { | ||
| 84 | my $self = shift; | ||
| 85 | return $self->{repository}; | ||
| 86 | } | ||
| 87 | |||
| 88 | =item $node = $type-E<gt>parent | ||
| 89 | |||
| 90 | Fetch the node that is the parent of this node. This will always return a node, even for the root node. The root node is the parent of itself. | ||
| 91 | |||
| 92 | If you consider time travel, you may wish to stop yourself before you think too hard on the implications and gross yourself out. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub parent { | ||
| 97 | my $self = shift; | ||
| 98 | |||
| 99 | my $parent_path = dirname($self->path); | ||
| 100 | |||
| 101 | $self->repository->check_permission($parent_path, $READ); | ||
| 102 | |||
| 103 | return Repository::Simple::Node->new( | ||
| 104 | $self->repository, | ||
| 105 | $parent_path, | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | =item $name = $node-E<gt>name | ||
| 110 | |||
| 111 | Fetch the name of the node. This will always be the last element of the node's path. That is, if the path of the node is: | ||
| 112 | |||
| 113 | /foo/bar/baz | ||
| 114 | |||
| 115 | then the name of the node is: | ||
| 116 | |||
| 117 | baz | ||
| 118 | |||
| 119 | In this API it has been decided that the root node will be represented by the string "/" to match with the normal Unix practice of naming the root tree object. The root node must have this name and no other node may have this name. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub name { | ||
| 124 | my ($self) = @_; | ||
| 125 | return $self->{name} if $self->{name}; | ||
| 126 | return $self->{name} = basename($self->{path}); | ||
| 127 | } | ||
| 128 | |||
| 129 | =item $path = $node-E<gt>path | ||
| 130 | |||
| 131 | This returns the full path from the root of the tree to this node. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub path { | ||
| 136 | my ($self) = @_; | ||
| 137 | return $self->{path}; | ||
| 138 | } | ||
| 139 | |||
| 140 | =item @nodes = $node-E<gt>nodes | ||
| 141 | |||
| 142 | Returns all the child nodes of this node. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub nodes { | ||
| 147 | my ($self) = @_; | ||
| 148 | |||
| 149 | my $path = $self->path; | ||
| 150 | my $repository = $self->repository; | ||
| 151 | my @node_names = $repository->engine->nodes_in($path); | ||
| 152 | |||
| 153 | my @nodes; | ||
| 154 | for my $node_name (@node_names) { | ||
| 155 | eval { | ||
| 156 | my $node_path = normalize_path($path, $node_name); | ||
| 157 | $repository->check_permission($node_path, $READ); | ||
| 158 | push @nodes, Repository::Simple::Node->new($repository, $node_path); | ||
| 159 | }; | ||
| 160 | |||
| 161 | # Ignore errors, just don't include the unreadable nodes | ||
| 162 | carp $@ if $@; | ||
| 163 | } | ||
| 164 | |||
| 165 | return @nodes; | ||
| 166 | } | ||
| 167 | |||
| 168 | =item @properties = $node-E<gt>properties | ||
| 169 | |||
| 170 | Returns all the proeprties of this node. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub properties { | ||
| 175 | my ($self) = @_; | ||
| 176 | |||
| 177 | my $path = $self->path; | ||
| 178 | my $repository = $self->repository; | ||
| 179 | my @property_names = $repository->engine->properties_in($path); | ||
| 180 | |||
| 181 | my @properties; | ||
| 182 | for my $property_name (@property_names) { | ||
| 183 | eval { | ||
| 184 | my $property_path = normalize_path($path, $property_name); | ||
| 185 | $repository->check_permission($path, $READ); | ||
| 186 | push @properties, | ||
| 187 | Repository::Simple::Property->new($self, $property_name); | ||
| 188 | }; | ||
| 189 | |||
| 190 | # Ignore errors, just don't list those properties | ||
| 191 | carp $@ if $@; | ||
| 192 | } | ||
| 193 | |||
| 194 | return @properties; | ||
| 195 | } | ||
| 196 | |||
| 197 | =item $type = $node-E<gt>type | ||
| 198 | |||
| 199 | Returns the L<Repository::Simple::Type::Node> object describing the node. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | sub type { | ||
| 204 | my ($self) = @_; | ||
| 205 | return $self->repository->engine->node_type_of($self->{path}); | ||
| 206 | } | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt> | ||
| 213 | |||
| 214 | =head1 LICENSE AND COPYRIGHT | ||
| 215 | |||
| 216 | Copyright 2005 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All | ||
| 217 | Rights Reserved. | ||
| 218 | |||
| 219 | This module is free software; you can redistribute it and/or modify it under | ||
| 220 | the same terms as Perl itself. See L<perlartistic>. | ||
| 221 | |||
| 222 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 223 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 224 | FOR A PARTICULAR PURPOSE. | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | 1 |