Class: Ashikawa::Core::EdgeCollection

Inherits:
Collection
  • Object
show all
Defined in:
lib/ashikawa-core/edge_collection.rb

Overview

Note:

This is basically just a regular collection with some additional attributes and methods to ease working with collections in the graph module.

An edge collection as it is returned from a graph

Constant Summary collapse

REMOVE_EDGES_AQL_STATEMENT =

The prepared AQL statement to remove edges

<<-AQL.gsub(/^[ \t]*/, '')
FOR e IN @@edge_collection
  FILTER e._from == @from && e._to == @to
  REMOVE e._key IN @@edge_collection
AQL

Constants inherited from Collection

Collection::CONTENT_CLASS, Collection::CONTENT_TYPES

Instance Attribute Summary collapse

Attributes inherited from Collection

#content_type, #database, #id, #name, #status

Instance Method Summary collapse

Methods inherited from Collection

#[], #add_index, #create_document, #create_edge, #delete, #fetch, #figure, #index, #indices, #key_options, #length, #load, #query, #replace, #truncate, #unload, #volatile?, #wait_for_sync=, #wait_for_sync?

Constructor Details

#initialize(database, raw_collection, graph) ⇒ EdgeCollection

Note:

You should not create instance manually but rather use Graph#add_edge_definition

Create a new EdgeCollection object

Parameters:

  • database (Database)

    The database the connection belongs to

  • raw_collection (Hash)

    The raw collection returned from the server

  • graph (Graph)

    The graph from which this collection was fetched


32
33
34
35
# File 'lib/ashikawa-core/edge_collection.rb', line 32

def initialize(database, raw_collection, graph)
  super(database, raw_collection)
  @graph = graph
end

Instance Attribute Details

#graphGraph (readonly)

The Graph instance this EdgeCollection was originally fetched from

Returns:

  • (Graph)

    The Graph instance the collection was fetched from


23
24
25
# File 'lib/ashikawa-core/edge_collection.rb', line 23

def graph
  @graph
end

Instance Method Details

#add(directions) ⇒ Edge

Create one or more edges between documents with certain attributes

Examples:

Create an edge between two vertices

edges = edge_collection.add(from: vertex_a, to: vertex_b)

Parameters:

  • from (Document)

    The outbound vertex

  • to (Document)

    The inbound vertex

  • attributes (Hash)

    Additional attributes to add to all created edges

Returns:

  • (Edge)

    The created Edge


46
47
48
49
50
# File 'lib/ashikawa-core/edge_collection.rb', line 46

def add(directions)
  from_vertex, to_vertex = directions.values_at(:from, :to)
  response = send_request_for_this_collection('', post: { _from: from_vertex.id, _to: to_vertex.id })
  fetch(response['edge']['_key'])
end

#build_content_class(data, additional_atttributes = {}) ⇒ Edge

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds a new edge object and passes the current graph to it

Parameters:

  • data (Hash)

    The raw data to be used to instatiate the class

  • additional_atttributes (Hash) (defaults to: {})

    Initial attributes to be passed to the Edge

Returns:

  • (Edge)

    The instatiated edge


76
77
78
# File 'lib/ashikawa-core/edge_collection.rb', line 76

def build_content_class(data, additional_atttributes = {})
  Edge.new(@database, data, additional_atttributes.merge(graph: graph))
end

#remove(from_to) ⇒ Object

Note:

This will remove ALL edges between the given vertices. For more fine grained control delete the desired edges through Edge#remove.

Remove edges by example

Parameters:

  • from_to (Hash)

    Specifies the edge by its vertices to be removed

Options Hash (from_to):

  • from (Document)

    The from part of the edge

  • to (Document)

    The to part of the edge


60
61
62
63
64
65
66
67
68
# File 'lib/ashikawa-core/edge_collection.rb', line 60

def remove(from_to)
  bind_vars = {
    :@edge_collection => name,
    :from             => from_to[:from].id,
    :to               => from_to[:to].id
  }

  database.query.execute(REMOVE_EDGES_AQL_STATEMENT, bind_vars: bind_vars)
end