Module: Arango::DocumentCollection::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

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

Retrieves all collections from the database.

Parameters:

  • exclude_system (Boolean) (defaults to: true)

    Optional, default true, exclude system collections.

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:



59
60
61
62
63
# File 'lib/arango/document_collection/class_methods.rb', line 59

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.

Parameters:

  • name (String)

    Name of the collection

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/arango/document_collection/class_methods.rb', line 100

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.

Parameters:

  • name (String)

    The name of the collection.

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:

  • nil



50
51
52
53
# File 'lib/arango/document_collection/class_methods.rb', line 50

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.

Parameters:

  • name (String)

    Name of the collection

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/arango/document_collection/class_methods.rb', line 110

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.

Parameters:

  • collection_hash (Hash)

Returns:



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

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.

Parameters:

Returns:



40
41
42
43
44
# File 'lib/arango/document_collection/class_methods.rb', line 40

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.

Parameters:

  • name (String)

    The name of the collection.

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:



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

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.

Parameters:

  • exclude_system (Boolean) (defaults to: true)

    Optional, default true, exclude system collections.

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:

  • (Array<String>)

    List of collection names.



90
91
92
93
94
# File 'lib/arango/document_collection/class_methods.rb', line 90

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.

Parameters:

  • exclude_system (Boolean) (defaults to: true)

    Optional, default true, exclude system collections.

  • database (Arango::Database) (defaults to: Arango.current_database)

Returns:

  • (Array<String>)

    List of collection names.



80
81
82
83
84
# File 'lib/arango/document_collection/class_methods.rb', line 80

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



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

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