rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.08k stars 161 forks source link

Error registering the lib directory automatically #573

Closed AlexanderMint closed 4 years ago

AlexanderMint commented 4 years ago

Describe the bug

Auto registration error "uninitialized constant Lib (NameError)"

Traceback:

Traceback (most recent call last):
        7: from app.rb:4:in `<main>'
        6: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/2.6.0/forwardable.rb:230:in `auto_registration'
        5: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.1.2/lib/rom/setup.rb:51:in `auto_registration'
        4: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.1.2/lib/rom/setup/auto_registration.rb:58:in `relations'
        3: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.1.2/lib/rom/setup/auto_registration.rb:81:in `load_entities'
        2: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.1.2/lib/rom/setup/auto_registration.rb:81:in `map'
        1: from /Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.1.2/lib/rom/setup/auto_registration.rb:99:in `block in load_entities'
/Users/man/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/dry-inflector-0.2.0/lib/dry/inflector.rb:90:in `constantize': uninitialized constant Lib (NameError)

To Reproduce

  1. Create file app.rb
require 'rom'

configuration = ROM::Configuration.new(:memory)
configuration.auto_registration(File.expand_path('.') + '/lib')
container = ROM.container(configuration)
  1. Create file lib/relations/users.rb
module Relations
  class Users < ROM::Relation[:memory]
    schema(infer: true)
  end
end

Source: https://rom-rb.org/5.0/learn/advanced/explicit-setup/#namespaces-inferred-from-directory-structure

  1. Run ruby app.rb

Expected behavior

Auto class registration Relations::Users

Your environment

tycooon commented 4 years ago

This took me a while to figure out that the problem is in the gem and not my setup. Certainly should be fixed ☹️

solnic commented 4 years ago

Relations, Mappers and Commands are reserved namespaces and they cannot be top-level. This is a limitation rather than a bug. Anyway, I plan to improve auto-registration in 6.0.0 so stay tuned.

tycooon commented 4 years ago

@solnic the problem is that the example in the docs doesn't work. So maybe that part should be removed from the docs at least?

solnic commented 4 years ago

@tycooon ah yeah that's not good :( It shouldn't be removed though, it should be changed. I'll take a look.

solnic commented 4 years ago

OK I updated this section in the current + old doc versions and now it should reflect the reality better: https://rom-rb.org/learn/core/5.2/framework-setup/#auto-registration