开发者

Rails engine 'require' prefix

开发者 https://www.devze.com 2023-02-24 06:07 出处:网络
If i have a gem \'foo\' (Which is engine-based), how can i say that all files included in it will need the \'foo/\' prefix when \'require\'开发者_如何学Going them?

If i have a gem 'foo' (Which is engine-based), how can i say that all files included in it will need the 'foo/' prefix when 'require'开发者_如何学Going them?

For instance, if in my 'foo' gem i have a 'bar.rb' file, i'd like to be able to force that the inclusion of this file are done like this:

require 'foo/bar'

instead of the usual

require 'bar'

i want this behavior to apply whatever the inclusion context is (From an APP, from another gem, etc)...

i know it's doable since most of rails files are included through the 'rails' prefix:

require 'rails/something'


You can't do that programatically other than manually specifying the prefix manually when the files are required. There's no way for Ruby to know other than if you tell it.


How about this:

Dir["/path/to/directory/*.rb"].each {|file| require file }

Just make sure you have your foo in the root directory and the rest of the files in the subdirectory (otherwise it'll include itself)

0

精彩评论

暂无评论...
验证码 换一张
取 消