Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts off by default instead of on by default? #248

Open
vdbijl opened this issue Jan 13, 2017 · 2 comments
Open

Contracts off by default instead of on by default? #248

vdbijl opened this issue Jan 13, 2017 · 2 comments

Comments

@vdbijl
Copy link

vdbijl commented Jan 13, 2017

Because of performance requirements we want our contract checks to only run in development/testing.
The default is that contracts are always on. Is it possible to reverse that? Off by default, on with a switch?

The reason I ask is that it is difficult to automatically guarantee that contracts are not running in production, ie the env variable is set and used correctly. The reverse is easy.

Or to rephrase my question/request. Is there an easy way make sure contracts are off in production. Or maybe we can configure it ourselves?

@egonSchiele
Copy link
Owner

egonSchiele commented Jan 17, 2017

Are you referring to the NO_CONTRACTS env var? That is the easiest way to make sure that contracts aren't running. But you can also specify your own override callback instead of using the default one with override_failure_callback. Here's a simple example:

require "contracts"
include Contracts

Contract.override_failure_callback do |data|
  p data
  false # don't run the function since the contract failed
end

Contract Num => Num
def incr a
  a + 1
end

incr("Asdas") # will just print a hash instead of throwing an error

@vdbijl
Copy link
Author

vdbijl commented Feb 11, 2017

Thanks for the feedback (sorry for the delay because holiday ;).
Yes I am referring to the NO_CONTRACTS env var. The problem is that I am (for now only) using the contracts in a gem that is used in another gem. Having the coupling via an ENV var means, that the gem that is using my gem with contracts needs to know about contracts and needs to set the ENV var (AFAIK).
So I was looking for a way to locally disable the contract checking in production from the gem that is using contracts. Performance is critical to our app that's why I am disabling the checking.

I guess the only way to do that is by monkey_patching the following methods (the ones that use NO_CONTRACTS):

  • InvariantExtension#invariant to return nil
  • MethodHandler#ignore_decorators to return true

But then I have to check this every time contracts.gem updates.

Another way might be to make contracts a development dependency and complety monkey patch the Contract method to do nothing...?

Am I making sense? :) Maybe I want to solve this in the wrong way...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants