Class: Synapse::Wiring::WireRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/wiring/wire_registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(duplicates_allowed) ⇒ undefined

Parameters:

  • duplicates_allowed (Boolean)


6
7
8
9
# File 'lib/synapse/wiring/wire_registry.rb', line 6

def initialize(duplicates_allowed)
  @duplicates_allowed = duplicates_allowed
  @wires = Array.new
end

Instance Method Details

#each_type {|Class| ... } ⇒ undefined

Yields the type that each wire is registered for

Yields:

  • (Class)

Returns:

  • (undefined)


15
16
17
18
19
# File 'lib/synapse/wiring/wire_registry.rb', line 15

def each_type
  @wires.each do |wire|
    yield wire.type
  end
end

#register(wire) ⇒ undefined

Parameters:

Returns:

  • (undefined)

Raises:

  • (DuplicateWireError)

    If duplicates aren’t allowed and another wire exists that wires the exact same type as the given wire



25
26
27
28
29
30
31
32
33
34
# File 'lib/synapse/wiring/wire_registry.rb', line 25

def register(wire)
  unless @duplicates_allowed
    if @wires.include? wire
      raise DuplicateWireError
    end
  end

  @wires.push wire
  @wires.sort!
end

#wire_for(target_type) ⇒ Wire

Retrieves the most specific wire for a given type, if any

Parameters:

  • target_type (Class)

Returns:



40
41
42
43
44
# File 'lib/synapse/wiring/wire_registry.rb', line 40

def wire_for(target_type)
  @wires.find do |wire|
    wire.type >= target_type
  end
end

#wires_for(target_type) ⇒ Array

Retrieves any wires for a given type, regardless of specificity

Parameters:

  • target_type (Class)

Returns:

  • (Array)


50
51
52
53
54
# File 'lib/synapse/wiring/wire_registry.rb', line 50

def wires_for(target_type)
  @wires.find_all do |wire|
    wire.type >= target_type
  end
end