Class: Arango::Document

Inherits:
Object
  • Object
show all
Includes:
Collection_Return, Helper_Error, Helper_Return
Defined in:
lib/Document.rb

Direct Known Subclasses

Edge, Vertex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Collection_Return

#collection=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(name: nil, collection:, body: {}, rev: nil, from: nil, to: nil, cache_name: nil) ⇒ Document

Returns a new instance of Document.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#bodyObject

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def body
  @body
end

#cache_nameObject (readonly)

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def cache_name
  @cache_name
end

#collectionObject (readonly)

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def collection
  @collection
end

#databaseObject (readonly)

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def database
  @database
end

#graphObject (readonly)

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def graph
  @graph
end

#serverObject (readonly)

DEFINE ==



84
85
86
# File 'lib/Document.rb', line 84

def server
  @server
end

Class Method Details

.new(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#any(collection) ⇒ Object



318
319
320
# File 'lib/Document.rb', line 318

def any(collection)
  edges(collection: collection)
end

#create(body: {}, waitForSync: nil, returnNew: nil, silent: nil) ⇒ Object

POST ==



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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) ⇒ Object

DELETE ===



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# 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) ⇒ Object

EDGE ===



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# 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) ⇒ Object



147
148
149
150
151
# 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) ⇒ Object



72
73
74
75
# 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) ⇒ Object

HEAD ==



188
189
190
191
192
193
# 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

#idObject



55
56
57
# File 'lib/Document.rb', line 55

def id
  return @body[:_id]
end

#id=(att) ⇒ Object



68
69
70
# File 'lib/Document.rb', line 68

def id=(att)
  assign_attributes({_id: id})
end

#in(collection) ⇒ Object



326
327
328
# File 'lib/Document.rb', line 326

def in(collection)
  edges(collection: collection, direction: "in")
end

#nameObject Also known as: key



46
47
48
# File 'lib/Document.rb', line 46

def name
  return @body[:_key]
end

#name=(att) ⇒ Object Also known as: key=



59
60
61
# File 'lib/Document.rb', line 59

def name=(att)
  assign_attributes({_key: att})
end

#out(collection) ⇒ Object



322
323
324
# 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) ⇒ Object

PUT ==



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# 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) ⇒ Object

GET ==



178
179
180
181
182
183
184
# 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

#revObject



51
52
53
# File 'lib/Document.rb', line 51

def rev
  return @body[:_rev]
end

#rev=(att) ⇒ Object



64
65
66
# File 'lib/Document.rb', line 64

def rev=(att)
  assign_attributes({_rev: att})
end

#to(string: false) ⇒ Object



153
154
155
156
157
# 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) ⇒ Object



77
78
79
80
# File 'lib/Document.rb', line 77

def to=(att)
  att = att.id if att.is_a?(Arango::Document)
  assign_attributes({_to: att})
end

#to_hObject

TO HASH ===



112
113
114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object

TRAVERSAL ===



332
333
334
335
336
337
338
339
340
341
342
343
344
# 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) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 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