-
Notifications
You must be signed in to change notification settings - Fork 82
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 and order of includes in the app #268
Comments
Does it work without the contract? I would expect the behavior of |
Whoops, accidentally closed. |
Yes it does work if I take the contract out. I guess it makes sense that the contract can't know about a type that hasn't been defined yet but this means I have to be very careful about where I include my contracts. It's a bit annoying for custom classes. Either that or I only return simple types which doesn't seem all that satisfactory either. |
I ran into this as well. I worked around it by stubbing any classes that are referenced in contracts before you actually load those classes. Then you don't have to worry about load order. Example: # ---------------
# lib/mygem.rb
# ---------------
module MyGem
class ClassOne; end
class ClassTwo; end
end
require "contracts"
require_relative "mygem/classone"
require_relative "mygem/classtwo"
module MyGem
def self.some_method
# Other stuff can go here if you need it.
end
end
# ---------------
# lib/mygem/classone.rb
# ---------------
module MyGem
class ClassOne
include ::Contracts::Core
Contract MyGem::ClassTwo => String
def something_that_calls_classtwo(classtwo_obj)
# Do something with classtwo_obj
end
end
end
# ---------------
# lib/mygem/classtwo.rb
# ---------------
module MyGem
class ClassTwo
include ::Contracts::Core
Contract MyGem::ClassOne => String
def something_that_calls_classone(classone_obj)
# Do something with classone_obj
end
end
end |
I have a contract like this
When the app includes the module this code is in I get an error message that says
uninitialized constant Svc::User (NameError)
So I tried ::User instead but that doesn't either. So it looks like I need to load all other classes first before I can use contracts that use them.
Is there a way to get around this?
The text was updated successfully, but these errors were encountered: