Skip to content

Commit

Permalink
Elide slice name from lib/ when autoloading
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley committed Sep 17, 2021
1 parent b449ef1 commit 49b717d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -18,7 +18,7 @@ gem "hanami-devtools", require: false, git: "https://github.com/hanami/devtools.

gem "dry-files", git: "https://github.com/dry-rb/dry-files.git", branch: "master"
gem "dry-configurable", git: "https://github.com/dry-rb/dry-configurable.git", branch: "master"
gem "dry-system", git: "https://github.com/dry-rb/dry-system.git", branch: "master"
gem "dry-system", git: "https://github.com/dry-rb/dry-system.git", branch: "explicit-two-way-component-dir-namespaces"

group :test do
gem "dotenv"
Expand Down
4 changes: 3 additions & 1 deletion lib/hanami/application.rb
Expand Up @@ -254,7 +254,9 @@ def configure_container(container)

if root.join("lib").directory?
config.component_dirs.add "lib" do |dir|
dir.default_namespace = application_name.to_s
# Make the files in `lib/[application_name]/` take the root identifier
# namespace in the container
dir.namespaces.add application_name.to_s
end

configuration.autoloader&.push_dir(root.join("lib"))
Expand Down
14 changes: 11 additions & 3 deletions lib/hanami/slice.rb
Expand Up @@ -115,12 +115,20 @@ def define_container

if root.join("lib").directory?
config.component_dirs.add "lib" do |dir|
dir.default_namespace = namespace_path.tr(File::SEPARATOR, config.namespace_separator)
if application.configuration.autoloader
# If we're using an autoloader, expect all files in the slice's `lib/` to
# use the slice's const namespace
dir.namespaces.root const: namespace_path.tr(File::SEPARATOR, config.namespace_separator)
else
# Otherwise expect them to be inside `lib/[namespace]`, and make that take
# the root identifier namespace in the container
dir.namespaces.add namespace_path.tr(File::SEPARATOR, config.namespace_separator)
end
end

application.configuration.autoloader&.push_dir(root.join("lib"))
end
end

application.configuration.autoloader&.push_dir(root.join("lib"), namespace: namespace)
end

# Force after configure hook to run
Expand Down
4 changes: 2 additions & 2 deletions spec/new_integration/container/auto_injection_spec.rb
Expand Up @@ -20,14 +20,14 @@ class SharedService
end
RUBY

write "slices/admin/lib/admin/slice_service.rb", <<~RUBY
write "slices/admin/lib/slice_service.rb", <<~RUBY
module Admin
class SliceService
end
end
RUBY

write "slices/admin/lib/admin/test_op.rb", <<~RUBY
write "slices/admin/lib/test_op.rb", <<~RUBY
module Admin
class TestOp
include Deps[
Expand Down
4 changes: 2 additions & 2 deletions spec/new_integration/container/auto_registration_spec.rb
Expand Up @@ -22,7 +22,7 @@ class Operation
end
RUBY

write "slices/admin/lib/admin/nba_jam/get_that_outta_here.rb", <<~RUBY
write "slices/admin/lib/nba_jam/get_that_outta_here.rb", <<~RUBY
module Admin
module NBAJam
class GetThatOuttaHere
Expand Down Expand Up @@ -60,7 +60,7 @@ class Operation
end
RUBY

write "slices/admin/lib/admin/nba_jam/get_that_outta_here.rb", <<~RUBY
write "slices/admin/lib/nba_jam/get_that_outta_here.rb", <<~RUBY
module Admin
module NBAJam
class GetThatOuttaHere
Expand Down
6 changes: 3 additions & 3 deletions spec/new_integration/container/autoloader_spec.rb
Expand Up @@ -23,7 +23,7 @@ class GetThatOuttaHere
end
RUBY

write "slices/admin/lib/admin/operations/create_game.rb", <<~RUBY
write "slices/admin/lib/operations/create_game.rb", <<~RUBY
module Admin
module Operations
class CreateGame
Expand All @@ -35,7 +35,7 @@ def call
end
RUBY

write "slices/admin/lib/admin/entities/game.rb", <<~RUBY
write "slices/admin/lib/entities/game.rb", <<~RUBY
# auto_register: false
module Admin
Expand All @@ -46,7 +46,7 @@ class Game
end
RUBY

write "slices/admin/lib/admin/entities/quarter.rb", <<~RUBY
write "slices/admin/lib/entities/quarter.rb", <<~RUBY
# auto_register: false
module Admin
Expand Down
4 changes: 2 additions & 2 deletions spec/new_integration/container/imports_spec.rb
Expand Up @@ -56,7 +56,7 @@ class TestOp
end
RUBY

write "slices/search/lib/search/index_entity.rb", <<~RUBY
write "slices/search/lib/index_entity.rb", <<~RUBY
module Search
class IndexEntity
end
Expand Down Expand Up @@ -91,7 +91,7 @@ class TestOp
end
RUBY

write "slices/search/lib/search/index_entity.rb", <<~RUBY
write "slices/search/lib/index_entity.rb", <<~RUBY
module Search
class IndexEntity
end
Expand Down
4 changes: 2 additions & 2 deletions spec/new_integration/view/views_spec.rb
Expand Up @@ -12,7 +12,7 @@ class Application < Hanami::Application
end
RUBY

write "slices/main/lib/main/view.rb", <<~RUBY
write "slices/main/lib/view.rb", <<~RUBY
require "hanami/view"
module Main
Expand All @@ -21,7 +21,7 @@ class View < Hanami::View
end
RUBY

write "slices/main/lib/main/views/test_view.rb", <<~RUBY
write "slices/main/lib/views/test_view.rb", <<~RUBY
module Main
module Views
class TestView < Main::View
Expand Down
4 changes: 2 additions & 2 deletions spec/new_integration/web_app_spec.rb
Expand Up @@ -37,7 +37,7 @@ class Routes < Hanami::Application::Routes

write "lib/test_app/.keep", ""

write "slices/main/lib/main/actions/home/index.rb", <<~RUBY
write "slices/main/lib/actions/home/index.rb", <<~RUBY
require "hanami/action"
module Main
Expand All @@ -53,7 +53,7 @@ def handle(*, res)
end
RUBY

write "slices/admin/lib/admin/actions/dashboard/show.rb", <<~RUBY
write "slices/admin/lib/actions/dashboard/show.rb", <<~RUBY
require "hanami/action"
module Admin
Expand Down

0 comments on commit 49b717d

Please sign in to comment.