Module: Arango::Graph::ClassMethods

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

Overview

Arango Graph ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

extend Arango::Graph with class methods



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
68
69
70
71
72
73
# File 'lib/arango/graph/class_methods.rb', line 27

def self.extended(base)

  # Retrieves all graphs from the database.
  # @param database [Arango::Database]
  # @return [Array<Arango::Graph::Base>]
  def all (database: Arango.current_database)
    result = Arango::Requests::Graph::ListAll.execute(server: database.server)
    result.graphs.map { |c| from_results({}, c.to_h, database: database) }
  end

  # Get graph from the database.
  # @param name [String] The name of the graph.
  # @param database [Arango::Database]
  # @return [Arango::Database]
  def get (name:, database: Arango.current_database)
    args = { graph: name }
    result = Arango::Requests::Graph::Get.execute(server: database.server, args: args)
    from_results({}, result.graph, database: database)
  end

  # Retrieves a list of all graphs.
  # @param database [Arango::Database]
  # @return [Array<String>] List of graph names.
  def list (database: Arango.current_database)
    result = Arango::Requests::Graph::ListAll.execute(server: database.server)
    result.graphs.map { |c| c[:name] }
  end

  # Removes a graph.
  # @param name [String] The name of the graph.
  # @param database [Arango::Database]
  # @return nil
  def delete(name:, database: Arango.current_database)
    args = { graph: name }
    result = Arango::Requests::Graph::Delete.execute(server: database.server, args: args)
  end

  # Check if graph exists.
  # @param name [String] Name of the graph
  # @param database [Arango::Database]
  # @return [Boolean]
  def exists? (name:, database: Arango.current_database)
    args = { name: name }
    result = Arango::Requests::Graph::Get.execute(server: database.server, args: args)
    result.graphs.map { |c| c[:name] }.include?(name)
  end
end

Instance Method Details

#all(database: Arango.current_database) ⇒ Array<Arango::Graph::Base>

Retrieves all graphs from the database.

Parameters:

Returns:



32
33
34
35
# File 'lib/arango/graph/class_methods.rb', line 32

def all (database: Arango.current_database)
  result = Arango::Requests::Graph::ListAll.execute(server: database.server)
  result.graphs.map { |c| from_results({}, c.to_h, database: database) }
end

#create(is_smart: @is_smart, smart_graph_attribute: @smart_graph_attribute, number_of_shards: @number_of_shards) ⇒ Object

Create Graph in database



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/arango/graph/class_methods.rb', line 76

def create(is_smart: @is_smart, smart_graph_attribute: @smart_graph_attribute,
           number_of_shards: @number_of_shards)
  body = {
    name: @name,
    edgeDefinitions:   edge_definitions_raw,
    isSmart: is_smart,
    options: {
      smartGraphAttribute: smart_graph_attribute,
      numberOfShards: number_of_shards
    }
  }
  body[:options].delete_if{|k,v| v.nil?}
  body.delete(:options) if body[:options].empty?
  result = Arango::Requests::Graph::Create.execute(server: @database.server, body: body, key: :graph)
  return_element(result)
end

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

Removes a graph.

Parameters:

  • name (String)

    The name of the graph.

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

Returns:

  • nil



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

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

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

Check if graph exists.

Parameters:

  • name (String)

    Name of the graph

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

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/arango/graph/class_methods.rb', line 68

def exists? (name:, database: Arango.current_database)
  args = { name: name }
  result = Arango::Requests::Graph::Get.execute(server: database.server, args: args)
  result.graphs.map { |c| c[:name] }.include?(name)
end

#from_h(graph_hash, database: Arango.current_database) ⇒ Object

Create Arango::Graph from hash



7
8
9
10
11
12
13
14
# File 'lib/arango/graph/class_methods.rb', line 7

def from_h(graph_hash, database: Arango.current_database)
  graph_hash = graph_hash.transform_keys { |k| k.to_s.underscore.to_sym }
  graph_hash.merge!(database: database) unless graph_hash.has_key?(:database)
  if graph_hash.has_key?(:properties)
    graph_hash[:name] = graph_hash[:properties].delete(:name) if graph_hash[:properties].has_key?(:name)
  end
  Arango::Graph::Base.new(**graph_hash)
end

#from_results(graph_result, properties_result, database: Arango.current_database) ⇒ Arango::Graph::Base

Takes a Arango::Result and instantiates a Arango::Graph::Base object from it.

Parameters:

Returns:



20
21
22
23
24
# File 'lib/arango/graph/class_methods.rb', line 20

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

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

Get graph from the database.

Parameters:

  • name (String)

    The name of the graph.

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

Returns:



41
42
43
44
45
# File 'lib/arango/graph/class_methods.rb', line 41

def get (name:, database: Arango.current_database)
  args = { graph: name }
  result = Arango::Requests::Graph::Get.execute(server: database.server, args: args)
  from_results({}, result.graph, database: database)
end

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

Retrieves a list of all graphs.

Parameters:

Returns:

  • (Array<String>)

    List of graph names.



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

def list (database: Arango.current_database)
  result = Arango::Requests::Graph::ListAll.execute(server: database.server)
  result.graphs.map { |c| c[:name] }
end