Class: Neography::RelationshipTraverser

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/neography/relationship_traverser.rb

Instance Method Summary collapse

Constructor Details

#initialize(node, types, direction) ⇒ RelationshipTraverser

Returns a new instance of RelationshipTraverser


5
6
7
8
9
# File 'lib/neography/relationship_traverser.rb', line 5

def initialize(node, types, direction)
  @node      = node
  @types     = [types]
  @direction = direction
end

Instance Method Details

#bothObject


64
65
66
67
# File 'lib/neography/relationship_traverser.rb', line 64

def both
  @direction = :both
  self
end

#delObject


56
57
58
# File 'lib/neography/relationship_traverser.rb', line 56

def del
  each { |rel| @node.neo_server.delete_relationship(rel) }
end

#eachObject


21
22
23
24
25
26
27
28
29
# File 'lib/neography/relationship_traverser.rb', line 21

def each
  iterator.each do |i| 
    rel = Neography::Relationship.new(i, @node.neo_server)
    rel.start_node = Neography::Node.load(rel.start_node, @node.neo_server)
    rel.end_node = Neography::Node.load(rel.end_node, @node.neo_server)

    yield rel if match_to_other?(rel)
  end
end

#empty?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/neography/relationship_traverser.rb', line 31

def empty?
  first == nil
end

#incomingObject


69
70
71
72
# File 'lib/neography/relationship_traverser.rb', line 69

def incoming
  @direction = :incoming
  self
end

#iteratorObject


35
36
37
# File 'lib/neography/relationship_traverser.rb', line 35

def iterator
  Array(@node.neo_server.get_node_relationships(@node, @direction, @types))
end

#match_to_other?(rel) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
42
43
44
45
46
47
48
49
# File 'lib/neography/relationship_traverser.rb', line 39

def match_to_other?(rel)
  if @to_other.nil?
    true
  elsif @direction == :outgoing
    rel.end_node == @to_other
  elsif @direction == :incoming
    rel.start_node == @to_other
  else
    rel.start_node == @to_other || rel.end_node == @to_other
  end
end

#outgoingObject


74
75
76
77
# File 'lib/neography/relationship_traverser.rb', line 74

def outgoing
  @direction = :outgoing
  self
end

#sizeObject


60
61
62
# File 'lib/neography/relationship_traverser.rb', line 60

def size
  [*self].size
end

#to_other(to_other) ⇒ Object


51
52
53
54
# File 'lib/neography/relationship_traverser.rb', line 51

def to_other(to_other)
  @to_other = to_other
  self
end

#to_sObject


11
12
13
14
15
16
17
18
19
# File 'lib/neography/relationship_traverser.rb', line 11

def to_s
  if @types.size == 1 && !@types.empty?
    "#{self.class} [type: #{@type} dir:#{@direction}]"
  elsif !@types.empty?
    "#{self.class} [types: #{@types.join(',')} dir:#{@direction}]"
  else
    "#{self.class} [types: ANY dir:#{@direction}]"
  end
end