Module: Pacer::EdgeMixin

Included in:
DexEdge, EdgeWrapper, Neo4jEdge, TinkerEdge
Defined in:
lib/pacer/graph/edge_mixin.rb

Overview

This module is mixed into the raw Blueprints Edge class from any graph implementation.

Adds more convenient/rubyish methods and adds support for extensions to some methods where needed.

Instance Method Summary collapse

Instance Method Details

#add_extensions(exts) ⇒ Pacer::EdgeWrapper

Add extensions to this edge.

If any extension has a Edge module within it, this edge will be extended with the extension's Edge module.



18
19
20
21
22
23
24
# File 'lib/pacer/graph/edge_mixin.rb', line 18

def add_extensions(exts)
  if exts.any?
    EdgeWrapper.wrap(self, exts)
  else
    self
  end
end

#clone_into(target_graph, opts = {}) {|e| ... } ⇒ Pacer::EdgeMixin

Clones this edge into the target graph.

This differs from the #copy_into in that it tries to set the new element_id the same as the original element_id.

Options Hash (opts):

  • :create_vertices (true)

    Create the vertices associated to this edge if they don't already exist.

Yields:

  • (e)

    Optional block yields the edge after it has been created.

Raises:

  • (StandardError)

    If this the associated vertices don't exist and :create_vertices is not set



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pacer/graph/edge_mixin.rb', line 85

def clone_into(target_graph, opts = {})
  e_idx = target_graph.index_name("tmp:e:#{graph.to_s}", :edge, :create => true)
  e = target_graph.edge(element_id) || e_idx.get('id', element_id).first
  unless e
    v_idx = target_graph.index_name("tmp:v:#{graph.to_s}", :vertex, :create => true)
    iv = target_graph.vertex(in_vertex.element_id) || v_idx.get('id', in_vertex.element_id).first
    ov = target_graph.vertex(out_vertex.element_id) || v_idx.get('id', out_vertex.element_id).first
    if opts[:create_vertices]
      iv ||= in_vertex.clone_into target_graph
      ov ||= out_vertex.clone_into target_graph
    end
    raise 'vertices not found' if not iv or not ov
    e = target_graph.create_edge(element_id, iv, ov, label, properties)
    e_idx.put('id', element_id, e)
    yield e if block_given?
  end
  e
end

#copy_into(target_graph) {|e| ... } ⇒ Pacer::EdgeMixin

Copies this edge into the target graph with the next available edge id.

Yields:

  • (e)

    Optional block yields the edge after it has been created.

Raises:

  • (StandardError)

    If this the associated vertices don't exist



112
113
114
115
116
117
118
119
120
121
# File 'lib/pacer/graph/edge_mixin.rb', line 112

def copy_into(target_graph)
  v_idx = target_graph.index_name("tmp:v:#{graph.to_s}", :vertex, :create => true)
  iv = v_idx.get('id', in_vertex.element_id).first || target_graph.vertex(in_vertex.element_id)
  ov = v_idx.get('id', out_vertex.element_id).first || target_graph.vertex(out_vertex.element_id)

  raise 'vertices not found' if not iv or not ov
  e = target_graph.create_edge nil, iv, ov, label, properties
  yield e if block_given?
  e
end

#delete!Object

Deletes the edge from its graph.



68
69
70
# File 'lib/pacer/graph/edge_mixin.rb', line 68

def delete!
  graph.remove_edge element
end

#display_nameString

Returns the display name of the edge.



59
60
61
62
63
64
65
# File 'lib/pacer/graph/edge_mixin.rb', line 59

def display_name
  if graph and graph.edge_name
    graph.edge_name.call self
  else
    "#{ out_vertex.element_id }-#{ get_label }-#{ in_vertex.element_id }"
  end
end

#in_vertex(extensions = nil) ⇒ Pacer::VertexMixin

The incoming vertex for this edge.



28
29
30
31
32
33
34
35
36
# File 'lib/pacer/graph/edge_mixin.rb', line 28

def in_vertex(extensions = nil)
  v = inVertex
  v.graph = graph
  if extensions
    v.add_extensions extensions
  else
    v
  end
end

#inspectString

Returns a human-readable representation of the edge using the standard ruby console representation of an instantiated object.



53
54
55
# File 'lib/pacer/graph/edge_mixin.rb', line 53

def inspect
  "#<E[#{element_id}]:#{display_name}>"
end

#out_vertex(extensions = nil) ⇒ Pacer::VertexMixin

The outgoing vertex for this edge.



40
41
42
43
44
45
46
47
48
# File 'lib/pacer/graph/edge_mixin.rb', line 40

def out_vertex(extensions = nil)
  v = outVertex
  v.graph = graph
  if extensions
    v.add_extensions extensions
  else
    v
  end
end