Class: EnumMachineContrib::Vertex

Inherits:
Struct
  • Object
show all
Defined in:
lib/enum_machine_contrib/vertex.rb

Constant Summary collapse

VERTEX_MODES =

rubocop:disable Lint/ConstantDefinitionInBlock

%i[pending dropped combined cycled].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Vertex

Returns a new instance of Vertex.



12
13
14
15
16
17
18
19
20
# File 'lib/enum_machine_contrib/vertex.rb', line 12

def initialize(value)
  self.value = value

  @incoming_edges  = EdgeSet.new
  @outcoming_edges = EdgeSet.new

  @resolved = false
  pending!
end

Instance Attribute Details

#incoming_edgesObject (readonly)

Returns the value of attribute incoming_edges.



8
9
10
# File 'lib/enum_machine_contrib/vertex.rb', line 8

def incoming_edges
  @incoming_edges
end

#levelObject

Returns the value of attribute level.



7
8
9
# File 'lib/enum_machine_contrib/vertex.rb', line 7

def level
  @level
end

#modeObject

Returns the value of attribute mode.



7
8
9
# File 'lib/enum_machine_contrib/vertex.rb', line 7

def mode
  @mode
end

#outcoming_edgesObject (readonly)

Returns the value of attribute outcoming_edges.



8
9
10
# File 'lib/enum_machine_contrib/vertex.rb', line 8

def outcoming_edges
  @outcoming_edges
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



5
6
7
# File 'lib/enum_machine_contrib/vertex.rb', line 5

def value
  @value
end

Class Method Details

.replace!(replacing_vertexes) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/enum_machine_contrib/vertex.rb', line 70

def self.replace!(replacing_vertexes)
  new_vertex = Vertex[replacing_vertexes.flat_map(&:value)]

  replacing_vertexes.each do |replacing_vertex|
    replacing_vertex.incoming_edges.each do |edge|
      next if replacing_vertexes.include?(edge.from)

      edge.from.edge_to(new_vertex)
    end

    replacing_vertex.outcoming_edges.filter_map do |edge|
      next if replacing_vertexes.include?(edge.to)

      new_vertex.edge_to(edge.to)
    end

    replacing_vertex.dropped!
  end

  new_vertex
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/enum_machine_contrib/vertex.rb', line 40

def active?
  !dropped?
end

#dropped!Object



52
53
54
55
56
57
58
59
# File 'lib/enum_machine_contrib/vertex.rb', line 52

def dropped!
  return if dropped?

  self.mode = :dropped

  incoming_edges.each(&:dropped!)
  outcoming_edges.each(&:dropped!)
end

#edge_to(to_vertex) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/enum_machine_contrib/vertex.rb', line 61

def edge_to(to_vertex)
  new_edge = Edge.new(self, to_vertex)

  outcoming_edges.add(new_edge)
  to_vertex.incoming_edges.add(new_edge)

  new_edge
end

#inspectObject



92
93
94
# File 'lib/enum_machine_contrib/vertex.rb', line 92

def inspect
  "<Vertex [#{mode}]#{'[resolved]' if resolved?} value=#{value || 'nil'}>"
end

#resolved!Object



44
45
46
# File 'lib/enum_machine_contrib/vertex.rb', line 44

def resolved!
  @resolved = true
end

#resolved?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/enum_machine_contrib/vertex.rb', line 48

def resolved?
  @resolved == true
end