lib/rgraph/link.rb in rgraph-0.1.1 vs lib/rgraph/link.rb in rgraph-0.2.0

- old
+ new

@@ -1,24 +1,30 @@ -class Link - attr_accessor :source, :target, :type - +class Link < Hash def initialize(attributes) - @attributes = attributes - @source = @attributes.delete(:source) - @target = @attributes.delete(:target) - @type = @attributes.delete(:type) || 'undirected' + self[:type] = 'undirected' + self[:weight] = 1 - raise Exception.new("source cant be nil") unless @source - raise Exception.new("target cant be nil") unless @target - raise Exception.new("source must be of type Node") unless @source.is_a? Node - raise Exception.new("target must be of type Node") unless @target.is_a? Node + attributes.each_pair do |key, value| + self[key] = value + end - @attributes[:weight] ||= 1 + 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 - @source.neighbours << @target - @target.neighbours << @source unless @type == 'directed' + self[:source].neighbours << self[:target] + self[:target].neighbours << self[:source] unless self[:type] == 'directed' end - def [](attribute) - @attributes[attribute] + def source + self[:source] + end + + def target + self[:target] + end + + def type + self[:type] end end