Class: Link

Inherits:
Hash
  • Object
show all
Defined in:
lib/rgraph/link.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Link

Returns a new instance of Link.

Raises:

  • (Exception)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rgraph/link.rb', line 2

def initialize(attributes)
  self[:type] = 'undirected'
  self[:weight] = 1

  attributes.each_pair do |key, value|
    self[key] = value
  end

  raise Exception.new("source cant be nil") unless self[:source]
  raise Exception.new("target cant be nil") unless self[:target]
  raise Exception.new("source must be of type Node") unless self[:source].is_a? Node
  raise Exception.new("target must be of type Node") unless self[:target].is_a? Node

  self[:source].neighbours << self[:target]
  self[:target].neighbours << self[:source] unless self[:type] == 'directed'
end

Instance Method Details

#sourceObject



19
20
21
# File 'lib/rgraph/link.rb', line 19

def source
  self[:source]
end

#targetObject



23
24
25
# File 'lib/rgraph/link.rb', line 23

def target
  self[:target]
end

#typeObject



27
28
29
# File 'lib/rgraph/link.rb', line 27

def type
  self[:type]
end