Repository::Simple::Value - Class for retrieving and setting property values


Repository-Simple documentation Contained in the Repository-Simple distribution.

Index


Code Index:

NAME

Top

Repository::Simple::Value - Class for retrieving and setting property values

DESCRIPTION

Top

This class is used for access a property value. This class is never instantiated directly, but retrieved from a property via the value() method:

  my $value = $property->value;
  my $scalar = $value->get_scalar;
  my $handle = $handle->get_handle('<');

METHODS

$scalar = $value->get_scalar

Retrieve the value of the property as a scalar value.

$handle = $value->get_handle
$handle = $value->get_handle($mode)

Retrieve the value of the property as an IO handle. The $mode argument is used to specify what kind of handle it is. It should be one of:

  • "<"
  • ">"
  • ">>"
  • "+<"
  • "+>"
  • "+>>"

If the value cannot be returned with a handle in the given mode, the method will croak. If $mode is not given, then "<" is assumed.

$value->set_handle($handle)

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.,

  $foo = "blah blah";
  $value1->set_handle(\*STDIN);
  $value2->set_handle(IO::Scalar->new(\$foo));

Make sure to call the 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 save() method returned.

$value->set_value($value)

Replaces the value with the scalar value $value.

Make sure to call the 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 save() method returned.

AUTHOR

Top

Andrew Sterling Hanenkamp, <hanenkamp@cpan.org>

LICENSE AND COPYRIGHT

Top


Repository-Simple documentation Contained in the Repository-Simple distribution.
package Repository::Simple::Value;

use strict;
use warnings;

our $VERSION = '0.06';

use Repository::Simple::Permission;
use Repository::Simple::Util;

our @CARP_NOT = qw( Repository::Simple::Util );

# $value = Repository::Simple::Value->new($engine, $path)
#
# Create a value object.
#
sub new {
    my ($class, $repository, $path) = @_;

    return bless { 
        repository => $repository,
        engine     => $repository->engine,
        path       => $path,
    }, $class;
}

sub get_scalar {
    my $self = shift;

    $self->{repository}->check_permission($self->{path}, $READ);

    return $self->{engine}->get_scalar($self->{path});
}

sub get_handle {
    my ($self, $mode) = @_;

    $mode ||= '<';

    $self->{repository}->check_permission($self->{path}, $READ)
        if $mode =~ /<|\+/;

    $self->{repository}->check_permission($self->{path}, $SET_PROPERTY)
        if $mode =~ />|\+/;

    return $self->{engine}->get_handle($self->{path}, $mode)
}

sub set_handle {
    my ($self, $handle) = @_;

    $self->{repository}->check_permission($self->{path}, $SET_PROPERTY);

    $self->{engine}->set_handle($self->{path}, $handle);
}

sub set_scalar {
    my ($self, $value) = @_;

    $self->{repository}->check_permission($self->{path}, $SET_PROPERTY);

    $self->{engine}->set_scalar($self->{path}, $value);
}

1