Class: Conjunction::Nexus

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/conjunction/nexus.rb

Overview

The Nexus provides a central source to couple objects together explicitly:

class Conjunction::Nexus
  couple Foo, to: CommonMaterial
  couple Bar, to: CommonMaterial

  couple FooFlow, to: FooState, bidirectional: true
end

Class Method Summary collapse

Class Method Details

.conjugate(conjunctive, junction:) ⇒ Object



19
20
21
# File 'lib/conjunction/nexus.rb', line 19

def conjugate(conjunctive, junction:)
  _couplings[junction.try(:junction_key)][conjunctive] if couples?(junction)
end

.couple(conjunctive, to:, bidirectional: false) ⇒ Object (private)

Raises:

  • (TypeError)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/conjunction/nexus.rb', line 29

def couple(conjunctive, to:, bidirectional: false)
  raise TypeError, "#{conjunctive} is not a valid conjunctive" unless conjunctive.respond_to?(:conjugate)
  raise TypeError, "#{to} is not a valid junction" unless to.respond_to?(:junction_key)

  if bidirectional
    raise TypeError, "#{conjunctive} is not a valid junction" unless conjunctive.respond_to?(:junction_key)

    _couplings[conjunctive.junction_key][to] = conjunctive
  end

  _couplings[to.junction_key][conjunctive] = to
end

.couples?(junction) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/conjunction/nexus.rb', line 23

def couples?(junction)
  _couplings.key?(junction.try(:junction_key))
end