Class: GraphElements::Vertex

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

Overview

Represent graph’s vertex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructor to vertex

Params:
id

Integer user id

Returns Vertex’s Instance



11
12
13
14
15
16
17
18
# File 'lib/social_framework/graphs/graph_elements.rb', line 11

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def attributes
  @attributes
end

#colorObject

Returns the value of attribute color.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def color
  @color
end

#edgesObject

Returns the value of attribute edges.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def edges
  @edges
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def id
  @id
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def type
  @type
end

#visitsObject

Returns the value of attribute visits.



5
6
7
# File 'lib/social_framework/graphs/graph_elements.rb', line 5

def visits
  @visits
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

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



22
23
24
# File 'lib/social_framework/graphs/graph_elements.rb', line 22

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/social_framework/graphs/graph_elements.rb', line 39

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

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

  edge.labels << label
end

#hashObject

Overriding hash method to always equals Returns id hash



30
31
32
# File 'lib/social_framework/graphs/graph_elements.rb', line 30

def hash
  self.id.hash
end