dry-rb / dry-effects

Algebraic effects in Ruby
https://dry-rb.org/gems/dry-effects/
MIT License
112 stars 20 forks source link

Make env handler overridable #44

Closed flash-gordon closed 5 years ago

flash-gordon commented 5 years ago

The same way it works for resolve:

context 'user-provided values' do
  include Dry::Effects::Handler.Env
  include Dry::Effects.Env(:foo)

  it 'can be overridden by providing a handle option' do
    handled = with_env(foo: 'external') do
      with_env({ foo: 'internal' }, overridable: true) do
        foo
      end
    end

    expect(handled).to eql('external')
  end
end

context 'ENV' do
  include Dry::Effects.Env(env: 'RACK_ENV')

  before { ENV['RACK_ENV'] = 'test' }

  it 'can be overridden' do
    handled = with_env('RACK_ENV' => 'production') do
      with_env({}, overridable: true) do
        env
      end
    end

    expect(handled).to eql('production')
  end
end