Module: Dry::Container::Stub

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

Instance Method Summary collapse

Instance Method Details

#enable_stubs!Object

Stubs have already been enabled turning this into a noop



34
35
36
# File 'lib/dry/container/stub.rb', line 34

def enable_stubs!
  # DO NOTHING
end

#resolve(key) ⇒ Object

Overrides resolve to look into stubbed keys first



7
8
9
# File 'lib/dry/container/stub.rb', line 7

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

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

Add a stub to the container



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dry/container/stub.rb', line 12

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



28
29
30
31
# File 'lib/dry/container/stub.rb', line 28

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