Class: ArangoVertex

Inherits:
ArangoDocument show all
Defined in:
lib/ArangoRB_Ver.rb

Overview

DOCUMENT ====

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ArangoDocument

#any, #collection, create, #create_document, create_edge, #create_edge, #database, #from, #in, #out, #retrieve_edges, #to

Methods inherited from ArangoServer

address, async, async=, batch, cancelAsync, changePropertyWAL, checkPort, cluster, cluster=, clusterRoundtrip, clusterStatistics, collection, collection=, createDumpBatch, database, database=, databaseVersion, databases, default_server, destroyAllAsync, destroyAsync, destroyCluster, destroyDumpBatch, destroyExpiredAsync, echo, endpoints, execute, executeCluster, executeClusterHead, executeClusterPut, fetchAsync, flushWAL, graph, graph=, log, pendingAsync, prolongDumpBatch, propertyWAL, reload, request, restart, retrieveAsync, retrieveDoneAsync, retrievePendingAsync, return_result, return_result_async, role, server, serverId, shutdown, sleep, statistics, tasks, test, time, transactions, updateCluster, user, user=, username, users, verbose, verbose=, version

Constructor Details

#initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}) ⇒ ArangoVertex

TESTED



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ArangoRB_Ver.rb', line 6

def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database,  body: {}) # TESTED
  if collection.is_a?(String)
    @collection = collection
  elsif collection.is_a?(ArangoCollection)
    @collection = collection.collection
  else
    raise "collection should be a String or an ArangoCollection instance, not a #{collection.class}"
  end

  if graph.is_a?(String)
    @graph = graph
  elsif graph.is_a?(ArangoGraph)
    @graph = graph.graph
  else
    raise "graph should be a String or an ArangoGraph instance, not a #{graph.class}"
  end

  if database.is_a?(String)
    @database = database
  else
    raise "database should be a String, not a #{database.class}"
  end

  if key.is_a?(String) || key.nil?
    @key = key
    unless key.nil?
      body["_key"] = @key
      @id = "#{@collection}/#{@key}"
    end
  elsif key.is_a?(ArangoDocument)
    @key = key.key
    @id = key.id
  else
    raise "key should be a String, not a #{key.class}"
  end

  if body.is_a?(Hash)
    @body = body
  else
    raise "body should be a Hash, not a #{body.class}"
  end
  @idCache = "VER_#{@id}"
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



50
51
52
# File 'lib/ArangoRB_Ver.rb', line 50

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



50
51
52
# File 'lib/ArangoRB_Ver.rb', line 50

def id
  @id
end

#idCacheObject (readonly)

Returns the value of attribute idCache.



50
51
52
# File 'lib/ArangoRB_Ver.rb', line 50

def idCache
  @idCache
end

#keyObject (readonly)

Returns the value of attribute key.



50
51
52
# File 'lib/ArangoRB_Ver.rb', line 50

def key
  @key
end

Instance Method Details

#create(body: @body, waitForSync: nil) ⇒ Object Also known as: create_vertex

POST ====



89
90
91
92
93
94
95
# File 'lib/ArangoRB_Ver.rb', line 89

def create(body: @body, waitForSync: nil) # TESTED
  query = {"waitForSync" => waitForSync}.delete_if{|k,v| v.nil?}
  body["_key"] = @key if body["_key"].nil? && !@key.nil?
  request = @@request.merge({ :body => body.to_json, :query => query })
  result = self.class.post("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@collection}", request)
  return_result result: result, body: body
end

#destroy(waitForSync: nil) ⇒ Object

DELETE ===



133
134
135
136
137
138
# File 'lib/ArangoRB_Ver.rb', line 133

def destroy(waitForSync: nil) # TESTED
  query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
  request = @@request.merge({ :query => query })
  result = self.class.delete("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
  return_result result: result, caseTrue: true
end

#graphObject



66
67
68
# File 'lib/ArangoRB_Ver.rb', line 66

def graph
  ArangoGraph.new(graph: @graph, database: @database)
end

#replace(body: {}, waitForSync: nil) ⇒ Object

MODIFY ===



100
101
102
103
104
105
# File 'lib/ArangoRB_Ver.rb', line 100

def replace(body: {}, waitForSync: nil) # TESTED
  query = { "waitForSync" => waitForSync }.delete_if{|k,v| v.nil?}
  request = @@request.merge({ :body => body.to_json, :query => query })
  result = self.class.put("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
  return_result result: result, body: body
end

#retrieveObject

GET ===



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ArangoRB_Ver.rb', line 72

def retrieve # TESTED
  result = self.class.get("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", @@request)
  return result.headers["x-arango-async-id"] if @@async == "store"
  return true if @@async
  result = result.parsed_response
  if @@verbose
    @body = result["vertex"] unless result["error"]
    result
  else
    return result["errorMessage"] if result["error"]
    @body = result["vertex"]
    self
  end
end

#return_result(result:, body: {}, caseTrue: false) ⇒ Object

UTILITY ===



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ArangoRB_Ver.rb', line 142

def return_result(result:, body: {}, caseTrue: false)
  return result.headers["x-arango-async-id"] if @@async == "store"
  return true if @@async
  result = result.parsed_response
  if @@verbose
    unless result["error"]
      @key = result["vertex"]["_key"]
      @id = "#{@collection}/#{@key}"
      @body = result["vertex"].merge(body)
    end
    result
  else
    return result["errorMessage"] if result["error"]
    return true if caseTrue
    @key = result["vertex"]["_key"]
    @id = "#{@collection}/#{@key}"
    @body = result["vertex"].merge(body)
    self
  end
end

#to_hashObject Also known as: to_h

RETRIEVE ===



54
55
56
57
58
59
60
61
62
63
# File 'lib/ArangoRB_Ver.rb', line 54

def to_hash
  {
    "key" => @key,
    "id" => @id,
    "collection" => @collection,
    "database" => @database,
    "body" => @body,
    "idCache" => @idCache
  }.delete_if{|k,v| v.nil?}
end

#update(body: {}, waitForSync: nil, keepNull: nil) ⇒ Object

TESTED



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ArangoRB_Ver.rb', line 107

def update(body: {}, waitForSync: nil, keepNull: nil) # TESTED
  query = {"waitForSync" => waitForSync, "keepNull" => keepNull}.delete_if{|k,v| v.nil?}
  request = @@request.merge({ :body => body.to_json, :query => query })
  result = self.class.patch("/_db/#{@database}/_api/gharial/#{@graph}/vertex/#{@id}", request)
  return result.headers["x-arango-async-id"] if @@async == "store"
  return true if @@async
  result = result.parsed_response
  if @@verbose
    unless result["error"]
      @key = result["_key"]
      @id = "#{@collection}/#{@key}"
      @body = result["vertex"].body
    end
    result
  else
    return result["errorMessage"] if result["error"]
    @key = result["vertex"]["_key"]
    @id = "#{@collection}/#{@key}"
    @body = @body.merge(body)
    @body = @body.merge(result["vertex"])
    self
  end
end