diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc01acc..897d8de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,8 +28,6 @@ jobs: - "3.0" - "2.7" - "2.6" - - "2.5" - - "jruby" include: - ruby: "3.0" coverage: "true" diff --git a/docsite/source/index.html.md b/docsite/source/index.html.md index 028f8cc..e9b41ee 100644 --- a/docsite/source/index.html.md +++ b/docsite/source/index.html.md @@ -49,7 +49,10 @@ Currently, the railtie **does not make any assumptions about your directory/file ```ruby # config/initializers/system.rb Dry::Rails.container do - auto_register!("app/operations") + config.component_dirs.add "app/operations" + config.component_dirs.add "lib" do |dir| + dir.default_namespace = "my_super_cool_app" + end end ``` diff --git a/dry-rails.gemspec b/dry-rails.gemspec index 6f3d5bc..032840a 100644 --- a/dry-rails.gemspec +++ b/dry-rails.gemspec @@ -29,9 +29,9 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.6.0" # to update dependencies edit project.yml - spec.add_runtime_dependency "dry-schema", "~> 1.5" - spec.add_runtime_dependency "dry-system", "~> 0.18.0", ">= 0.18.1" - spec.add_runtime_dependency "dry-validation", "~> 1.5" + spec.add_runtime_dependency "dry-schema", "~> 1.8" + spec.add_runtime_dependency "dry-system", "~> 0.20.0", ">= 0.18.1" + spec.add_runtime_dependency "dry-validation", "~> 1.7" spec.add_development_dependency "bundler" spec.add_development_dependency "rake" diff --git a/lib/dry/rails.rb b/lib/dry/rails.rb index c7551e0..839a3bf 100644 --- a/lib/dry/rails.rb +++ b/lib/dry/rails.rb @@ -11,10 +11,12 @@ module Dry # # config/initializer/system.rb # # Dry::Rails.container do - # auto_register!("lib", "app/operations") - # end + # config.component_dirs.add "lib" do |dir| + # dir.default_namespace = "my_super_cool_app" + # end # - # @see Dry::Rails::Container.auto_register! + # config.component_dirs.add "app/operations" + # end # # @api public module Rails diff --git a/lib/dry/rails/container.rb b/lib/dry/rails/container.rb index ae4b6be..dcad37d 100644 --- a/lib/dry/rails/container.rb +++ b/lib/dry/rails/container.rb @@ -27,19 +27,9 @@ class Container < System::Container # # @api public # @!scope class - setting :features, %i[application_contract safe_params controller_helpers], reader: true - - # @overload config.auto_register_paths=(paths) - # Set an array of path/block pairs for auto-registration - # - # This is a low-level setting that typically should not be set explicitly, - # use `auto_register!` instead. - # - # @param paths [Array] - # - # @api public - # @!scope class - setting :auto_register_paths, [].freeze, reader: true + setting :features, + default: %i[application_contract safe_params controller_helpers], + reader: true # @overload config.auto_inject_constant=(auto_inject_constant) # Set a custom import constant name @@ -48,7 +38,9 @@ class Container < System::Container # # @api public # @!scope class - setting :auto_inject_constant, "Deps", reader: true + setting :auto_inject_constant, + default: "Deps", + reader: true # @overload config.container_constant=(container_constant) # Set a custom container constant @@ -57,7 +49,9 @@ class Container < System::Container # # @api public # @!scope class - setting :container_constant, "Container", reader: true + setting :container_constant, + default: "Container", + reader: true # @!endgroup @@ -65,82 +59,6 @@ class Container < System::Container config.auto_registrar = Rails::AutoRegistrars::App class << self - # Set up auto-registration paths and optional a configuration block - # - # @example set up a single path - # Dry::Rails.container do - # auto_register!("app/operations") - # end - # - # @example set up a single path with a configuration block - # Dry::Rails.container do - # auto_register!("app/operations") do |config| - # config.exclude do |component| - # component.path.start_with?("concerns") - # end - # end - # end - # - # @example set up multiple paths - # Dry::Rails.container do - # auto_register!("lib", "app/operations") - # end - # - # @example set up multiple paths with a configuration block - # Dry::Rails.container do - # # in this case the config block will be applied to all paths - # auto_register!("lib", "app/operations") do |config| - # config.exclude do |component| - # component.path.start_with?("concerns") - # end - # end - # end - # - # @param paths [Array] One or more paths relative to the root - # @param set_load_paths [Boolean] Whether the paths should be added to $LOAD_PATH - # @param load_files [Boolean] Whether files should be `required`-ed already - # - # @return [self] - # - # @api public - # - # TODO: this should be moved to dry-system - def auto_register!(*paths, set_load_paths: true, load_files: false, &block) - load_paths!(*paths) if set_load_paths - - if load_files - paths.each { |path| super(path, &block) } - else - config.auto_register_paths.concat(paths.product([block])) - end - - self - end - - # Finalize the container - # - # This is called automatically via the railtie, so typically you won't be using this method - # directly - # - # @param freeze [Boolean] Whether the container should be frozen upon finalization - # - # @return [self] - # - # @api public - # - # TODO: just like auto_register!, this should be moved to dry-system - def finalize!(freeze: false, &block) - features.each do |feature| - start(feature) - end - - auto_register_paths.each do |(path, path_block)| - auto_register!(path, set_load_paths: false, load_files: true, &path_block) - end - - super - end - # Return if a given component was booted # # @return [Boolean] diff --git a/lib/dry/rails/railtie.rb b/lib/dry/rails/railtie.rb index 10a2a14..4063b93 100644 --- a/lib/dry/rails/railtie.rb +++ b/lib/dry/rails/railtie.rb @@ -33,8 +33,6 @@ def finalize! container = Dry::Rails.create_container( root: root_path, - name: name, - default_namespace: name.to_s, inflector: default_inflector, system_dir: root_path.join("config/system"), bootable_dirs: [root_path.join("config/system/boot")] diff --git a/spec/dummy-6.x/dummy/config/environments/test.rb b/spec/dummy-6.x/dummy/config/environments/test.rb index bae3b39..c045a26 100644 --- a/spec/dummy-6.x/dummy/config/environments/test.rb +++ b/spec/dummy-6.x/dummy/config/environments/test.rb @@ -18,7 +18,7 @@ # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{1.hour.to_i}" + "Cache-Control" => "public, max-age=3600" } # Show full error reports and disable caching. diff --git a/spec/dummy/app/forms/create_user_form.rb b/spec/dummy/app/forms/create_user_form.rb new file mode 100644 index 0000000..0b928fc --- /dev/null +++ b/spec/dummy/app/forms/create_user_form.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class CreateUserForm +end diff --git a/spec/dummy/config/initializers/system.rb b/spec/dummy/config/initializers/system.rb index 82ce138..2e7ceba 100644 --- a/spec/dummy/config/initializers/system.rb +++ b/spec/dummy/config/initializers/system.rb @@ -3,5 +3,13 @@ require "dry/rails" Dry::Rails.container do - auto_register!("lib") + config.component_dirs.add "lib" do |dir| + dir.default_namespace = "dummy" + end + + config.component_dirs.add "app/operations" + config.component_dirs.add "app/services" + config.component_dirs.add("app/workers") do |dir| + dir.memoize = true + end end diff --git a/spec/integration/dry/rails/container/auto_register/with_block_spec.rb b/spec/integration/dry/rails/container/auto_register/with_block_spec.rb deleted file mode 100644 index 8fb0696..0000000 --- a/spec/integration/dry/rails/container/auto_register/with_block_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Dry::Rails::Container, ".auto_register!" do - subject(:system) { Dummy::Container } - - before(:all) do - Dry::Rails.container do - auto_register!("app/workers") do |config| - config.memoize = true - end - end - end - - it "auto-registers files based on block config" do - mailer_worker = Dummy::Container["mailer_worker"] - - expect(mailer_worker).to be_instance_of(MailerWorker) - expect(Dummy::Container["mailer_worker"]).to be(mailer_worker) # memoized - expect(mailer_worker.mailer).to be_instance_of(Mailer) - end -end diff --git a/spec/integration/dry/rails/container/auto_register/without_block_spec.rb b/spec/integration/dry/rails/container/auto_register/without_block_spec.rb deleted file mode 100644 index d2036a9..0000000 --- a/spec/integration/dry/rails/container/auto_register/without_block_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Dry::Rails::Container, ".auto_register!" do - subject(:system) { Dummy::Container } - - context "with a single path" do - before(:all) do - Dry::Rails.container do - auto_register!("app/operations") - end - end - - it "auto-registers files found under the specified path" do - create_user = Dummy::Container["create_user"] - - expect(create_user).to be_instance_of(CreateUser) - end - end - - context "with multiple paths" do - before(:all) do - Dry::Rails.container do - auto_register!("app/operations", "app/services") - end - end - - it "auto-registers files found under the specified paths" do - create_user = Dummy::Container["create_user"] - github = Dummy::Container["github"] - - expect(create_user).to be_instance_of(CreateUser) - expect(github).to be_instance_of(Github) - end - end -end diff --git a/spec/integration/dry/rails/container/resolve_spec.rb b/spec/integration/dry/rails/container/resolve_spec.rb index f5e7fc8..cad03fa 100644 --- a/spec/integration/dry/rails/container/resolve_spec.rb +++ b/spec/integration/dry/rails/container/resolve_spec.rb @@ -3,24 +3,25 @@ RSpec.describe Dry::Rails::Container, ".[]" do subject(:system) { Dummy::Container } - context "with auto-registration" do - before(:all) do - Dry::Rails.container do - auto_register!("app/operations") - end - end - + context "with auto-registration from system initializer" do it "returns an auto-registered component" do expect(system["create_user"]).to be_instance_of(CreateUser) + expect(system["github"]).to be_instance_of(Github) end - end - context "with auto-registration from system initializer" do it "returns auto-registered component with another auto-injected" do notifier = system[:notifier] expect(notifier).to be_instance_of(Dummy::Notifier) expect(notifier.mailer).to be_instance_of(Mailer) end + + it "auto-registers files based on block config" do + mailer_worker = Dummy::Container["mailer_worker"] + + expect(mailer_worker).to be_instance_of(MailerWorker) + expect(Dummy::Container["mailer_worker"]).to be(mailer_worker) # memoized + expect(mailer_worker.mailer).to be_instance_of(Mailer) + end end end diff --git a/spec/integration/dry/rails/container_spec.rb b/spec/integration/dry/rails/container_spec.rb index d5d77a4..2cbd03b 100644 --- a/spec/integration/dry/rails/container_spec.rb +++ b/spec/integration/dry/rails/container_spec.rb @@ -30,7 +30,6 @@ end it "allows setting up the container in multiple steps" do - expect(system.config.default_namespace).to eql("dummy") expect(system[:logger]).to be_instance_of(Logger) end diff --git a/spec/integration/dry/rails/railtie/finalize_spec.rb b/spec/integration/dry/rails/railtie/finalize_spec.rb index 7808549..8608e80 100644 --- a/spec/integration/dry/rails/railtie/finalize_spec.rb +++ b/spec/integration/dry/rails/railtie/finalize_spec.rb @@ -7,21 +7,23 @@ Rails.application.reloader.reload! Dry::Rails.container do - auto_register!("app/operations") + config.component_dirs.add "app/forms" end Dummy::Container.register("foo", Object.new) + expect(Dummy::Container.keys).to include("foo") + Rails.application.reloader.reload! expect(Dummy::Container.keys).to_not include("foo") klass = Class.new do - include Dummy::Deps["create_user"] + include Dummy::Deps["create_user_form"] end obj = klass.new - expect(obj.create_user).to be_instance_of(CreateUser) + expect(obj.create_user_form).to be_instance_of(CreateUserForm) end end diff --git a/templates/default.rb b/templates/default.rb index 7b670fc..0f60865 100644 --- a/templates/default.rb +++ b/templates/default.rb @@ -8,6 +8,8 @@ config.features = %i[application_contract] # enable auto-registration in the lib dir - # auto_register!('lib') + # config.component_dirs.add "lib" do |dir| + # dir.default_namespace = "my_super_cool_app" + # end end CODE