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 collapse

Attributes inherited from Vertex

#attributes, #edges, #id, #type

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



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

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

Instance Attribute Details

#colorObject

Returns the value of attribute color.



45
46
47
# File 'lib/social_framework/graphs/graph_elements.rb', line 45

def color
  @color
end

#visitsObject

Returns the value of attribute visits.



45
46
47
# File 'lib/social_framework/graphs/graph_elements.rb', line 45

def visits
  @visits
end

Instance Method Details

#==(other) ⇒ Object

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



63
64
65
# File 'lib/social_framework/graphs/graph_elements.rb', line 63

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



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

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



69
70
71
# File 'lib/social_framework/graphs/graph_elements.rb', line 69

def hash
  self.id.hash
end