Class: ArangoEdge

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

Overview

GRAPH EDGE ===

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ArangoDocument

#any, #collection, create, 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: {}, from: nil, to: nil) ⇒ ArangoEdge

TESTED



4
5
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ArangoRB_Edg.rb', line 4

def initialize(key: nil, collection: @@collection, graph: @@graph, database: @@database, body: {}, from: nil, to: nil) # 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
  elsif database.is_a?(ArangoDatabase)
    @database = database.database
  else
    raise "database should be a String or an ArangoDatabase instance, 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

  if from.is_a?(String)
    @body["_from"] = from
  elsif from.is_a?(ArangoDocument)
    @body["_from"] = from.id
  elsif from.nil?
  else
    raise "from should be a String or an ArangoDocument instance, not a #{from.class}"
  end

  if to.is_a?(String)
    @body["_to"] = to
  elsif to.is_a?(ArangoDocument)
    @body["_to"] = to.id
  elsif to.nil?
  else
    raise "to should be a String or an ArangoDocument instance, not a #{to.class}"
  end

  @idCache = "EDG_#{@id}"
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



69
70
71
# File 'lib/ArangoRB_Edg.rb', line 69

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



69
70
71
# File 'lib/ArangoRB_Edg.rb', line 69

def id
  @id
end

#idCacheObject (readonly)

Returns the value of attribute idCache.



69
70
71
# File 'lib/ArangoRB_Edg.rb', line 69

def idCache
  @idCache
end

#keyObject (readonly)

Returns the value of attribute key.



69
70
71
# File 'lib/ArangoRB_Edg.rb', line 69

def key
  @key
end

Instance Method Details

#create(body: {}, from: , to: , waitForSync: nil) ⇒ Object Also known as: create_document, create_vertex

POST ====



108
109
110
111
112
113
114
115
116
# File 'lib/ArangoRB_Edg.rb', line 108

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

#destroy(body: nil, waitForSync: nil) ⇒ Object

DELETE ===



155
156
157
158
159
160
# File 'lib/ArangoRB_Edg.rb', line 155

def destroy(body: nil, 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}/edge/#{@id}", request)
  return_result result: result, caseTrue: true
end

#graphObject



85
86
87
# File 'lib/ArangoRB_Edg.rb', line 85

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

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

MODIFY ===



122
123
124
125
126
127
# File 'lib/ArangoRB_Edg.rb', line 122

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}/edge/#{@id}", request)
  return_result result: result, body: body
end

#retrieveObject

GET ===



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ArangoRB_Edg.rb', line 91

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

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

UTILITY ===



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ArangoRB_Edg.rb', line 164

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["edge"]["_key"]
      @id = "#{@collection}/#{@key}"
      @body = body
    end
    result
  else
    return result["errorMessage"] if result["error"]
    return true if caseTrue
    @key = result["edge"]["_key"]
    @id = "#{@collection}/#{@key}"
    @body = body
    self
  end
end

#to_hashObject Also known as: to_h

RETRIEVE ===



73
74
75
76
77
78
79
80
81
82
# File 'lib/ArangoRB_Edg.rb', line 73

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ArangoRB_Edg.rb', line 129

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}/edge/#{@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 = body
    end
    result
  else
    return result["errorMessage"] if result["error"]
    @key = result["edge"]["_key"]
    @id = "#{@collection}/#{@key}"
    @body = @body.merge(body)
    @body = @body.merge(result["edge"])
    self
  end
end