Class: RedGrape::Edge

Inherits:
Element show all
Includes:
Pipe::InV, Pipe::OutV
Defined in:
lib/red_grape/edge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pipe::InV

#in_v

Methods included from Pipe::OutV

#out_v

Methods inherited from Element

#<=>, #==, #[], #[]=, #directed_value, #method_missing, #pass_through, #property, #property_keys, #remove_property, #set_property

Constructor Details

#initialize(graph, id, source, target, label, opts = {}) ⇒ Edge

Returns a new instance of Edge.



10
11
12
13
14
15
16
17
18
# File 'lib/red_grape/edge.rb', line 10

def initialize(graph, id, source, target, label, opts={})
  super graph, opts
  @id = id
  @source = source.is_a?(Vertex) ? source : graph.vertex(source)
  @target = target.is_a?(Vertex) ? target : graph.vertex(target)
  @source.add_out_edge self
  @target.add_in_edge self
  @label = label
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RedGrape::Element

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/red_grape/edge.rb', line 8

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



8
9
10
# File 'lib/red_grape/edge.rb', line 8

def label
  @label
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/red_grape/edge.rb', line 8

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/red_grape/edge.rb', line 8

def target
  @target
end

Instance Method Details

#connected?(v) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/red_grape/edge.rb', line 24

def connected?(v)
  id = v.is_a?(Vertex) ? v.id : v
  @source.id == id.to_s || @target.id == id.to_s
end

#to_hObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/red_grape/edge.rb', line 33

def to_h
  {
    version: RedGrape::VERSION,
    results: {
      _type: '_edge',
      _id: @id,
      _label: @label,
      _source: @source.id,
      _target: @target.id
    }.merge(@property)
  }
end

#to_sObject



29
30
31
# File 'lib/red_grape/edge.rb', line 29

def to_s
  "e[#{@id}][#{@source.id}-#{@label}->#{@target.id}]"
end

#vertex(direction) ⇒ Object



20
21
22
# File 'lib/red_grape/edge.rb', line 20

def vertex(direction)
  directed_value direction, @target, @source
end