Module: Redgraph::Graph::EdgeMethods

Included in:
Redgraph::Graph
Defined in:
lib/redgraph/graph/edge_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_edge(edge) ⇒ Object

Adds an edge. If successul it returns the created object, otherwise false



8
9
10
# File 'lib/redgraph/graph/edge_methods.rb', line 8

def add_edge(edge)
  merge_or_add_edge(edge, :create)
end

#count_edges(type: nil, properties: nil) ⇒ Object

Counts edges. Options:

  • type: filter by type

  • properties: filter by properties



68
69
70
71
72
73
# File 'lib/redgraph/graph/edge_methods.rb', line 68

def count_edges(type: nil, properties: nil)
  edge = Edge.new(type: type, properties: properties)

  cmd = "MATCH #{edge.to_query_string} RETURN COUNT(edge)"
  query(cmd).flatten[0]
end

#edges(type: nil, src: nil, dest: nil, properties: nil, order: nil, limit: nil, skip: nil) ⇒ Object

Finds edges. Options:

  • type

  • src

  • dest

  • properties

  • order

  • limit

  • skip



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
# File 'lib/redgraph/graph/edge_methods.rb', line 29

def edges(type: nil, src: nil, dest: nil, properties: nil, order: nil, limit: nil, skip: nil)
  _order = if order
    raise MissingAliasPrefixError unless order.include?("edge.")
    "ORDER BY #{order}"
  end
  _limit = "LIMIT #{limit}" if limit
  _skip = "SKIP #{skip}" if skip

  _where = if src || dest
    clauses = [
      ("ID(src) = #{src.id}" if src),
      ("ID(dest) = #{dest.id}" if dest)
    ].compact.join(" AND ")
    "WHERE #{clauses}"
  end

  edge = Edge.new(type: type, src: src, dest: dest, properties: properties)

  cmd = "MATCH #{edge.to_query_string} #{_where}
         RETURN src, edge, dest #{_order} #{_skip} #{_limit}"
  result = _query(cmd)

  result.resultset.map do |item|
    src = node_from_resultset_item(item["src"])
    dest = node_from_resultset_item(item["dest"])
    edge = edge_from_resultset_item(item["edge"])

    edge.src = src
    edge.dest = dest

    edge
  end
end

#merge_edge(edge) ⇒ Object

Merges (creates an edge unless one with the same type and properties exists) an edge. If successul it returns the object, otherwise false



15
16
17
# File 'lib/redgraph/graph/edge_methods.rb', line 15

def merge_edge(edge)
  merge_or_add_edge(edge, :merge)
end