Module: Dry::Core::Container::Stub

Defined in:
lib/dry/core/container/stub.rb

Instance Method Summary collapse

Instance Method Details

#enable_stubs!Object

Stubs have already been enabled turning this into a noop



37
38
39
# File 'lib/dry/core/container/stub.rb', line 37

def enable_stubs!
  # DO NOTHING
end

#resolve(key) ⇒ Object

Overrides resolve to look into stubbed keys first



10
11
12
# File 'lib/dry/core/container/stub.rb', line 10

def resolve(key)
  _stubs.fetch(key.to_s) { super }
end

#stub(key, value, &block) ⇒ Object

Add a stub to the container



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dry/core/container/stub.rb', line 15

def stub(key, value, &block)
  unless key?(key)
    raise ArgumentError, "cannot stub #{key.to_s.inspect} - no such key in container"
  end

  _stubs[key.to_s] = value

  if block
    yield
    unstub(key)
  end

  self
end

#unstub(*keys) ⇒ Object

Remove stubbed keys from the container



31
32
33
34
# File 'lib/dry/core/container/stub.rb', line 31

def unstub(*keys)
  keys = _stubs.keys if keys.empty?
  keys.each { |key| _stubs.delete(key.to_s) }
end