Module: Arango::EdgeCollection::ClassMethods

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

Overview

Arango EdgeCollection ClassMethods

Instance Method Summary collapse

Instance Method Details

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

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:



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

def all (exclude_system: true, database: Arango.current_database)
  args = { 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

#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



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

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 a edge collection exists.

Parameters:

  • name (String)

    Name of the collection

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

Returns:

  • (Boolean)


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

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

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

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

Parameters:

  • collection_hash (Hash)

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arango/edge_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::EdgeCollection::Base.new(**collection_hash)
end

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

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

Parameters:

Returns:



42
43
44
45
46
# File 'lib/arango/edge_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::EdgeCollection

Get collection from the database.

Parameters:

  • name (String)

    The name of the collection.

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

Returns:



62
63
64
65
66
67
# File 'lib/arango/edge_collection/class_methods.rb', line 62

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 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.



73
74
75
76
77
# File 'lib/arango/edge_collection/class_methods.rb', line 73

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]] == :edge }.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: :edge, properties: {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/arango/edge_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: :edge,
        properties: {})
  case type
  when :document
    Arango::DocumentCollection::Base.new(database: database, graph: graph,
                                         name: name, id: nil, globally_unique_id: globally_unique_id, is_system: false, status: status, type: :document,
                                         properties: properties)
  when :edge
    super(database: database, graph: graph,
          name: name, id: id, globally_unique_id: globally_unique_id, status: status, type: :edge, is_system: is_system,
          properties: properties)
  else raise "unknown type"
  end
end