ArangoDB2

 view release on metacpan or  search on metacpan

lib/ArangoDB2/Base.pm  view on Meta::CPAN

package ArangoDB2::Base;

use strict;
use warnings;

use Carp qw(croak);
use Data::Dumper;
use JSON::XS;
use Scalar::Util qw(blessed weaken);



# new
#
# Arango organizes data hierarchically: Databases > Collections > Documents
#
# This constructor can build ArangoDB2::Database, Collection, Document, Edge,
# etc. objects which all follow the same pattern
sub new
{
    my $class = shift;
    # new instance
    my $self = {};
    bless($self, $class);
    # process args
    while (my $arg = shift) {
        # if arg is a ref it should be another
        # ArangoDB::* object
        if (ref $arg) {
            # prevent circular ref
            weaken $arg;
            # create reference to parent object
            $self->{$arg->_class} = $arg;
        }
        # if arg is a string then it is the "name"
        # of this object
        else {
            $self->name($arg);
        }
    }
    # if we have a name and can get then try it
    $self->get if defined $self->name
        and $self->can('get');

    return $self;
}

# api_path
#
# return /_db/<db name>/_api
sub api_path
{
    my $self = shift;

    my $db_name
        = $self->database
        ? $self->database->name
        : $self->name;

    return '/' . join('/', '_db', $db_name, '_api', @_);
}

# arango
#
# ArangoDB2 instance
sub arango { $_[0]->{arango} }

# collection
#
# parent ArangoDB2::Collection instance
sub collection {
    my($self, $value) = @_;

    if (defined $value) {
        # if value is already an object then set it
        if (ref $value) {
            $self->{collection} = $value;
        }
        # otherwise treat as name and get collection object
        else {
            $self->{collection} = $self->database->collection($value);
        }
    }

    return $self->{collection};
}

# data
#
# ref to hash containing document data
sub data { shift->_get_set('data', @_) }



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