| 1 | package Repository::Simple::Property; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.06'; | ||
| 7 | |||
| 8 | use Readonly; | ||
| 9 | use Repository::Simple::Permission; | ||
| 10 | use Repository::Simple::Util qw( normalize_path ); | ||
| 11 | use Repository::Simple::Value; | ||
| 12 | |||
| 13 | our @CARP_NOT = qw( Repository::Simple::Util ); | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Repository::Simple::Property - Repository properties | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | See L<Repository::Simple::Node>. | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Each instance of this class represents a single property of a node. | ||
| 26 | |||
| 27 | To retrieve a property instance, do not construct the object directly. Rather, use the methods associated with a node to retrieve the properties associated with that node: | ||
| 28 | |||
| 29 | my @properties = $node->properties; | ||
| 30 | for my $property (@properties) { | ||
| 31 | print $property->name, " = ", $property->value->get_scalar; | ||
| 32 | } | ||
| 33 | |||
| 34 | Each property has a parent (node), a name, a value, and a type. The name is non-empty string identifying the property. The value is a valid value according to the property type. The type is an instance of L<Repository::Simple::Type::Property>. If a property value is set to C<undef>, this is the same as deleting the property from the parent node. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | # $property = Repository::Simple::Property->new($node, $name) | ||
| 39 | # | ||
| 40 | # Create a new property object. | ||
| 41 | sub new { | ||
| 42 | my ($class, $node, $name) = @_; | ||
| 43 | return bless { | ||
| 44 | node => $node, | ||
| 45 | name => $name, | ||
| 46 | }, $class; | ||
| 47 | } | ||
| 48 | |||
| 49 | =over | ||
| 50 | |||
| 51 | =item $node = $self-E<gt>parent | ||
| 52 | |||
| 53 | Get the node to which this property belongs. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub parent { | ||
| 58 | my $self = shift; | ||
| 59 | return $self->{node}; | ||
| 60 | } | ||
| 61 | |||
| 62 | =item $name = $self-E<gt>name | ||
| 63 | |||
| 64 | Get the name of the property. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub name { | ||
| 69 | my $self = shift; | ||
| 70 | return $self->{name}; | ||
| 71 | } | ||
| 72 | |||
| 73 | =item $path = $self-E<gt>path | ||
| 74 | |||
| 75 | Get the full path to the property. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub path { | ||
| 80 | my $self = shift; | ||
| 81 | return $self->{path} if $self->{path}; | ||
| 82 | return $self->{path} = normalize_path($self->{node}->path, $self->{name}); | ||
| 83 | } | ||
| 84 | |||
| 85 | =item $value = $self-E<gt>value | ||
| 86 | |||
| 87 | Retrieve the value stored in the property. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub value { | ||
| 92 | my $self = shift; | ||
| 93 | return Repository::Simple::Value->new( | ||
| 94 | $self->parent->repository, $self->path); | ||
| 95 | } | ||
| 96 | |||
| 97 | =item $type = $self-E<gt>type | ||
| 98 | |||
| 99 | Retrieve the L<Repository::Simple::Type::Property> used to validate and store values for this property. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub type { | ||
| 104 | my $self = shift; | ||
| 105 | return $self->engine->property_type_of($self->path); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub engine { | ||
| 109 | my $self = shift; | ||
| 110 | return $self->{engine} if $self->{engine}; | ||
| 111 | return $self->{engine} = $self->{node}->repository->engine; | ||
| 112 | } | ||
| 113 | |||
| 114 | =item $property-E<gt>save | ||
| 115 | |||
| 116 | Tells the storage engine to save the property. If you've modified the property | ||
| 117 | somehow, the change might already have been made. However, the change is not | ||
| 118 | guaranteed until this method is called. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub save { | ||
| 123 | my $self = shift; | ||
| 124 | my $path = $self->path; | ||
| 125 | $self->parent->repository->check_permission($path, $SET_PROPERTY); | ||
| 126 | return $self->engine->save_property($path); | ||
| 127 | } | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =head1 AUTHOR | ||
| 132 | |||
| 133 | Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt> | ||
| 134 | |||
| 135 | =head1 LICENSE AND COPYRIGHT | ||
| 136 | |||
| 137 | Copyright 2005 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All | ||
| 138 | Rights Reserved. | ||
| 139 | |||
| 140 | This module is free software; you can redistribute it and/or modify it under | ||
| 141 | the same terms as Perl itself. See L<perlartistic>. | ||
| 142 | |||
| 143 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 144 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 145 | FOR A PARTICULAR PURPOSE. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 1 |