Module: Arango::DocumentCollection::ClassMethods

Defined in:
lib/arango/document_collection/class_methods.rb

Overview

Arango DocumentCollection ClassMethods

Instance Method Summary collapse

Instance Method Details

#all(exclude_system: true, database: Arango.current_database) ⇒ Array<Arango::DocumentCollection>

Retrieves all collections from the database.



61
62
63
64
65
# File 'lib/arango/document_collection/class_methods.rb', line 61

def all (exclude_system: true, database: Arango.current_database)
  query = { excludeSystem: exclude_system }
  result = Arango::Requests::Collection::ListAll.execute(server: database.server, params: query)
  result.result.map { |c| from_results({}, c.to_h, database: database) }
end

#any_exists?(name:, exclude_system: true, database: Arango.current_database) ⇒ Boolean

Check if s document collection exists.



102
103
104
105
106
# File 'lib/arango/document_collection/class_methods.rb', line 102

def any_exists? (name:, exclude_system: true, database: Arango.current_database)
  args = { excludeSystem: exclude_system }
  result = Arango::Requests::Collection::ListAll.execute(server: database.server, args: args)
  result.result.map { |c| c[:name] }.include?(name)
end

#delete(name:, database: Arango.current_database) ⇒ Object

Removes a collection.



52
53
54
55
# File 'lib/arango/document_collection/class_methods.rb', line 52

def delete(name:, database: Arango.current_database)
  args = { name: name }
  Arango::Requests::Collection::Delete.execute(server: database.server, args: args)
end

#exists?(name:, exclude_system: true, database: Arango.current_database) ⇒ Boolean

Check if s document collection exists.



112
113
114
115
116
# File 'lib/arango/document_collection/class_methods.rb', line 112

def exists? (name:, exclude_system: true, database: Arango.current_database)
  args = { excludeSystem: exclude_system }
  result = Arango::Requests::Collection::ListAll.execute(server: database.server, args: args)
  result.result.select { |c| TYPES[c[:type]] == :document }.map { |c| c[:name] }.include?(name)
end

#from_h(collection_hash, database: Arango.current_database) ⇒ Arango::DocumentCollection

Takes a hash and instantiates a Arango::DocumentCollection object from it.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arango/document_collection/class_methods.rb', line 25

def from_h(collection_hash, database: Arango.current_database)
  collection_hash = collection_hash.transform_keys! { |k| k.to_s.underscore.to_sym }
  collection_hash.merge!(database: database) unless collection_hash.key?(:database)
  if collection_hash.key?(:properties)
    collection_hash[:id] = collection_hash[:properties].delete(:id) if collection_hash[:properties].key?(:id)
    collection_hash[:name] = collection_hash[:properties].delete(:name) if collection_hash[:properties].key?(:name)
    collection_hash[:status] = collection_hash[:properties].delete(:status) if collection_hash[:properties].key?(:status)
    collection_hash[:type] = collection_hash[:properties].delete(:type) if collection_hash[:properties].key?(:type)
  end
  collection_hash[:type] = TYPES[collection_hash[:type]] if collection_hash[:type].is_a?(Integer)
  Arango::DocumentCollection::Base.new(**collection_hash)
end

#from_results(collection_result, properties_result, database: Arango.current_database) ⇒ Arango::DocumentCollection

Takes a Arango::Result and instantiates a Arango::DocumentCollection object from it.



42
43
44
45
46
# File 'lib/arango/document_collection/class_methods.rb', line 42

def from_results(collection_result, properties_result, database: Arango.current_database)
  hash = collection_result ? {}.merge(collection_result.to_h) : {}
  hash[:properties] = properties_result
  from_h(hash, database: database)
end

#get(name:, database: Arango.current_database) ⇒ Arango::DocumentCollection

Get collection from the database.



71
72
73
74
75
76
# File 'lib/arango/document_collection/class_methods.rb', line 71

def get (name:, database: Arango.current_database)
  args = { name: name }
  result = Arango::Requests::Collection::Get.execute(server: database.server, args: args)
  props = Arango::Requests::Collection::GetProperties.execute(server: database.server, args: args)
  from_results(result, props.raw_result, database: database)
end

#list(exclude_system: true, database: Arango.current_database) ⇒ Array<String>

Retrieves a list of document collections.



92
93
94
95
96
# File 'lib/arango/document_collection/class_methods.rb', line 92

def list (exclude_system: true, database: Arango.current_database)
  args = { excludeSystem: exclude_system }
  result = Arango::Requests::Collection::ListAll.execute(server: database.server, args: args)
  result.result.select { |c| TYPES[c[:type]] == :document }.map { |c| c[:name] }
end

#list_all(exclude_system: true, database: Arango.current_database) ⇒ Array<String>

Retrieves a list of all collections.



82
83
84
85
86
# File 'lib/arango/document_collection/class_methods.rb', line 82

def list_all (exclude_system: true, database: Arango.current_database)
  args = { excludeSystem: exclude_system }
  result = Arango::Requests::Collection::ListAll.execute(server: database.server, args: args)
  result.result.map { |c| c[:name] }
end

#new(database: Arango.current_database, graph: nil, name:, id: nil, globally_unique_id: nil, is_system: false, status: nil, type: :document, properties: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/arango/document_collection/class_methods.rb', line 6

def new(database: Arango.current_database, graph: nil,
        name:, id: nil, globally_unique_id: nil, is_system: false, status: nil, type: :document,
        properties: {})
  case type
  when :document
    super(database: database, graph: graph,
          name: name, id: id, globally_unique_id: globally_unique_id, status: status, type: :document, is_system: is_system,
          properties: properties)
  when :edge
    Arango::EdgeCollection::Base.new(database: database, graph: graph,
                                     name: name, id: nil, globally_unique_id: globally_unique_id, is_system: false, status: status, type: :edge,
                                     properties: properties)
  else raise "unknown type"
  end
end