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

[Feature Request] Option to skip type checking in certain cases #66

Closed
CodingAnarchy opened this issue Jun 19, 2019 · 3 comments
Closed

Comments

@CodingAnarchy
Copy link

We have a number of tests that will pass in object_double([class]) when we are running specs for our Rails application, but these all fail on the Types.Instance([class]) checks we are using. As it is difficult to add an option for a test double, since these classes are not even loaded in production environments, there is not an easy way to skip these type checks when needed.

It would be great if we could pass in an extra option to ignore the type checking in certain scenarios. Something like option :arg, type: Types.Instance(Foo), ignore_type: Rails.env.test? would be ideal.

@CodingAnarchy
Copy link
Author

CodingAnarchy commented Jun 19, 2019

The only way I was able to get this working for our system at present is to construct something like this:

type_check = Rails.env.test? ? Types.Instance(Object) : Types.Instance(Foo)
option :arg, type: type_check

Which, while functional, is not really ideal.

@nepalez
Copy link
Member

nepalez commented Jun 19, 2019

@CodingAnarchy Hi!

I don't think we should add this settings directly into dry-initializer just because this seems like an edge case on far edge. This is a "bad news"

But there's a good news too :)
Option definition is actually extensible via dispatchers. This means you can insert your own setting to option and param.

I would recommend you to read the dispatchers class with its sublcasses to look at how exactly they are implemented. Then you should possibly read dry-initializer-rails as an example of adding new setting.

@nepalez
Copy link
Member

nepalez commented Jun 19, 2019

I think you need a dispatcher like this:

# Define a dispatcher
dispatcher = lambda do |ignore_type: nil, **options|
  ignore_type&.call ? options.reject { |k| k == :type } : options
end

# Then register a dispatcher
Dry::Initializer::Dispatchers << dispatcher

You can either do this in your application or define a gem extending dry-initalizer just like dry-initialize-rails does

@nepalez nepalez closed this as completed Mar 21, 2021
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