package Data::MultiValued::TagContainer;
{
$Data::MultiValued::TagContainer::VERSION = '0.0.1_5';
}
{
$Data::MultiValued::TagContainer::DIST = 'Data-MultiValued';
}
use Moose;
use namespace::autoclean;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw(HashRef);
use Data::MultiValued::Exceptions;
has _storage => (
is => 'rw',
isa => HashRef,
init_arg => undef,
default => sub { { } },
traits => ['Hash'],
handles => {
_has_tag => 'exists',
_get_tag => 'get',
_create_tag => 'set',
_delete_tag => 'delete',
all_tags => 'keys',
},
);
has _default_tag => (
is => 'rw',
init_arg => undef,
predicate => '_has_default_tag',
clearer => '_clear_default_tag',
);
sub get {
my ($self,$args) = @_;
my $tag = $args->{tag};
if (!defined($tag)) {
if ($self->_has_default_tag) {
return $self->_default_tag;
}
Data::MultiValued::Exceptions::TagNotFound->throw({
value => $tag,
});
}
if (!$self->_has_tag($tag)) {
Data::MultiValued::Exceptions::TagNotFound->throw({
value => $tag,
});
}
return $self->_get_tag($tag);
}
sub get_or_create {
my ($self,$args) = @_;
my $tag = $args->{tag};
if (!defined($tag)) {
if ($self->_has_default_tag) {
return $self->_default_tag;
}
else {
return $self->_default_tag(
$self->_create_new_inferior
);
}
}
if (!$self->_has_tag($tag)) {
$self->_create_tag($tag,$self->_create_new_inferior);
}
return $self->_get_tag($tag);
}
sub _clear_storage {
my ($self) = @_;
$self->_storage({});
}
sub clear {
my ($self,$args) = @_;
my $tag = $args->{tag};
if (!defined($tag)) {
$self->_clear_default_tag;
$self->_clear_storage;
}
elsif ($self->_has_tag($tag)) {
$self->_delete_tag($tag);
}
return;
}
sub _create_new_inferior {
my ($self) = @_;
return {};
}
__PACKAGE__->meta->make_immutable();
1;
__END__