Class: Traver::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/traver/graph.rb

Instance Method Summary collapse

Constructor Details

#initializeGraph



3
4
5
# File 'lib/traver/graph.rb', line 3

def initialize
  @vertices = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



25
26
27
# File 'lib/traver/graph.rb', line 25

def method_missing(method_name, *args)
  self[method_name]
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/traver/graph.rb', line 12

def [](key)
  key = key.to_s
  
  if plural?(key)
    vertices[key.singularize.to_sym]
  else
    name, index = key.split(/(\d+)$/)
    index = (index || 1).to_i
  
    vertices[name.to_sym][index - 1]
  end
end

#add_vertex(key, object) ⇒ Object



7
8
9
10
# File 'lib/traver/graph.rb', line 7

def add_vertex(key, object)
  vertices[key] ||= []
  vertices[key] << object
end