dry-system-rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +15 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +59 -0
  6. data/.travis.yml +15 -2
  7. data/CHANGELOG.md +11 -0
  8. data/Gemfile +3 -0
  9. data/README.md +55 -19
  10. data/Rakefile +2 -0
  11. data/dry-system-rails.gemspec +2 -0
  12. data/lib/dry-system-rails.rb +2 -0
  13. data/lib/dry/system/rails.rb +37 -55
  14. data/lib/dry/system/rails/container.rb +56 -0
  15. data/lib/dry/system/rails/railtie.rb +67 -0
  16. data/lib/dry/system/rails/version.rb +3 -1
  17. data/spec/dummy/Gemfile +2 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  19. data/spec/dummy/app/models/user_repo.rb +1 -1
  20. data/spec/dummy/app/operations/create_user.rb +2 -0
  21. data/spec/dummy/app/workers/mailer_worker.rb +5 -0
  22. data/spec/dummy/config/application.rb +2 -0
  23. data/spec/dummy/config/boot.rb +2 -0
  24. data/spec/dummy/config/environment.rb +2 -0
  25. data/spec/dummy/config/environments/development.rb +2 -0
  26. data/spec/dummy/config/environments/production.rb +2 -0
  27. data/spec/dummy/config/environments/test.rb +2 -0
  28. data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
  29. data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
  30. data/spec/dummy/config/initializers/container.rb +8 -2
  31. data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
  32. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  33. data/spec/dummy/config/initializers/inflections.rb +2 -0
  34. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  35. data/spec/dummy/config/initializers/new_framework_defaults.rb +2 -0
  36. data/spec/dummy/config/initializers/session_store.rb +2 -0
  37. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -0
  38. data/spec/dummy/config/routes.rb +2 -0
  39. data/spec/dummy/config/spring.rb +2 -0
  40. data/spec/dummy/config/system/boot/persistence.rb +5 -1
  41. data/spec/dummy/lib/mailer.rb +2 -0
  42. data/spec/integration/container_spec.rb +12 -0
  43. data/spec/integration/environments_spec.rb +15 -0
  44. data/spec/integration/models/user_repo_spec.rb +2 -0
  45. data/spec/integration/railtie_spec.rb +25 -0
  46. data/spec/spec_helper.rb +17 -4
  47. metadata +15 -6
  48. data/spec/dummy/config/system/import.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 746486d34b40312cbc2a40f629e30d5c570042e6b5630ae93681928e18b9126c
4
- data.tar.gz: dc657dbe569a28e509dd5cca5618673edc8e6377565b674b67797ceb659041f6
3
+ metadata.gz: 90d51a08b5024e86dc3bf7af90b18b6f39a5d8f5bcc7cd219b57c0fac0a9670a
4
+ data.tar.gz: 81e0a1be3b3b7bcfe394bf21bf4931b11250c8612af936766622eb98a1a88d1b
5
5
  SHA512:
6
- metadata.gz: 6c373f00f28132e84e3a5c1419588e98cb12884d44fdabff2640b94e67279eb68c746d9469517277b4101471b0f9e39f1348f5b71800fcafb2f1a75157c260f6
7
- data.tar.gz: 576cdd865136d905943c2806b89a9d398109a6a945a0dd096fbe023cc47b324d0136874c743a563b7b2d4ee87c3b89289756a283e9646781d8dcab78fa2bb6f6
6
+ metadata.gz: cfd84104666878865808a1df1b13bd331475a2422fd92b8a6fb05acbb214e5fcbbce6427661037facf186f2cadacf42b2879816408f56030f2aef557d22affe9
7
+ data.tar.gz: 974009073ebe8cddc8ac6f61395e055257dedc90311fdcdf386ba0631b23d08abc2d09e569c193c795b90e48d455c07a6bd0d5a50e0b05579b454c008414996d
data/.codeclimate.yml ADDED
@@ -0,0 +1,15 @@
1
+ version: "2"
2
+
3
+ prepare:
4
+ fetch:
5
+ - url: "https://raw.githubusercontent.com/dry-rb/devtools/master/.rubocop.yml"
6
+ path: ".rubocop.yml"
7
+
8
+ exclude_patterns:
9
+ - "benchmarks/"
10
+ - "examples/"
11
+ - "spec/"
12
+
13
+ plugins:
14
+ rubocop:
15
+ enabled: true
data/.gitignore CHANGED
@@ -6,3 +6,5 @@ bin/
6
6
  tmp/
7
7
  .idea/
8
8
  Gemfile.lock
9
+ .byebug_history
10
+ spec/dummy/.bundle
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --color
2
2
  --require spec_helper
3
3
  --order random
4
+ --tag ~production_env
data/.rubocop.yml ADDED
@@ -0,0 +1,59 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/EachWithObject:
5
+ Enabled: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: single_quotes
10
+
11
+ Style/Alias:
12
+ Enabled: false
13
+
14
+ Style/LambdaCall:
15
+ Enabled: false
16
+
17
+ Style/StabbyLambdaParentheses:
18
+ Enabled: false
19
+
20
+ Style/FormatString:
21
+ Enabled: false
22
+
23
+ Layout/SpaceInLambdaLiteral:
24
+ Enabled: false
25
+
26
+ Layout/MultilineMethodCallIndentation:
27
+ Enabled: true
28
+ EnforcedStyle: indented
29
+
30
+ Metrics/LineLength:
31
+ Max: 100
32
+
33
+ Metrics/MethodLength:
34
+ Max: 22
35
+
36
+ Metrics/ClassLength:
37
+ Max: 150
38
+
39
+ Metrics/AbcSize:
40
+ Max: 20
41
+
42
+ Metrics/BlockLength:
43
+ Enabled: true
44
+ Exclude:
45
+ - 'spec/**/*_spec.rb'
46
+
47
+ Metrics/CyclomaticComplexity:
48
+ Enabled: true
49
+ Max: 10
50
+
51
+ Lint/BooleanSymbol:
52
+ Enabled: false
53
+
54
+ Style/AccessModifierDeclarations:
55
+ Enabled: false
56
+
57
+ Style/BlockDelimiters:
58
+ EnforcedStyle: semantic
59
+
data/.travis.yml CHANGED
@@ -1,11 +1,24 @@
1
1
  language: ruby
2
- sudo: false
3
2
  cache: bundler
4
3
  bundler_args: --without console
4
+ before_script:
5
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
6
+ - chmod +x ./cc-test-reporter
7
+ - ./cc-test-reporter before-build
8
+ after_script:
9
+ - "[ -d coverage ] && ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
5
10
  script:
6
11
  - bundle exec rake spec
12
+ - RAILS_ENV=production bundle exec rspec --tag production_env
7
13
  rvm:
8
- - 2.3.1
14
+ - 2.6.2
15
+ - 2.5.5
16
+ - 2.4.6
17
+ - 2.3.8
18
+ - truffleruby
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: truffleruby
9
22
  notifications:
10
23
  email: false
11
24
  webhooks:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # v0.2.0 2019-04-16
2
+
3
+ ### Added
4
+
5
+ - Support for code reloading in development environment (arielvalentin + solnic)
6
+
7
+ ### Changed
8
+
9
+ - [BREAKING] Initializer interface is now `Dry::System::Rails.container { ... }` which simply captures the block
10
+ to evaluate it in the context of the container class. This gives you full control over container creation (solnic)
11
+
1
12
  # v0.1.0 2018-01-05
2
13
 
3
14
  ### Changed
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
@@ -5,6 +7,7 @@ gemspec
5
7
  group :test do
6
8
  gem 'railties'
7
9
  gem 'actionpack'
10
+ gem 'simplecov', require: false, platform: :mri
8
11
  end
9
12
 
10
13
  group :tools do
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  [gem]: https://rubygems.org/gems/dry-system-rails
2
2
  [travis]: https://travis-ci.org/dry-rb/dry-system-rails
3
3
  [codeclimate]: https://codeclimate.com/github/dry-rb/dry-system-rails
4
- [coveralls]: https://coveralls.io/r/dry-rb/dry-system-rails
4
+ [chat]: https://dry-rb.zulipchat.com
5
5
  [inchpages]: http://inch-ci.org/github/dry-rb/dry-system-rails
6
6
 
7
- # dry-system-rails [![Join the chat at https://gitter.im/dry-rb/chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dry-rb/chat)
7
+ # dry-system-rails [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
8
8
 
9
9
  [![Gem Version](https://badge.fury.io/rb/dry-system-rails.svg)][gem]
10
10
  [![Build Status](https://travis-ci.org/dry-rb/dry-system-rails.svg?branch=master)][travis]
@@ -22,39 +22,76 @@ Add it to your Gemfile:
22
22
  gem 'dry-system-rails'
23
23
  ```
24
24
 
25
- Assuming your `Rails.application.class` is `MyApp::Application`, add `config/system/import.rb`
26
- file with the following content:
27
-
28
- ``` ruby
29
- # config/system/import.rb
30
- module MyApp
31
- Import = Container.injector
32
- end
33
- ```
25
+ ## Usage
34
26
 
35
27
  To configure auto-registration create `config/initializer/system.rb` with the following content:
36
28
 
37
29
  ``` ruby
38
- require 'dry/system/rails'
39
-
40
- Dry::System::Rails.configure do |config|
30
+ Dry::System::Rails.container do
41
31
  # you can set it to whatever you want and add as many dirs you want
42
32
  config.auto_register << 'lib'
43
33
  end
44
34
  ```
45
35
 
46
- Now you can use `MyApp::Import` to inject components into your objects:
36
+ The `Dry::System::Rails::Railtie` creates a container and injector on your behalf at runtime and assign them to two constants `Container` and `Import`
37
+ under your applications root module. E.g. if your application is named `MyApp`, the `Railtie` will add the following constants:
38
+
39
+ * `MyApp::Container`
40
+ * `MyApp::Import`
41
+
42
+ Now you can use `MyApp::Import` to inject components into your objects and framework components:
47
43
 
48
44
  ``` ruby
49
45
  # lib/user_repo.rb
50
46
  class UserRepo
47
+
51
48
  end
52
49
 
53
50
  # lib/create_user.rb
54
- require 'import'
55
-
56
51
  class CreateUser
57
- include Import['user_repo']
52
+ include MyApp::Import['user_repo']
53
+ end
54
+
55
+ # app/controllers/users_controller.rb
56
+ class UsersController < ApplicationController
57
+ include MyApp::Import['create_user']
58
+ end
59
+ ```
60
+
61
+ ## Working with Framework Dependencies
62
+
63
+ The Rails API is designed around the usage of class methods. If you choose to write domain logic in objects you will likely encounter a situation where your code will have to use one of the framework components. That is where manual registration using [bootable dependency](https://dry-rb.org/gems/dry-system/booting) will come in handy.
64
+
65
+ E.g. You have an object `CreateWidget` that needs to process widgets asynchronously with an `Widgets:NotificationJob` but you want to leverage dependency injection to decouple the components:
66
+
67
+ ```ruby
68
+ # config/initializer/system.rb
69
+ Dry::System::Rails.configure do |config|
70
+ config.auto_register << 'lib'
71
+ end
72
+
73
+ # app/jobs/widgets/notification_job.rb
74
+ class Widgets::NotificationJob < ApplicationJob
75
+ end
76
+
77
+ # config/system/boot/application.rb
78
+ # Use bootable componets to manually register framework dependencies
79
+ MyApp::Container.boot(:application) do |app|
80
+ setup do
81
+ app.namespace(:widgets) do |widgets|
82
+ widgets.register(:notification, memoize: true) { Widgets::NotificationJob }
83
+ end
84
+ end
85
+ end
86
+
87
+ # lib/create_widget.rb
88
+ class CreateWidget
89
+ include MyApp::Import[job: 'widgets.notification']
90
+
91
+ def call(args)
92
+ # some logic that creates a widget command
93
+ job.perform_later(create_widget_command)
94
+ end
58
95
  end
59
96
  ```
60
97
 
@@ -62,7 +99,6 @@ end
62
99
 
63
100
  This is super alpha and it's missing a couple of things:
64
101
 
65
- * Support for code reloading in dev mode
66
102
  * Some generators to make UX nicer
67
103
  * Tests for loading scripts (console etc)
68
104
  * Tests for running rake tasks
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
2
4
  require 'bundler/gem_tasks'
3
5
 
4
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
@@ -1,4 +1,6 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  require File.expand_path('../lib/dry/system/rails/version', __FILE__)
3
5
 
4
6
  Gem::Specification.new do |spec|
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry/system/rails'
@@ -1,71 +1,53 @@
1
- require 'dry/system/container'
2
- require 'rails/railtie'
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/system/rails/railtie'
4
+ require 'dry/system/rails/container'
3
5
 
4
6
  module Dry
5
7
  module System
8
+ # Initializer interface
9
+ #
10
+ # @example set up a customized container
11
+ # # config/initializer/system.rb
12
+ #
13
+ # Dry::System::Rails.container do
14
+ # use :monitoring
15
+ #
16
+ # config.auto_register << 'app/operations'
17
+ # end
18
+ #
19
+ # @api public
6
20
  module Rails
7
- extend Dry::Configurable
8
-
9
- setting :auto_register, []
10
-
11
- def self.configure
12
- super
13
-
14
- container = create_container(config)
15
-
16
- Railtie.configure do
17
- config.container = container
18
- end
21
+ # Set container block that will be evaluated in the context of the container
22
+ #
23
+ # @api public
24
+ def self.container(&block)
25
+ @container_block = block
26
+ self
19
27
  end
20
28
 
21
- def self.create_container(defaults)
22
- auto_register = defaults.auto_register
23
-
24
- container = Class.new(Dry::System::Container).configure do |config|
29
+ # Create a new container class
30
+ #
31
+ # This is used during booting and reloading
32
+ #
33
+ # @api private
34
+ def self.create_container(options = {})
35
+ container = Class.new(Container).configure { |config|
25
36
  config.root = ::Rails.root
26
37
  config.system_dir = config.root.join('config/system')
27
- config.auto_register = auto_register
28
- end
38
+ config.update(options)
39
+ }
29
40
 
30
41
  container.load_paths!('lib', 'app', 'app/models')
31
- end
32
-
33
- class Railtie < ::Rails::Railtie
34
- initializer 'dry.system.create_container' do
35
- System::Rails.configure
36
- end
37
-
38
- config.to_prepare do |*args|
39
- Railtie.finalize!
40
- end
41
42
 
42
- def finalize!
43
- if app_namespace.const_defined?(:Container)
44
- app_namespace.send(:remove_const, :Container)
45
- end
46
- app_namespace.const_set(:Container, container)
47
- container.config.name = name
48
- container.finalize!
49
- end
43
+ container.class_eval(&@container_block) if container_block
50
44
 
51
- def name
52
- app_namespace.name.underscore.to_sym
53
- end
54
-
55
- # TODO: we had to rename namespace=>app_namespace because
56
- # Rake::DSL's Kernel#namespace *sometimes* breaks things.
57
- # Currently we are missing specs verifying that rake tasks work
58
- # correctly and those must be added!
59
- def app_namespace
60
- @app_namespace ||= begin
61
- top_level_namespace = ::Rails.application.class.to_s.split('::').first
62
- Object.const_get(top_level_namespace)
63
- end
64
- end
45
+ container
46
+ end
65
47
 
66
- def container
67
- Railtie.config.container
68
- end
48
+ # @api private
49
+ def self.container_block
50
+ defined?(@container_block) && @container_block
69
51
  end
70
52
  end
71
53
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/system/container'
4
+
5
+ module Dry
6
+ module System
7
+ module Rails
8
+ # Customized Container class for Rails application
9
+ #
10
+ # @api public
11
+ class Container < System::Container
12
+ setting :auto_register_configs, [], &:dup
13
+
14
+ class << self
15
+ # Auto register files from the provided directory
16
+ #
17
+ # @api public
18
+ def auto_register!(dir, &block)
19
+ if block
20
+ config.auto_register_configs << [dir, block]
21
+ else
22
+ config.auto_register << dir
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ # @api private
29
+ def finalize!(options = {})
30
+ config.auto_register_configs.each do |(dir, block)|
31
+ auto_registrar.call(dir, &block)
32
+ end
33
+ super
34
+ end
35
+
36
+ # Use `require_dependency` to make code reloading work
37
+ #
38
+ # @api private
39
+ def require_path(path)
40
+ require_dependency(path)
41
+ end
42
+
43
+ # This is called when reloading in dev mode
44
+ #
45
+ # @api private
46
+ def refresh_boot_files
47
+ booter.boot_files.each do |boot_file|
48
+ load(boot_file)
49
+ end
50
+ self
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module Dry
6
+ module System
7
+ module Rails
8
+ # System railtie is responsible for setting up a container and handling reloading in dev mode
9
+ #
10
+ # @api private
11
+ class Railtie < ::Rails::Railtie
12
+ config.to_prepare do
13
+ Railtie.finalize!
14
+ end
15
+
16
+ # @api private
17
+ def finalize!
18
+ container = System::Rails.create_container(name: name)
19
+
20
+ set_or_reload(:Container, container)
21
+ set_or_reload(:Import, container.injector)
22
+
23
+ container.refresh_boot_files if reloading?
24
+
25
+ container.finalize!(freeze: freeze?)
26
+ end
27
+
28
+ # @api private
29
+ def freeze?
30
+ !::Rails.env.test?
31
+ end
32
+
33
+ # @api private
34
+ def reloading?
35
+ app_namespace.const_defined?(:Container)
36
+ end
37
+
38
+ # @api private
39
+ def name
40
+ app_namespace.name.underscore.to_sym
41
+ end
42
+
43
+ # TODO: we had to rename namespace=>app_namespace because
44
+ # Rake::DSL's Kernel#namespace *sometimes* breaks things.
45
+ # Currently we are missing specs verifying that rake tasks work
46
+ # correctly and those must be added!
47
+ #
48
+ # @api private
49
+ def app_namespace
50
+ @app_namespace ||= begin
51
+ top_level_namespace = ::Rails.application.class.to_s.split("::").first
52
+ Object.const_get(top_level_namespace)
53
+ end
54
+ end
55
+
56
+ # @api private
57
+ def set_or_reload(const_name, const)
58
+ if app_namespace.const_defined?(const_name)
59
+ app_namespace.__send__(:remove_const, const_name)
60
+ end
61
+
62
+ app_namespace.const_set(const_name, const)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Dry
2
4
  module System
3
5
  module Rails
4
- VERSION = '0.1.0'.freeze
6
+ VERSION = '0.2.0'
5
7
  end
6
8
  end
7
9
  end
data/spec/dummy/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gem 'railties'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ApplicationController < ActionController::Base
2
4
  protect_from_forgery with: :exception
3
5
  end
@@ -1,4 +1,4 @@
1
- require 'import'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class UserRepo
4
4
  include Dummy::Import['persistence.db']
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Operations
2
4
  class CreateUser
3
5
  end
@@ -0,0 +1,5 @@
1
+ module Workers
2
+ class MailerWorker
3
+ include Dummy::Import['mailer']
4
+ end
5
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'boot'
2
4
 
3
5
  require 'rails'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
2
4
 
3
5
  require 'bundler/setup' # Set up gems listed in the Gemfile.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Load the Rails application.
2
4
  require_relative 'application'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # ApplicationController.renderer.defaults.merge!(
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
@@ -1,5 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry/system/rails'
2
4
 
3
- Dry::System::Rails.configure do |config|
4
- config.auto_register << 'app/operations'
5
+ Dry::System::Rails.container do
6
+ config.auto_register << 'lib' << 'app/operations'
7
+
8
+ auto_register!('app/workers') do |config|
9
+ config.memoize = true
10
+ end
5
11
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Specify a serializer for the signed and encrypted cookie jars.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Configure sensitive parameters which will be filtered from the log file.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Add new inflection rules using the following format. Inflections
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Add new mime types for use in respond_to blocks:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
  #
3
5
  # This file contains migration options to ease your Rails 5.0 upgrade.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  Rails.application.config.session_store :cookie_store, key: '_dummy_session'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # This file contains settings for ActionController::ParamsWrapper which
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
4
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  %w(
2
4
  .ruby-version
3
5
  .rbenv-vars
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Dummy::Container.boot(:persistence) do |container|
2
- container.register('persistence.db', :i_am_db)
4
+ start do
5
+ container.register("persistence.db", :i_am_db)
6
+ end
3
7
  end
@@ -0,0 +1,2 @@
1
+ class Mailer
2
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe 'Application container' do
2
4
  subject(:system) { Dummy::Container }
3
5
 
@@ -14,4 +16,14 @@ RSpec.describe 'Application container' do
14
16
  expect(system['operations.create_user']).to be_instance_of(Operations::CreateUser)
15
17
  end
16
18
  end
19
+
20
+ describe '#auto_register!' do
21
+ it 'auto-registers files based on config' do
22
+ mailer_worker = Dummy::Container['workers.mailer_worker']
23
+
24
+ expect(mailer_worker).to be_instance_of(Workers::MailerWorker)
25
+ expect(Dummy::Container['workers.mailer_worker']).to be(mailer_worker) # memoized
26
+ expect(mailer_worker.mailer).to be_instance_of(Mailer)
27
+ end
28
+ end
17
29
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe 'Environments' do
4
+ subject { Dummy::Container }
5
+
6
+ describe "frozen?" do
7
+ context 'when Rails environment is test' do
8
+ it { is_expected.not_to be_frozen }
9
+ end
10
+
11
+ context 'when Rails environment is not test', :production_env do
12
+ it { is_expected.to be_frozen }
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'user_repo'
2
4
 
3
5
  RSpec.describe UserRepo do
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Dry::System::Rails::Railtie do
4
+ subject(:railtie) do
5
+ Dry::System::Rails::Railtie.instance
6
+ end
7
+
8
+ describe '.finalize!' do
9
+ it 'reloads container and import module' do
10
+ Dummy::Container.register('foo', Object.new)
11
+
12
+ Rails.application.reloader.reload!
13
+
14
+ expect(Dummy::Container.keys).to_not include('foo')
15
+
16
+ klass = Class.new do
17
+ include Dummy::Import['operations.create_user']
18
+ end
19
+
20
+ obj = klass.new
21
+
22
+ expect(obj.create_user).to be_instance_of(Operations::CreateUser)
23
+ end
24
+ end
25
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
4
+ require 'yaml'
5
+ rubies = YAML.safe_load(File.read(File.join(__dir__, '..', '.travis.yml')))['rvm']
6
+ latest_mri = rubies.select { |v| v =~ /\A\d+\.\d+.\d+\z/ }.max
7
+
8
+ if RUBY_VERSION == latest_mri
9
+ require 'simplecov'
10
+ SimpleCov.start do
11
+ add_filter '/spec/'
12
+ end
13
+ end
14
+ end
15
+
1
16
  begin
2
17
  require 'byebug'
3
18
  rescue LoadError; end
@@ -9,9 +24,7 @@ SPEC_ROOT = Pathname(__dir__)
9
24
  Dir[SPEC_ROOT.join('shared/**/*.rb')].each(&method(:require))
10
25
  Dir[SPEC_ROOT.join('support/**/*.rb')].each(&method(:require))
11
26
 
12
- ENV['RAILS_ENV'] = 'test'
27
+ ENV['RAILS_ENV'] ||= 'test'
13
28
  require SPEC_ROOT.join('dummy/config/environment')
14
29
 
15
- RSpec.configure do |config|
16
- config.disable_monkey_patching!
17
- end
30
+ RSpec.configure(&:disable_monkey_patching!)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-system-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-system
@@ -73,8 +73,10 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".codeclimate.yml"
76
77
  - ".gitignore"
77
78
  - ".rspec"
79
+ - ".rubocop.yml"
78
80
  - ".travis.yml"
79
81
  - CHANGELOG.md
80
82
  - CONTRIBUTING.md
@@ -85,12 +87,15 @@ files:
85
87
  - dry-system-rails.gemspec
86
88
  - lib/dry-system-rails.rb
87
89
  - lib/dry/system/rails.rb
90
+ - lib/dry/system/rails/container.rb
91
+ - lib/dry/system/rails/railtie.rb
88
92
  - lib/dry/system/rails/version.rb
89
93
  - spec/dummy/.rspec
90
94
  - spec/dummy/Gemfile
91
95
  - spec/dummy/app/controllers/application_controller.rb
92
96
  - spec/dummy/app/models/user_repo.rb
93
97
  - spec/dummy/app/operations/create_user.rb
98
+ - spec/dummy/app/workers/mailer_worker.rb
94
99
  - spec/dummy/config/application.rb
95
100
  - spec/dummy/config/boot.rb
96
101
  - spec/dummy/config/environment.rb
@@ -112,10 +117,12 @@ files:
112
117
  - spec/dummy/config/secrets.yml
113
118
  - spec/dummy/config/spring.rb
114
119
  - spec/dummy/config/system/boot/persistence.rb
115
- - spec/dummy/config/system/import.rb
120
+ - spec/dummy/lib/mailer.rb
116
121
  - spec/dummy/log/.keep
117
122
  - spec/integration/container_spec.rb
123
+ - spec/integration/environments_spec.rb
118
124
  - spec/integration/models/user_repo_spec.rb
125
+ - spec/integration/railtie_spec.rb
119
126
  - spec/spec_helper.rb
120
127
  homepage: https://github.com/dry-rb/dry-system-rails
121
128
  licenses:
@@ -136,8 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
143
  - !ruby/object:Gem::Version
137
144
  version: '0'
138
145
  requirements: []
139
- rubyforge_project:
140
- rubygems_version: 2.7.7
146
+ rubygems_version: 3.0.3
141
147
  signing_key:
142
148
  specification_version: 4
143
149
  summary: Railtie for dry-system
@@ -147,6 +153,7 @@ test_files:
147
153
  - spec/dummy/app/controllers/application_controller.rb
148
154
  - spec/dummy/app/models/user_repo.rb
149
155
  - spec/dummy/app/operations/create_user.rb
156
+ - spec/dummy/app/workers/mailer_worker.rb
150
157
  - spec/dummy/config/application.rb
151
158
  - spec/dummy/config/boot.rb
152
159
  - spec/dummy/config/environment.rb
@@ -168,8 +175,10 @@ test_files:
168
175
  - spec/dummy/config/secrets.yml
169
176
  - spec/dummy/config/spring.rb
170
177
  - spec/dummy/config/system/boot/persistence.rb
171
- - spec/dummy/config/system/import.rb
178
+ - spec/dummy/lib/mailer.rb
172
179
  - spec/dummy/log/.keep
173
180
  - spec/integration/container_spec.rb
181
+ - spec/integration/environments_spec.rb
174
182
  - spec/integration/models/user_repo_spec.rb
183
+ - spec/integration/railtie_spec.rb
175
184
  - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- module Dummy
2
- Import = Container.injector
3
- end