| 1 | package Repository::Simple::Value; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.06'; | ||
| 7 | |||
| 8 | use Repository::Simple::Permission; | ||
| 9 | use Repository::Simple::Util; | ||
| 10 | |||
| 11 | our @CARP_NOT = qw( Repository::Simple::Util ); | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Repository::Simple::Value - Class for retrieving and setting property values | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This class is used for access a property value. This class is never instantiated directly, but retrieved from a property via the C<value()> method: | ||
| 20 | |||
| 21 | my $value = $property->value; | ||
| 22 | my $scalar = $value->get_scalar; | ||
| 23 | my $handle = $handle->get_handle('<'); | ||
| 24 | |||
| 25 | =head2 METHODS | ||
| 26 | |||
| 27 | =over | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | # $value = Repository::Simple::Value->new($engine, $path) | ||
| 32 | # | ||
| 33 | # Create a value object. | ||
| 34 | # | ||
| 35 | sub new { | ||
| 36 | my ($class, $repository, $path) = @_; | ||
| 37 | |||
| 38 | return bless { | ||
| 39 | repository => $repository, | ||
| 40 | engine => $repository->engine, | ||
| 41 | path => $path, | ||
| 42 | }, $class; | ||
| 43 | } | ||
| 44 | |||
| 45 | =item $scalar = $value-E<gt>get_scalar | ||
| 46 | |||
| 47 | Retrieve the value of the property as a scalar value. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub get_scalar { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | $self->{repository}->check_permission($self->{path}, $READ); | ||
| 55 | |||
| 56 | return $self->{engine}->get_scalar($self->{path}); | ||
| 57 | } | ||
| 58 | |||
| 59 | =item $handle = $value-E<gt>get_handle | ||
| 60 | |||
| 61 | =item $handle = $value-E<gt>get_handle($mode) | ||
| 62 | |||
| 63 | Retrieve the value of the property as an IO handle. The C<$mode> argument is used to specify what kind of handle it is. It should be one of: | ||
| 64 | |||
| 65 | =over | ||
| 66 | |||
| 67 | =item * | ||
| 68 | |||
| 69 | "<" | ||
| 70 | |||
| 71 | =item * | ||
| 72 | |||
| 73 | ">" | ||
| 74 | |||
| 75 | =item * | ||
| 76 | |||
| 77 | ">>" | ||
| 78 | |||
| 79 | =item * | ||
| 80 | |||
| 81 | "+<" | ||
| 82 | |||
| 83 | =item * | ||
| 84 | |||
| 85 | "+>" | ||
| 86 | |||
| 87 | =item * | ||
| 88 | |||
| 89 | "+>>" | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | If the value cannot be returned with a handle in the given mode, the method will croak. If C<$mode> is not given, then "<" is assumed. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub get_handle { | ||
| 98 | my ($self, $mode) = @_; | ||
| 99 | |||
| 100 | $mode ||= '<'; | ||
| 101 | |||
| 102 | $self->{repository}->check_permission($self->{path}, $READ) | ||
| 103 | if $mode =~ /<|\+/; | ||
| 104 | |||
| 105 | $self->{repository}->check_permission($self->{path}, $SET_PROPERTY) | ||
| 106 | if $mode =~ />|\+/; | ||
| 107 | |||
| 108 | return $self->{engine}->get_handle($self->{path}, $mode) | ||
| 109 | } | ||
| 110 | |||
| 111 | =item $value-E<gt>set_handle($handle) | ||
| 112 | |||
| 113 | Given a ready-to-read IO handle, this method will replace the contents of the value with the contents of the entire file handle. The handle should be passed as a reference to a glob. E.g., | ||
| 114 | |||
| 115 | $foo = "blah blah"; | ||
| 116 | $value1->set_handle(\*STDIN); | ||
| 117 | $value2->set_handle(IO::Scalar->new(\$foo)); | ||
| 118 | |||
| 119 | Make sure to call the C<save()> method on the property or a parent node to ensure the change has taken place. The change might take place immediately for some engines, but the change is guaranteed to have happened by the time the C<save()> method returned. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub set_handle { | ||
| 124 | my ($self, $handle) = @_; | ||
| 125 | |||
| 126 | $self->{repository}->check_permission($self->{path}, $SET_PROPERTY); | ||
| 127 | |||
| 128 | $self->{engine}->set_handle($self->{path}, $handle); | ||
| 129 | } | ||
| 130 | |||
| 131 | =item $value-E<gt>set_value($value) | ||
| 132 | |||
| 133 | Replaces the value with the scalar value C<$value>. | ||
| 134 | |||
| 135 | Make sure to call the C<save()> method on the property or a parent node to ensure the change has taken place. The change might take place immediately for some engines, but the change is guaranteed to have happened by the time the C<save()> method returned. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub set_scalar { | ||
| 140 | my ($self, $value) = @_; | ||
| 141 | |||
| 142 | $self->{repository}->check_permission($self->{path}, $SET_PROPERTY); | ||
| 143 | |||
| 144 | $self->{engine}->set_scalar($self->{path}, $value); | ||
| 145 | } | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =head1 AUTHOR | ||
| 150 | |||
| 151 | Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt> | ||
| 152 | |||
| 153 | =head1 LICENSE AND COPYRIGHT | ||
| 154 | |||
| 155 | Copyright 2005 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All | ||
| 156 | Rights Reserved. | ||
| 157 | |||
| 158 | This module is free software; you can redistribute it and/or modify it under | ||
| 159 | the same terms as Perl itself. See L<perlartistic>. | ||
| 160 | |||
| 161 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 162 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 163 | FOR A PARTICULAR PURPOSE. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | 1 |