Class: EnumMachineContrib::Edge

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

Constant Summary collapse

EDGE_MODES =

rubocop:disable Lint/ConstantDefinitionInBlock

%i[pending dropped].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Edge

Returns a new instance of Edge.



11
12
13
14
15
16
17
# File 'lib/enum_machine_contrib/edge.rb', line 11

def initialize(from, to)
  self.from = from
  self.to   = to

  @resolved = false
  pending!
end

Instance Attribute Details

#fromObject

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



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

def from
  @from
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#toObject

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



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

def to
  @to
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/enum_machine_contrib/edge.rb', line 37

def active?
  !dropped?
end

#dropped!Object



41
42
43
44
45
46
# File 'lib/enum_machine_contrib/edge.rb', line 41

def dropped!
  self.mode = :dropped

  to.incoming_edges.delete(self)
  from.outcoming_edges.delete(self)
end

#inspectObject



64
65
66
# File 'lib/enum_machine_contrib/edge.rb', line 64

def inspect
  "<Edge [#{mode}]#{'[resolved]' if resolved?} #{from.inspect} -> #{to.inspect}>"
end

#resolved!Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/enum_machine_contrib/edge.rb', line 52

def resolved!
  @resolved = true

  to.incoming_edges.each do |edge|
    edge.dropped! unless edge == self
  end

  to.outcoming_edges.detect { |edge| edge.to == from }&.dropped!

  to.resolved!
end

#resolved?Boolean

Returns:

  • (Boolean)


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

def resolved?
  @resolved == true
end