Class: GraphElements::VertexDefault

Inherits:
Vertex
  • Object
show all
Defined in:
lib/social_framework/graphs/graph_elements.rb

Overview

Represent graph’s vertex

Instance Attribute Summary

Attributes inherited from Vertex

#attributes, #color, #edges, #id, #type, #visits

Instance Method Summary collapse

Constructor Details

#initialize(id, type, attributes = {}) ⇒ VertexDefault

Constructor to vertex

Params:
id

Integer user id

type

Class of vertex

attributes

Hash aditional attributes of vertex



50
51
52
53
54
55
56
57
# File 'lib/social_framework/graphs/graph_elements.rb', line 50

def initialize id, type, attributes = {}
  @id = id
  @type = type
  @edges = Array.new
  @visits = 0
  @color = :white
  @attributes = attributes
end

Instance Method Details

#==(other) ⇒ Object

Overriding equal method to compare vertex by id Returns true if id is equal or false if not



61
62
63
# File 'lib/social_framework/graphs/graph_elements.rb', line 61

def ==(other)
  self.id == other.id and self.type == other.type
end

#add_edge(destiny, label = "") ⇒ Object

Add edges to vertex

Params:
destiny

Vertex destiny to edge

label

String label to edge

Returns edge created



76
77
78
79
80
81
82
83
84
85
# File 'lib/social_framework/graphs/graph_elements.rb', line 76

def add_edge destiny, label = ""
  edge = @edges.select { |e| e.destiny == destiny }.first

  if edge.nil?
    edge = EdgeDefault.new self, destiny
    @edges << edge
  end

  edge.labels << label
end

#hashObject

Overriding hash method to always equals Returns id hash



67
68
69
# File 'lib/social_framework/graphs/graph_elements.rb', line 67

def hash
  self.id.hash
end