Net-Fluidinfo

 view release on metacpan or  search on metacpan

lib/Net/Fluidinfo/Namespace.pm  view on Meta::CPAN

package Net::Fluidinfo::Namespace;
use Moose;
extends 'Net::Fluidinfo::Base';

has description     => (is => 'rw', isa => 'Str');
has parent          => (is => 'ro', isa => 'Maybe[Net::Fluidinfo::Namespace]', lazy_build => 1);
has namespace_names => (is => 'ro', isa => 'ArrayRef[Str]', writer => '_set_namespace_names');
has tag_names       => (is => 'ro', isa => 'ArrayRef[Str]', writer => '_set_tag_names');

with 'Net::Fluidinfo::HasObject', 'Net::Fluidinfo::HasPath';

our %FULL_GET_FLAGS = (
    description     => 1,
    namespace_names => 1,
    tag_names       => 1
);

sub _build_parent {
    # TODO: add croaks for dependencies
    my $self = shift;
    if ($self->path_of_parent ne "") {
        __PACKAGE__->get($self->fin, $self->path_of_parent, %FULL_GET_FLAGS);
    } else {
        undef;
    }
}

# Normal usage is to set description and path of self.
sub create {
    my $self = shift;

    my $payload = $self->json->encode({description => $self->description, name => $self->name});
    $self->fin->post(
        path       => $self->abs_path('namespaces', $self->path_of_parent),
        headers    => $self->fin->headers_for_json,
        payload    => $payload,
        on_success => sub {
            my $response = shift;
            my $h = $self->json->decode($response->content);
            $self->_set_object_id($h->{id});
        }
    );
}

sub get {
    my ($class, $fin, $path, %opts) = @_;

    $opts{returnDescription} = $class->true if delete $opts{description};
    $opts{returnNamespaces}  = $class->true if delete $opts{namespace_names};
    $opts{returnTags}        = $class->true if delete $opts{tag_names};
    
    $fin->get(
        path       => $class->abs_path('namespaces', $path),
        query      => \%opts,
        headers    => $fin->accept_header_for_json,
        on_success => sub {
            my $response = shift;
            my $h = $class->json->decode($response->content);
            my $ns = $class->new(fin => $fin, path => $path);
            $ns->_set_object_id($h->{id});
            $ns->description($h->{description})             if $opts{returnDescription};
            $ns->_set_namespace_names($h->{namespaceNames}) if $opts{returnNamespaces};
            $ns->_set_tag_names($h->{tagNames})             if $opts{returnTags};
            $ns;            
        }
    );
}



( run in 0.623 second using v1.01-cache-2.11-cpan-39bf76dae61 )