dry-auto_inject 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -27
- data/LICENSE +1 -1
- data/README.md +15 -43
- data/dry-auto_inject.gemspec +29 -17
- data/lib/dry-auto_inject.rb +1 -1
- data/lib/dry/auto_inject.rb +1 -1
- data/lib/dry/auto_inject/dependency_map.rb +2 -0
- data/lib/dry/auto_inject/method_parameters.rb +6 -2
- data/lib/dry/auto_inject/strategies/args.rb +3 -3
- data/lib/dry/auto_inject/strategies/constructor.rb +2 -2
- data/lib/dry/auto_inject/strategies/kwargs.rb +11 -10
- data/lib/dry/auto_inject/version.rb +1 -1
- metadata +16 -34
- data/.codeclimate.yml +0 -12
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
- data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
- data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
- data/.github/workflows/ci.yml +0 -76
- data/.github/workflows/docsite.yml +0 -34
- data/.github/workflows/sync_configs.yml +0 -34
- data/.gitignore +0 -10
- data/.rspec +0 -4
- data/.rubocop.yml +0 -95
- data/.rubocop_todo.yml +0 -6
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -17
- data/Rakefile +0 -14
- data/bin/console +0 -11
- data/bin/setup +0 -7
- data/docsite/source/basic-usage.html.md +0 -104
- data/docsite/source/how-does-it-work.html.md +0 -45
- data/docsite/source/index.html.md +0 -53
- data/docsite/source/injection-strategies.html.md +0 -94
- data/rakelib/rubocop.rake +0 -20
data/.rubocop_todo.yml
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
-
# on 2015-08-19 22:11:28 +0100 using RuboCop version 0.32.0.
|
3
|
-
# The point is for the user to remove these configuration records
|
4
|
-
# one by one as the offenses are removed from the code base.
|
5
|
-
# Note that changes in the inspected code, or installation of new
|
6
|
-
# versions of RuboCop, may require this file to be generated again.
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
-
|
5
|
-
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
-
|
7
|
-
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
-
|
9
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
-
|
11
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
-
|
13
|
-
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
|
data/CONTRIBUTING.md
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# Issue Guidelines
|
2
|
-
|
3
|
-
## Reporting bugs
|
4
|
-
|
5
|
-
If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
|
6
|
-
|
7
|
-
## Reporting feature requests
|
8
|
-
|
9
|
-
Report a feature request **only after discussing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
|
10
|
-
|
11
|
-
## Reporting questions, support requests, ideas, concerns etc.
|
12
|
-
|
13
|
-
**PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
|
14
|
-
|
15
|
-
# Pull Request Guidelines
|
16
|
-
|
17
|
-
A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
|
18
|
-
|
19
|
-
Other requirements:
|
20
|
-
|
21
|
-
1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
|
22
|
-
2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
|
23
|
-
3) Add API documentation if it's a new feature
|
24
|
-
4) Update API documentation if it changes an existing feature
|
25
|
-
5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
|
26
|
-
|
27
|
-
# Asking for help
|
28
|
-
|
29
|
-
If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org) or join [our chat](https://dry-rb.zulipchat.com).
|
data/Gemfile
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in dry-auto_inject.gemspec
|
6
|
-
gemspec
|
7
|
-
|
8
|
-
group :test do
|
9
|
-
gem "simplecov"
|
10
|
-
gem "codeclimate-test-reporter", require: nil
|
11
|
-
end
|
12
|
-
|
13
|
-
group :tools do
|
14
|
-
gem 'byebug', platforms: :mri
|
15
|
-
gem 'pry'
|
16
|
-
gem "ossy", git: "https://github.com/solnic/ossy.git", branch: "master"
|
17
|
-
end
|
data/Rakefile
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/gem_tasks'
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
7
|
-
|
8
|
-
require 'rspec/core'
|
9
|
-
require 'rspec/core/rake_task'
|
10
|
-
|
11
|
-
task default: :spec
|
12
|
-
|
13
|
-
desc 'Run all specs in spec directory'
|
14
|
-
RSpec::Core::RakeTask.new(:spec)
|
data/bin/console
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'dry-auto_inject'
|
6
|
-
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
9
|
-
|
10
|
-
require 'pry'
|
11
|
-
Pry.start
|
data/bin/setup
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Basic usage
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-auto_inject
|
5
|
-
---
|
6
|
-
|
7
|
-
### Requirements
|
8
|
-
|
9
|
-
You need only one thing before you can use dry-auto\_inject: a container to hold your application’s dependencies. These are commonly known as “inversion of control” containers.
|
10
|
-
|
11
|
-
A [dry-container](/gems/dry-container) will work well, but the only requirement is that the container responds to the `#[]` interface. For example, `my_container["users_repository"]` should return the “users_repository” object registered with the container.
|
12
|
-
|
13
|
-
### Creating an injector
|
14
|
-
|
15
|
-
To create an injector, pass the container to `Dry::AutoInject`:
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
Import = Dry::AutoInject(my_container)
|
19
|
-
```
|
20
|
-
|
21
|
-
Assign the injector to a constant (or make it globally accessible somehow) so you can refer to it from within your classes.
|
22
|
-
|
23
|
-
### Specifying dependencies
|
24
|
-
|
25
|
-
To specify the dependencies for a class, mix in the injector and provide the container identifiers for each dependency:
|
26
|
-
|
27
|
-
```ruby
|
28
|
-
class MyClass
|
29
|
-
include Import["users_repository", "deliver_welcome_email"]
|
30
|
-
end
|
31
|
-
```
|
32
|
-
|
33
|
-
### Using dependencies
|
34
|
-
|
35
|
-
Each dependency is available via a reader with a matching name:
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
class MyClass
|
39
|
-
include Import["users_repository"]
|
40
|
-
|
41
|
-
def call
|
42
|
-
puts users_repository.inspect
|
43
|
-
end
|
44
|
-
end
|
45
|
-
```
|
46
|
-
|
47
|
-
If your container identifiers include delimiters (like `"."`) or other characters that are not allowed within variable or method names, then the final part of the name will be used instead:
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
class MyClass
|
51
|
-
include Import["repositories.users"]
|
52
|
-
|
53
|
-
def call
|
54
|
-
puts users.inspect
|
55
|
-
end
|
56
|
-
end
|
57
|
-
```
|
58
|
-
|
59
|
-
### Specifying aliases for dependencies
|
60
|
-
|
61
|
-
You can specify dependencies as a hash to provide your own names for each one:
|
62
|
-
|
63
|
-
```ruby
|
64
|
-
class MyClass
|
65
|
-
include Import[users_repo: "repositories.users"]
|
66
|
-
|
67
|
-
def call
|
68
|
-
puts users_repo.inspect
|
69
|
-
end
|
70
|
-
end
|
71
|
-
```
|
72
|
-
|
73
|
-
If you want to provide a mix of inferred names and aliases, provide the aliases last:
|
74
|
-
|
75
|
-
```ruby
|
76
|
-
class MyClass
|
77
|
-
include Import[
|
78
|
-
"repositories.users",
|
79
|
-
deliver_email: "operations.deliver_welcome_email",
|
80
|
-
]
|
81
|
-
end
|
82
|
-
```
|
83
|
-
|
84
|
-
### Initializing your object
|
85
|
-
|
86
|
-
Initialize your object without any arguments and all the dependencies will be resolved from the the container automatically:
|
87
|
-
|
88
|
-
```ruby
|
89
|
-
my_obj = MyClass.new
|
90
|
-
```
|
91
|
-
|
92
|
-
### Passing manual dependencies
|
93
|
-
|
94
|
-
To provide an alternative object for a dependency, pass it to the initializer with a keyword argument matching the dependency’s name:
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
class MyClass
|
98
|
-
include Import["repositories.users"]
|
99
|
-
end
|
100
|
-
|
101
|
-
my_obj = MyClass.new(users: different_repo)
|
102
|
-
```
|
103
|
-
|
104
|
-
This technique is useful when testing your class in isolation. You can pass in test doubles to verify your class’ behaviour under various different circumstances.
|
@@ -1,45 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: How does it work?
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-auto_inject
|
5
|
-
---
|
6
|
-
|
7
|
-
dry-auto\_inject enables _constructor dependency injection_ for your objects. It achieves this by defining two methods in the module that you include in your class.
|
8
|
-
|
9
|
-
First, it defines `.new`, which resolves your dependencies from the container, if you haven't otherwise provided them as explicit arguments. It then passes these dependencies as arguments onto `#initialize`, as per Ruby’s usual behaviour.
|
10
|
-
|
11
|
-
It also defines `#initialize`, which receives these dependencies as arguments and then assigns them to instance variables. These variables are made available via `attr_reader`s.
|
12
|
-
|
13
|
-
So when you specify dependencies like this:
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
Import = Dry::AutoInject(MyContainer)
|
17
|
-
|
18
|
-
class MyClass
|
19
|
-
include Import["users_repository"]
|
20
|
-
end
|
21
|
-
```
|
22
|
-
|
23
|
-
You’re building something like this (this isn’t a line-for-line copy of what is mixed into your class; it’s intended as a guide only):
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
class MyClass
|
27
|
-
attr_reader :users_repository
|
28
|
-
|
29
|
-
def self.new(**args)
|
30
|
-
deps = {
|
31
|
-
users_repository: args[:users_repository] || MyContainer["users_repository"]
|
32
|
-
}
|
33
|
-
|
34
|
-
super(**deps)
|
35
|
-
end
|
36
|
-
|
37
|
-
def initialize(users_repository: nil)
|
38
|
-
super()
|
39
|
-
|
40
|
-
@users_repository = users_repository
|
41
|
-
end
|
42
|
-
end
|
43
|
-
```
|
44
|
-
|
45
|
-
Since these methods are defined in the module that you include in your class, you can still override them in your class if you wish to provide custom behavior.
|
@@ -1,53 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Introduction
|
3
|
-
description: Container-agnostic dependency resolution mixin
|
4
|
-
layout: gem-single
|
5
|
-
order: 4
|
6
|
-
type: gem
|
7
|
-
name: dry-auto_inject
|
8
|
-
sections:
|
9
|
-
- basic-usage
|
10
|
-
- how-does-it-work
|
11
|
-
- injection-strategies
|
12
|
-
---
|
13
|
-
|
14
|
-
dry-auto\_inject provides low-impact dependency injection and resolution support for your classes.
|
15
|
-
|
16
|
-
It’s designed to work with a container that holds your application’s dependencies. It works well with [dry-container](/gems/dry-container), but supports any container that responds to the `#[]` interface.
|
17
|
-
|
18
|
-
### Usage example
|
19
|
-
|
20
|
-
```ruby
|
21
|
-
# Set up a container (using dry-container here)
|
22
|
-
class MyContainer
|
23
|
-
extend Dry::Container::Mixin
|
24
|
-
|
25
|
-
register "users_repository" do
|
26
|
-
UsersRepository.new
|
27
|
-
end
|
28
|
-
|
29
|
-
register "operations.create_user" do
|
30
|
-
CreateUser.new
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Set up your auto-injection mixin
|
35
|
-
Import = Dry::AutoInject(MyContainer)
|
36
|
-
|
37
|
-
class CreateUser
|
38
|
-
include Import["users_repository"]
|
39
|
-
|
40
|
-
def call(user_attrs)
|
41
|
-
users_repository.create(user_attrs)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
create_user = MyContainer["operations.create_user"]
|
46
|
-
create_user.call(name: "Jane")
|
47
|
-
```
|
48
|
-
|
49
|
-
### Why use dry-auto\_inject?
|
50
|
-
|
51
|
-
Splitting your application’s behavior into smaller, more focused units makes for logic that is easier to understand, test, and refactor. Dependency injection is what then allows you to combine these small units to create more sophisticated behavior.
|
52
|
-
|
53
|
-
By using a container and dry-auto\_inject, this process becomes easy. You don’t need to worry about building constructors or accessors, and adding extra dependencies is as easy as adding a string to a list.
|
@@ -1,94 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Injection strategies
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-auto_inject
|
5
|
-
---
|
6
|
-
|
7
|
-
dry-auto\_inject supports three _injection strategies_, allowing you to design and integrate with different kinds of classes.
|
8
|
-
|
9
|
-
These strategies all provide _constructor dependency injection_: dependencies are passed when creating your objects. The strategies differ in how they expect dependencies to be passed to the initializer.
|
10
|
-
|
11
|
-
## Choosing a strategy
|
12
|
-
|
13
|
-
Choose a strategy when you build the injector:
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
# Default keyword arguments strategy
|
17
|
-
Import = Dry::AutoInject(MyContainer)
|
18
|
-
|
19
|
-
# Positional arguments strategy
|
20
|
-
Import = Dry::AutoInject(MyContainer).args
|
21
|
-
```
|
22
|
-
|
23
|
-
Strategies can also be chained from existing injectors, which means you can set up a single injector for your most commonly used strategy, then use a different strategy directly in particular classes if they have differing requirements. For example:
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
# Set up a standard strategy for your app
|
27
|
-
Import = Dry::AutoInject(MyContainer)
|
28
|
-
|
29
|
-
class MyClass
|
30
|
-
# Use the standard strategy here
|
31
|
-
include Import["users_repository"]
|
32
|
-
end
|
33
|
-
|
34
|
-
class SpecialClass
|
35
|
-
# Use a different strategy in this particular class
|
36
|
-
include Import.args["users_repository"]
|
37
|
-
end
|
38
|
-
```
|
39
|
-
|
40
|
-
## Strategies
|
41
|
-
|
42
|
-
### Keyword arguments (`kwargs`)
|
43
|
-
|
44
|
-
This is the default strategy.
|
45
|
-
|
46
|
-
Pass dependencies to the initializer using keyword arguments.
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
Import = Dry::AutoInject(MyContainer)
|
50
|
-
|
51
|
-
class MyClass
|
52
|
-
include Import["users_repository"]
|
53
|
-
end
|
54
|
-
|
55
|
-
MyClass.new(users_repository: my_repo)
|
56
|
-
```
|
57
|
-
|
58
|
-
The `#initialize` method has two possible argument signatures:
|
59
|
-
|
60
|
-
- If there is no `super` method for `#initialize`, or the `super` method takes no arguments, then the keyword arguments will be explicit, e.g. `#initialize(users_repository: nil)`.
|
61
|
-
- If the `super` method for `#initialize` takes its own set of keyword arguments, then the arguments will be a single splat, e.g. `#initialize(**args)`.
|
62
|
-
|
63
|
-
### Options hash (`hash`)
|
64
|
-
|
65
|
-
Pass the dependencies to the initializer as a single hash.
|
66
|
-
|
67
|
-
```ruby
|
68
|
-
Import = Dry::AutoInject(MyContainer).hash
|
69
|
-
|
70
|
-
class MyClass
|
71
|
-
include Import["users_repository"]
|
72
|
-
end
|
73
|
-
|
74
|
-
# This can also take `{users_repo: my_repo}`
|
75
|
-
MyClass.new(users_repository: my_repo)
|
76
|
-
```
|
77
|
-
|
78
|
-
The `#initialize` method has an argument signature of `#initialize(options)`, where `options` is expected to be a hash.
|
79
|
-
|
80
|
-
### Positional arguments (`args`)
|
81
|
-
|
82
|
-
Pass dependencies to the initializer using standard positional arguments.
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
Import = Dry::AutoInject(MyContainer).args
|
86
|
-
|
87
|
-
class MyClass
|
88
|
-
include Import["users_repository"]
|
89
|
-
end
|
90
|
-
|
91
|
-
MyClass.new(my_repo)
|
92
|
-
```
|
93
|
-
|
94
|
-
The `#initialize` method has an argument signature with a named positional argument for each dependency, e.g. `#initialize(users_repository)`.
|
data/rakelib/rubocop.rake
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'rubocop/rake_task'
|
5
|
-
|
6
|
-
Rake::Task[:default].enhance [:rubocop]
|
7
|
-
|
8
|
-
RuboCop::RakeTask.new do |task|
|
9
|
-
task.options << '--display-cop-names'
|
10
|
-
end
|
11
|
-
|
12
|
-
namespace :rubocop do
|
13
|
-
desc 'Generate a configuration file acting as a TODO list.'
|
14
|
-
task :auto_gen_config do
|
15
|
-
exec 'bundle exec rubocop --auto-gen-config'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
rescue LoadError
|
20
|
-
end
|