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



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

def enable_stubs!
  # DO NOTHING
end

#resolve(key) ⇒ Object

Overrides resolve to look into stubbed keys first



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

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

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

Add a stub to the container



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

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



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

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