Skip to content

Commit

Permalink
Add support for dry-system 0.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zlw committed Sep 19, 2021
1 parent 3f787c3 commit d3a0f67
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 174 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ jobs:
- "3.0"
- "2.7"
- "2.6"
- "2.5"
- "jruby"
include:
- ruby: "3.0"
coverage: "true"
Expand Down
5 changes: 4 additions & 1 deletion docsite/source/index.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
6 changes: 3 additions & 3 deletions dry-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions lib/dry/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 9 additions & 91 deletions lib/dry/rails/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Expand All @@ -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
Expand All @@ -57,90 +49,16 @@ class Container < System::Container
#
# @api public
# @!scope class
setting :container_constant, "Container", reader: true
setting :container_constant,
default: "Container",
reader: true

# @!endgroup

# The railtie has a rails-specific auto-registrar which is app-dir aware
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<String>] 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]
Expand Down
2 changes: 0 additions & 2 deletions lib/dry/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy-6.x/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/forms/create_user_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class CreateUserForm
end
10 changes: 9 additions & 1 deletion spec/dummy/config/initializers/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This file was deleted.

This file was deleted.

19 changes: 10 additions & 9 deletions spec/integration/dry/rails/container/resolve_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion spec/integration/dry/rails/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions spec/integration/dry/rails/railtie/finalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion templates/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d3a0f67

Please sign in to comment.