class Arango::Document

Attributes

body[R]

DEFINE ==

cache_name[R]

DEFINE ==

collection[R]

DEFINE ==

database[R]

DEFINE ==

graph[R]

DEFINE ==

server[R]

DEFINE ==

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/Document.rb, line 9
def self.new(*args)
  hash = args[0]
  super unless hash.is_a?(Hash)
  collection = hash[:collection]
  if collection.is_a?(Arango::Collection) &&
    collection.database.server.active_cache && !hash[:name].nil?
    cache_name = "#{collection.database.name}/#{collection.name}/#{hash[:name]}"
    cached = collection.database.server.cache.cache.dig(:document, cache_name)
    if cached.nil?
      hash[:cache_name] = cache_name
      return super
    else
      body = hash[:body] || {}
      [:rev, :from, :to].each{|k| body[:"_#{k}"] ||= hash[k]}
      body[:"_key"] ||= hash[:name]
      cached.assign_attributes(body)
      return cached
    end
  end
  super
end
new(name: nil, collection:, body: {}, rev: nil, from: nil, to: nil, cache_name: nil) click to toggle source
# File lib/Document.rb, line 31
def initialize(name: nil, collection:, body: {}, rev: nil, from: nil,
  to: nil, cache_name: nil)
  assign_collection(collection)
  unless cache_name.nil?
    @cache_name = cache_name
    @server.cache.save(:document, cache_name, self)
  end
  body[:_key]  ||= name
  body[:_rev]  ||= rev
  body[:_to]   ||= to
  body[:_from] ||= from
  body[:_id]   ||= "#{@collection.name}/#{name}" unless name.nil?
  assign_attributes(body)
end

Public Instance Methods

any(collection) click to toggle source
# File lib/Document.rb, line 318
def any(collection)
  edges(collection: collection)
end
assign_attributes(result)
Alias for: body=
body=(result) click to toggle source
# File lib/Document.rb, line 86
def body=(result)
  result.delete_if{|k,v| v.nil?}
  @body ||= {}
  # binding.pry if @body[:_key] == "Second_Key"
  hash = {
    "_key":  @body[:_key],
    "_id":   @body[:_id],
    "_rev":  @body[:_rev],
    "_from": @body[:_from],
    "_to":   @body[:_to]
  }
  @body = hash.merge(result)
  if @body[:_id].nil? && !@body[:_key].nil?
    @body[:_id] = "#{@collection.name}/#{@body[:_key]}"
  end
  set_up_from_or_to("from", result[:_from])
  set_up_from_or_to("to", result[:_to])
  if @server.active_cache && @cache_name.nil? && !@body[:_id].nil?
    @cache_name = "#{@database.name}/#{@body[:_id]}"
    @server.cache.save(:document, @cache_name, self)
  end
end
Also aliased as: assign_attributes
create(body: {}, waitForSync: nil, returnNew: nil, silent: nil) click to toggle source

POST ==

# File lib/Document.rb, line 197
def create(body: {}, waitForSync: nil, returnNew: nil, silent: nil)
  body = @body.merge(body)
  query = {
    "waitForSync": waitForSync,
    "returnNew":   returnNew,
    "silent":      silent
  }
  result = @database.request("POST", "_api/document/#{@collection.name}", body: body,
    query: query)
  return result if @server.async != false || silent
  body2 = result.clone
  if returnNew
    body2.delete(:new)
    body2 = body2.merge(result[:new])
  end
  body = body.merge(body2)
  assign_attributes(body)
  return return_directly?(result) ? result : self
end
destroy(waitForSync: nil, silent: nil, returnOld: nil, if_match: false) click to toggle source

DELETE ===

# File lib/Document.rb, line 277
def destroy(waitForSync: nil, silent: nil, returnOld: nil, if_match: false)
  query = {
    "waitForSync": waitForSync,
    "returnOld":   returnOld,
    "silent":      silent
  }
  headers = {}
  headers[:"If-Match"] = @body[:_rev] if if_match
  result = @database.request("DELETE", "_api/document/#{@body[:_id]}", query: query,
    headers: headers)
  return result if @server.async != false || silent
  body2 = result.clone
  if returnOld
    body2.delete(:old)
    body2 = body2.merge(result[:old])
  else
    body2 = body2.merge(@body)
  end
  return_element(body2)
  return true
end
edges(collection:, direction: nil) click to toggle source

EDGE ===

# File lib/Document.rb, line 301
def edges(collection:, direction: nil)
  satisfy_class?(collection, [Arango::Collection, String])
  collection = collection.is_a?(Arango::Collection) ? collection.name : collection
  query = {
    "vertex":    @body[:_id],
    "direction": direction
  }
  result = @database.request("GET", "_api/edges/#{collection}", query: query)
  return result if return_directly?(result)
  result[:edges].map do |edge|
    collection_name, key = edge[:_id].split("/")
    collection = Arango::Collection.new(name: collection_name,
      database: @database, type: :edge)
    Arango::Document.new(name: key, body: edge, collection: collection)
  end
end
from(string: false) click to toggle source
# File lib/Document.rb, line 147
def from(string: false)
  return @body[:_from] if string
  @from ||= retrieve_instance_from_and_to(@body[:_from])
  return @from
end
from=(att) click to toggle source
# File lib/Document.rb, line 72
def from=(att)
  att = att.id if att.is_a?(Arango::Document)
  assign_attributes({_from: att})
end
head(if_none_match: false, if_match: false) click to toggle source

HEAD ==

# File lib/Document.rb, line 188
def head(if_none_match: false, if_match: false)
  headers = {}
  headers[:"If-None-Match"] = @body[:_rev] if if_none_match
  headers[:"If-Match"]      = @body[:_rev] if if_match
  @database.request("HEAD", "_api/document/#{@body[:_id]}", headers: headers)
end
id() click to toggle source
# File lib/Document.rb, line 55
def id
  return @body[:_id]
end
id=(att) click to toggle source
# File lib/Document.rb, line 68
def id=(att)
  assign_attributes({_id: id})
end
in(collection) click to toggle source
# File lib/Document.rb, line 326
def in(collection)
  edges(collection: collection, direction: "in")
end
key()
Alias for: name
key=(att)
Alias for: name=
name() click to toggle source
# File lib/Document.rb, line 46
def name
  return @body[:_key]
end
Also aliased as: key
name=(att) click to toggle source
# File lib/Document.rb, line 59
def name=(att)
  assign_attributes({_key: att})
end
Also aliased as: key=
out(collection) click to toggle source
# File lib/Document.rb, line 322
def out(collection)
  edges(collection: collection, direction: "out")
end
replace(body: {}, waitForSync: nil, ignoreRevs: nil, returnOld: nil, returnNew: nil, silent: nil, if_match: false) click to toggle source

PUT ==

# File lib/Document.rb, line 219
def replace(body: {}, waitForSync: nil, ignoreRevs: nil, returnOld: nil,
  returnNew: nil, silent: nil, if_match: false)
  query = {
    "waitForSync": waitForSync,
    "returnNew":   returnNew,
    "returnOld":   returnOld,
    "ignoreRevs":  ignoreRevs,
    "silent":      silent
  }
  headers = {}
  headers[:"If-Match"] = @body[:_rev] if if_match
  result = @database.request("PUT", "_api/document/#{@body[:_id]}", body: body,
    query: query, headers: headers)
  return result if @server.async != false || silent
  body2 = result.clone
  if returnNew
    body2.delete(:new)
    body2 = body2.merge(result[:new])
  end
  body = body.merge(body2)
  assign_attributes(body)
  return return_directly?(result) ? result : self
end
retrieve(if_none_match: false, if_match: false) click to toggle source

GET ==

# File lib/Document.rb, line 178
def retrieve(if_none_match: false, if_match: false)
  headers = {}
  headers[:"If-None-Match"] = @body[:_rev] if if_none_match
  headers[:"If-Match"]      = @body[:_rev] if if_match
  result = @database.request("GET",  "_api/document/#{@body[:_id]}", headers: headers)
  return_element(result)
end
rev() click to toggle source
# File lib/Document.rb, line 51
def rev
  return @body[:_rev]
end
rev=(att) click to toggle source
# File lib/Document.rb, line 64
def rev=(att)
  assign_attributes({_rev: att})
end
to(string: false) click to toggle source
# File lib/Document.rb, line 153
def to(string: false)
  return @body[:_to] if string
  @to ||= retrieve_instance_from_and_to(@body[:_to])
  return @to
end
to=(att) click to toggle source
# File lib/Document.rb, line 77
def to=(att)
  att = att.id if att.is_a?(Arango::Document)
  assign_attributes({_to: att})
end
to_h() click to toggle source

TO HASH ===

# File lib/Document.rb, line 112
def to_h
  {
    "name":  @body[:_key],
    "id":    @body[:_id],
    "rev":   @body[:_rev],
    "from":  @body[:_from],
    "to":    @body[:_to],
    "body":  @body,
    "cache_name":  @cache_name,
    "collection": @collection.name,
    "graph": @graph&.name
  }.delete_if{|k,v| v.nil?}
end
traversal(body: {}, sort: nil, direction: nil, minDepth: nil, visitor: nil, itemOrder: nil, strategy: nil, filter: nil, init: nil, maxIterations: nil, maxDepth: nil, uniqueness: nil, order: nil, expander: nil, edgeCollection: nil) click to toggle source

TRAVERSAL ===

# File lib/Document.rb, line 332
def traversal(body: {}, sort: nil, direction: nil, minDepth: nil,
  visitor: nil, itemOrder: nil, strategy: nil,
  filter: nil, init: nil, maxIterations: nil, maxDepth: nil,
  uniqueness: nil, order: nil, expander: nil,
  edgeCollection: nil)
  Arango::Traversal.new(body: body,
    sort: sort, direction: direction, minDepth: minDepth,
    vertex: self, visitor: visitor,itemOrder: itemOrder,
    strategy: strategy, filter: filter, init: init,
    maxIterations: maxIterations, maxDepth: maxDepth,
    uniqueness: uniqueness, order: order,
    expander: expander, edgeCollection: edgeCollection)
end
update(body: {}, waitForSync: nil, ignoreRevs: nil, returnOld: nil, returnNew: nil, keepNull: nil, mergeObjects: nil, silent: nil, if_match: false) click to toggle source
# File lib/Document.rb, line 243
def update(body: {}, waitForSync: nil, ignoreRevs: nil,
  returnOld: nil, returnNew: nil, keepNull: nil,
  mergeObjects: nil, silent: nil, if_match: false)
  query = {
    "waitForSync":  waitForSync,
    "returnNew":    returnNew,
    "returnOld":    returnOld,
    "ignoreRevs":   ignoreRevs,
    "keepNull":     keepNull,
    "mergeObjects": mergeObjects,
    "silent":       silent
  }
  headers = {}
  headers[:"If-Match"] = @body[:_rev] if if_match
  result = @database.request("PATCH", "_api/document/#{@body[:_id]}", body: body,
    query: query, headers: headers, keepNull: keepNull)
  return result if @server.async != false || silent
  body2 = result.clone
  if returnNew
    body2.delete(:new)
    body2 = body2.merge(result[:new])
  end
  body = body.merge(body2)
  if mergeObjects
    @body = @body.merge(body)
  else
    body.each{|key, value| @body[key] = value}
  end
  assign_attributes(@body)
  return return_directly?(result) ? result : self
end

Private Instance Methods

retrieve_instance_from_and_to(var) click to toggle source
# File lib/Document.rb, line 159
def retrieve_instance_from_and_to(var)
  case var
  when NilClass
    return nil
  when String
    collection_name, document_name = var.split("/")
    collection = Arango::Collection.new name: collection_name, database: @database
    if @graph.nil?
      return Arango::Document.new(name: document_name, collection: collection)
    else
      collection.graph = @graph
      return Arango::Vertex.new(name: document_name, collection: collection)
    end
  end
end
set_up_from_or_to(attrs, var) click to toggle source
# File lib/Document.rb, line 126
def set_up_from_or_to(attrs, var)
  case var
  when NilClass
    @body[:"_#{attrs}"] = nil
  when String
    unless var.include?("/")
      raise Arango::Error.new err: :attribute_is_not_valid, data:
        {"attribute": attrs, "wrong_value": var}
    end
    @body[:"_#{attrs}"] = var
  when Arango::Document
    @body[:"_#{attrs}"] = var.id
    @from = var if attrs == "from"
    @to   = var if attrs == "to"
  else
    raise Arango::Error.new err: :attribute_is_not_valid, data:
      {"attribute": attrs, "wrong_value": var}
  end
end