Module: Wireless::Fetch

Included in:
Fetcher, Registry
Defined in:
lib/wireless/fetch.rb

Overview

A mixin which provides the #fetch method (and #[] alias) shared by Wireless::Registry and the cut-down version, Wireless::Fetcher, which is passed to the blocks used to resolve dependencies.

In both cases, @registry and @seen need to be defined as instance variables.

Instance Method Summary collapse

Instance Method Details

#fetch(name) ⇒ Object Also known as: []

Fetches the dependency with the specified name. Creates the dependency if it doesn’t exist. Raises a Wireless::KeyError if the dependency is not defined or a Wireless::CycleError if resolving the dependency results in a cycle.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wireless/fetch.rb', line 16

def fetch(name)
  name = name.to_sym

  if @seen.include?(name)
    path = [*@seen, name].join(' -> ')
    raise Wireless::CycleError, "cycle detected: #{path}"
  end

  unless (resolver = @registry[name])
    raise Wireless::KeyError.new(
      "dependency not found: #{name}",
      key: name,
      receiver: self
    )
  end

  fetcher = lambda do
    seen = @seen.dup
    seen.add(name)
    Fetcher.new(registry: @registry, seen: seen)
  end

  resolver.resolve(fetcher)
end