Skip to content

fixes #150: generate Contracts class #156

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

Closed

Conversation

robnormal
Copy link
Contributor

To generate the class statically, use bin/generate-contracts. This generates the "standard" Contracts class. To override code, you will have to use the admittedly awkward setup below. Contracts.configure(:contracts_use_file => false) causes the class to be generated on the fly and eval-ed, instead of loading from the existing file.

As an example, including the following at the top of spec/spec_helper.rb fixes #14:

module Contracts
  @load_on_require = false
  require 'contracts'

  Contracts.configure(
    :contracts_generator => Contracts::ContractsGenerator.new(
      'Class' => "require 'rspec'; lambda { |arg| arg.is_a?(contract) || RSpec::Mocks::Double }"
    ),
    :contracts_use_file => false
  )
  Contracts.load
end

The template for contract.rb lives in templates/contract.text.erb. ContractsGenerator does the generating. So far, I've broken out only the part I needed to override - namely, the lambdas used to check Class and other basic contracts.

@alex-fedorov
Copy link
Collaborator

@robnormal can you get rid of require_relative ? (travis failure) (load should be fine in this case)

@egonSchiele can you take a look at this one? I am still on the fence and have no real opinion on the API that we want to provide for such code inlining and overriding.

@egonSchiele
Copy link
Owner

@robnormal thanks for taking the time to do this PR. I don't like the pre-compile step because the code is getting pretty complicated (I appreciate that you made a minimal amount of changes, but it's still very different from most ruby projects). I'm afraid this is going to make it hard for newcomers to understand the code. So it will slow down contribution and monkeypatching. I think I would rather find a traditional solution, even if it means adding a few more methods and marginally slowing things down.

@alex-fedorov
Copy link
Collaborator

@robnormal I have an idea, how your problem can be fixed without code-generating, I will create a PR and will use your case as example soon.

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

Successfully merging this pull request may close these issues.

Integration with rspec/mocking
3 participants