Class: Neography::NodeTraverser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, types = nil, dir = "all") ⇒ NodeTraverser

Returns a new instance of NodeTraverser.



7
8
9
10
11
12
13
14
15
# File 'lib/neography/node_traverser.rb', line 7

def initialize(from, types = nil, dir = "all" )
  @from  = from
  @order = "depth first"
  @uniqueness = "none"
  @relationships = Array.new
  types.each do |type|
    @relationships << {"type" => type.to_s, "direction" => dir.to_s }
  end unless types.nil?
end

Instance Attribute Details

#depth(d) ⇒ Object

Returns the value of attribute depth.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def depth
  @depth
end

#filter(body) ⇒ Object

Returns the value of attribute filter.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def filter
  @filter
end

#order(o) ⇒ Object

Returns the value of attribute order.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def order
  @order
end

#prune(body) ⇒ Object

Returns the value of attribute prune.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def prune
  @prune
end

#relationshipsObject

Returns the value of attribute relationships.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def relationships
  @relationships
end

#uniqueness(u) ⇒ Object

Returns the value of attribute uniqueness.



5
6
7
# File 'lib/neography/node_traverser.rb', line 5

def uniqueness
  @uniqueness
end

Instance Method Details

#<<(other_node) ⇒ Object



17
18
19
20
# File 'lib/neography/node_traverser.rb', line 17

def <<(other_node)
  create(other_node)
  self
end

#[](index) ⇒ Object



97
98
99
# File 'lib/neography/node_traverser.rb', line 97

def [](index)
  each_with_index {|node,i| break node if index == i}
end

#both(type) ⇒ Object



36
37
38
39
# File 'lib/neography/node_traverser.rb', line 36

def both(type)
  @relationships << {"type" => type.to_s, "direction" => "all"}
  self
end

#create(other_node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/neography/node_traverser.rb', line 22

def create(other_node)
  case @relationships.first["direction"]
    when "outgoing", "out"
      rel = Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], @from, other_node))
    when "incoming", "in"
      rel = Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], other_node, @from))
    else
      rel = Array.new
      rel << Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], @from, other_node))
      rel << Neography::Relationship.new(@from.neo_server.create_relationship(@relationships.first["type"], other_node, @from))
  end
  rel       
end

#eachObject



105
106
107
108
109
110
111
# File 'lib/neography/node_traverser.rb', line 105

def each
  iterator.each do |i| 
    node = @from.class.new(i)
    node.neo_server = @from.neo_server
    yield node
  end
end

#empty?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/neography/node_traverser.rb', line 101

def empty?
  first == nil
end

#include_start_nodeObject



83
84
85
86
87
88
89
# File 'lib/neography/node_traverser.rb', line 83

def include_start_node
  @filter = {
    "language" => "builtin",
    "name"     => "all"
  }
  self
end

#incoming(type) ⇒ Object



46
47
48
49
# File 'lib/neography/node_traverser.rb', line 46

def incoming(type)
  @relationships << {"type" => type.to_s, "direction" => "in"}
  self
end

#iteratorObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/neography/node_traverser.rb', line 113

def iterator
  options = {
    "order"         => @order,
    "uniqueness"    => @uniqueness,
    "relationships" => @relationships
  }
  options["prune evaluator"] = @prune  unless @prune.nil?
  options["return filter"]   = @filter unless @filter.nil?
  options["depth"]           = @depth  unless @depth.nil?

  if @relationships[0]["type"].empty?
    rels = @from.neo_server.get_node_relationships(@from, @relationships[0]["direction"]) || []
    case @relationships[0]["direction"]
      when "in"
        rels.collect { |r| @from.neo_server.get_node(r["start"]) } #.uniq
      when "out"
        rels.collect { |r| @from.neo_server.get_node(r["end"]) } #.uniq
      else
        rels.collect { |r|
        if @from.neo_id == r["start"].split('/').last
          @from.neo_server.get_node(r["end"])
        else
          @from.neo_server.get_node(r["start"])
        end
        } #.uniq
    end
  else
    @from.neo_server.traverse(@from, "nodes", options)
  end
end

#outgoing(type) ⇒ Object



41
42
43
44
# File 'lib/neography/node_traverser.rb', line 41

def outgoing(type)
  @relationships << {"type" => type.to_s, "direction" => "out"}
  self
end

#sizeObject Also known as: length



91
92
93
# File 'lib/neography/node_traverser.rb', line 91

def size
  [*self].size
end