Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active August 17, 2018 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/5e6d0febe9c4c0df8cdd70609719c67e to your computer and use it in GitHub Desktop.
Save krisleech/5e6d0febe9c4c0df8cdd70609719c67e to your computer and use it in GitHub Desktop.
Oxygen

Oxygen is an private gem which has common Ruby code used across all our apps, gems and engines. It has no framework (Rails) code. Oxygen::Configuration uses Dry::Configurable.

You might configure it, in a Rails initalizer, as such:

Oxygen.configure do |c|
  c.client_name = 'Acme Inc.'
  c.client_key = 'acme'
  c.exception_handler = Oxygen::ExceptionHandler.new
  c.app_root = Rails.root
  c.env = Rails.env
  c.logger = Rails.logger
  c.fy_month_start = 4
  c.fy_month_end = 3
end

In specs we can set any of these configurations to other values so long as we reset it back to know values after each spec has run. It might be better if the initially set values where recorded and then could be restored with Oxygen.configuration.reset!. Even better might be to allow a block to be passed where some configuration only applies for the duration of the block before it is reverted back to its original state, but I don't know how this might work considering we use a block to do the actual configuration.

Maybe:

configuration = Oxygen::Configuration.new do
  c.env = 'production'
end

Oxygen.configure_with(configuration) do
  # spec
end
@paneq
Copy link

paneq commented Aug 17, 2018

but I don't know how this might work considering we use a block to do the actual configuration.

Check out: http://railseventstore.org/docs/subscribe/ for inspiration:

    event_store.within do
      Operation.new.run(file)
    end.subscribe(to: [OperationSucceeded]) do
      redirect_to results_index_path
    end.subscribe(to: [OperationFailed]) do
      render :new
    end.call

Implementation: https://github.com/RailsEventStore/rails_event_store/blob/a7d5894721fc45357a4d8f9b20097fc27139b05d/ruby_event_store/lib/ruby_event_store/client.rb#L230-L306

@paneq
Copy link

paneq commented Aug 17, 2018

def test_something_with_config
  configuration.within do
    # tests here with different config applied
  end.set do |c|
    c.logger = Logger.new(STDOUT)
  end.call
end

you could even skip .call if you knew that that within and set are always provided once only. In RES there can be many subscribers so we explicitly need a trigger.

@paneq
Copy link

paneq commented Aug 17, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment