plumbum 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +18 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE +22 -0
- data/README.md +163 -0
- data/lib/plumbum/consumer.rb +148 -0
- data/lib/plumbum/consumers/class_methods.rb +319 -0
- data/lib/plumbum/consumers/instance_methods.rb +105 -0
- data/lib/plumbum/consumers/scoped_consumer.rb +25 -0
- data/lib/plumbum/consumers.rb +12 -0
- data/lib/plumbum/errors/immutable_error.rb +8 -0
- data/lib/plumbum/errors/invalid_dependency_error.rb +8 -0
- data/lib/plumbum/errors/invalid_key_error.rb +8 -0
- data/lib/plumbum/errors/missing_dependency_error.rb +8 -0
- data/lib/plumbum/errors.rb +13 -0
- data/lib/plumbum/many_provider.rb +86 -0
- data/lib/plumbum/one_provider.rb +56 -0
- data/lib/plumbum/parameters.rb +39 -0
- data/lib/plumbum/provider.rb +148 -0
- data/lib/plumbum/providers/lazy.rb +26 -0
- data/lib/plumbum/providers/plural.rb +31 -0
- data/lib/plumbum/providers/singular.rb +26 -0
- data/lib/plumbum/providers.rb +12 -0
- data/lib/plumbum/rspec/deferred/consumer_examples.rb +2122 -0
- data/lib/plumbum/rspec/deferred/provider_examples.rb +848 -0
- data/lib/plumbum/rspec/deferred.rb +8 -0
- data/lib/plumbum/rspec/stub_provider.rb +75 -0
- data/lib/plumbum/rspec.rb +8 -0
- data/lib/plumbum/version.rb +57 -0
- data/lib/plumbum.rb +25 -0
- metadata +91 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c3ca814e726f5c7d8c17d831ef41acabf3f0d6d823f14856c11be28d62230e39
|
|
4
|
+
data.tar.gz: ddf9b0cd50e93c5fa1d8e0336d8635a66369e7b2156598865b1787d1d231b2ca
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cd6ba482abe9f0f59461daabc73ca244bf343ec4c76b390304e173469c81577757b599a6d67967377b57f376138e4b1697179392cfcad498c026611ca8db9b08
|
|
7
|
+
data.tar.gz: 95682a1f51ffc64f707deef2708fd50e523c3df947cc17408be6822d35863e9fcffae5e32191016e776cf31f6b0b6654ce2ff543c4966e73662284bc38088ae3
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
# 0.1.0
|
|
4
|
+
|
|
5
|
+
Initial development version.
|
|
6
|
+
|
|
7
|
+
## Consumers
|
|
8
|
+
|
|
9
|
+
Implemented `Plumbum::Consumer` to manage providers and dependencies.
|
|
10
|
+
|
|
11
|
+
- Implemented `Plumbum::Parameters` to receive dependencies as constructor parameters.
|
|
12
|
+
|
|
13
|
+
## Providers
|
|
14
|
+
|
|
15
|
+
Implemented `Plumbum::Provider` to define dependencies.
|
|
16
|
+
|
|
17
|
+
- Implemented `Plumbum::ManyProvider`.
|
|
18
|
+
- Implemented `Plumbum::OneProvider`.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
merlin@sleepingkingstudios.com.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
|
|
126
|
+
at [https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rob Smith
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Plumbum
|
|
2
|
+
|
|
3
|
+
Plumbum is a minimal dependency injection framework for Ruby, using vanilla Ruby semantics to define and reference dependencies from different providers.
|
|
4
|
+
|
|
5
|
+
<blockquote>
|
|
6
|
+
Read The
|
|
7
|
+
<a href="https://www.sleepingkingstudios.com/plumbum" target="_blank">
|
|
8
|
+
Documentation
|
|
9
|
+
</a>
|
|
10
|
+
</blockquote>
|
|
11
|
+
|
|
12
|
+
Plumbum defines an interface for declaring, providing, and consuming object dependencies. It defines the following concepts:
|
|
13
|
+
|
|
14
|
+
- Dependencies - A value or object used in some part of your application, such as a data repository, a configuration hash, or an environment name.
|
|
15
|
+
- [Providers](http://sleepingkingstudios.com/plumbum/providers) - A `Provider` makes one or more dependencies available to other parts of the application.
|
|
16
|
+
- [Consumers](http://sleepingkingstudios.com/plumbum/consumers) - A `Consumer` declares what dependencies it relies on and what providers it consumes. Plumbum handles resolving those dependencies.
|
|
17
|
+
|
|
18
|
+
As an example, consider a data repository. From a query object's perspective, there should be one source of truth as to where the data is read and persisted - but where? In a global singleton? A repository object manually passed to the query? What if we want to use different data sources in the test environment?
|
|
19
|
+
|
|
20
|
+
Plumbum solves this problem by decoupling the *source* of the dependency from how the dependency is *used*. We start by defining a provider - in this case, let's call it a `RepositoryProvider`. We can initialize the provider with a different *value* in different environments - for example, using static fixtures in the test environment. Inside our query class, then, we declare a *dependency* on a `#repository` value. We also declare a *provider* relationship with our defined `RepositoryProvider`. And that's it - when we call `query.repository`, Plumbum resolves that dependency to the provider. Want to inject a different dependency value in a specific test? Include `Plumbum::Parameters` and you can provide dependencies by passing them in as constructor parameters.
|
|
21
|
+
|
|
22
|
+
## Why Plumbum?
|
|
23
|
+
|
|
24
|
+
Plumbum was designed to solve a specific problem: decoupling the *definition* from the *use* of dependent objects and functions. You should use Plumbum when:
|
|
25
|
+
|
|
26
|
+
- You have dependencies that are used across your application or far from where they are nominally defined. For example, a data repository object could be used across the application - in commands, in queries, in reports, and so on.
|
|
27
|
+
- The *values* of your dependencies can change based on scope or context. For example, an application might use different configuration values in the development, test, and production environments, or you want to use a global logger in most contexts but pass in a mock logger instance in unit tests.
|
|
28
|
+
|
|
29
|
+
In addition to managing dependencies like configuration and repositories, one critical use case I've found for Plumbum in my own projects is defining a facade service over functionality like reading/writing to the standard IO streams or performing file system operations. Doing so allows me to define and inject mock services that make unit testing both easier and more safe to run.
|
|
30
|
+
|
|
31
|
+
## About
|
|
32
|
+
|
|
33
|
+
Plumbum is tested against MRI Ruby 3.2 through 4.0.
|
|
34
|
+
|
|
35
|
+
### Documentation
|
|
36
|
+
|
|
37
|
+
Documentation is generated using [YARD](https://yardoc.org/), and can be generated locally using the `yard` gem.
|
|
38
|
+
|
|
39
|
+
### License
|
|
40
|
+
|
|
41
|
+
Plumbum is released under the [MIT License](https://opensource.org/licenses/MIT).
|
|
42
|
+
|
|
43
|
+
### Contribute
|
|
44
|
+
|
|
45
|
+
The canonical repository for this gem is on [GitHub](https://github.com/sleepingkingstudios/plumbum). Community contributions are welcome - please feel free to fork or submit issues, bug reports or pull requests.
|
|
46
|
+
|
|
47
|
+
### Code of Conduct
|
|
48
|
+
|
|
49
|
+
Please note that the `Plumbum` project is released with a [Contributor Code of Conduct](https://github.com/sleepingkingstudios/plumbum/blob/master/CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms.
|
|
50
|
+
|
|
51
|
+
## Getting Started
|
|
52
|
+
|
|
53
|
+
Add the gem to your `Gemfile` or `gemspec`:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
gem 'plumbum'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Require `Plumbum` in your code:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
require 'plumbum'
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Identify Dependencies
|
|
66
|
+
|
|
67
|
+
Before writing any code to manage dependencies, stop and consider what your dependencies are. Our example is a library application with entities such as Author, Book, Patron, and Loan, and we want to manage our application's data persistence layer. Each entity is represented by a `collection` that wraps query and update methods for that entity, and we define a `repository` object that returns our collections.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
repository = SqlRepository.new(database_url:)
|
|
71
|
+
collection = repository.books
|
|
72
|
+
|
|
73
|
+
collection.where(author: 'Ursula K. LeGuin')
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Lots of places in the code will need to reference the current repository - in particular, any functions that query the existing data or insert or update entities will need the repository, so passing in the value directly will quickly become unwieldy.
|
|
77
|
+
|
|
78
|
+
We could define a singleton, such as a `Repository.current` class method. But what if we need the *value* of the current repository to depend on context? Here are a few reasons we might want to do so:
|
|
79
|
+
|
|
80
|
+
- High-priority requests in production use reserved database connections.
|
|
81
|
+
- The repository in the test environment uses a different data store - Redis-backed instead of a SQL database.
|
|
82
|
+
- We define a test repository with specific fixtures for running feature tests against the UI.
|
|
83
|
+
|
|
84
|
+
### Define Providers
|
|
85
|
+
|
|
86
|
+
The solution Plumbum defines is a `Provider`, a special type of object that "provides" or returns a dependency when requested. The documentation for providers is <a href="https://www.sleepingkingstudios.com/plumbum/providers" target="_blank">available online</a>.
|
|
87
|
+
|
|
88
|
+
Each provider implements a simple interface:
|
|
89
|
+
|
|
90
|
+
- `provider.has?(name)` checks if the provider defines the named dependency.
|
|
91
|
+
- `provider.get(name)` returns the requested dependency.
|
|
92
|
+
|
|
93
|
+
`Plumbum` has pre-defined providers for wrapping single and plural dependencies. In our case, we want to define a provider for just one dependency, so we'll use the `Plumbum::OneProvider` class. Further, we want to make sure that once the `repository` is set, it can't be changed to a different value, so we use the `write_once:` flag.
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
require 'plumbum'
|
|
97
|
+
|
|
98
|
+
module Library
|
|
99
|
+
REPOSITORY_PROVIDER = Plumbum::OneProvider.new(:repository, write_once: true)
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Notice that we are *not* initializing the repository with a value. Instead, we will do so lazily - in an initializer when running the server, or in the test setup when running unit tests.
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
Library::REPOSITORY_PROVIDER.value = SqlRepository.new(database_url:)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
This allows us to define a different value in each environment, and to defer setting the value until later in the startup process (for example, once we have loaded configuration values). However, since the `write_once:` flag is set, we ensure that the value can't be changed at a later point.
|
|
110
|
+
|
|
111
|
+
### Define Consumers
|
|
112
|
+
|
|
113
|
+
So, we've defined our `Provider`, allowing us to control how and when our `repository` value is defined and to access the value from anywhere in our application. That solves our immediate problem, but calling `repository = Library::REPOSITORY_PROVIDER.get(:repository)` everywhere isn't very elegant or efficient.
|
|
114
|
+
|
|
115
|
+
Thus, Plumbum defines the `Consumer` module which you can `include` in your class to manage providers and dependencies. The documentation for consumers is <a href="https://www.sleepingkingstudios.com/plumbum/consumers" target="_blank">available online</a>.
|
|
116
|
+
|
|
117
|
+
Let's define a query class that finds books with the requested author, using Plumbum to manage our dependencies.
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
module Library
|
|
121
|
+
module Queries
|
|
122
|
+
class FindBooksByAuthor
|
|
123
|
+
include Plumbum::Consumer
|
|
124
|
+
|
|
125
|
+
provider Library::REPOSITORY_PROVIDER
|
|
126
|
+
|
|
127
|
+
dependency :repository
|
|
128
|
+
|
|
129
|
+
def call(author_name, **filters)
|
|
130
|
+
author = repository.authors.find(name: author)
|
|
131
|
+
books = repository.books.where(**filters, author_id: author.id)
|
|
132
|
+
|
|
133
|
+
{
|
|
134
|
+
author:,
|
|
135
|
+
books:
|
|
136
|
+
}
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
In our query class, we first include `Plumbum::Consumer` to define the DSL methods. We then declare a `provider` - in our case, the `Library::REPOSITORY_PROVIDER`, which makes a `repository` dependency available. Finally, we declare that the query has a `dependency :repository`. Doing so defines the `#repository` method, which searches the providers defined for the class until it finds one that defines a `:repository` dependency.
|
|
144
|
+
|
|
145
|
+
A more complex example might define multiple providers and dependencies. The `dependency` method in particular provides a number of advanced features:
|
|
146
|
+
|
|
147
|
+
- Dependency aliases: `dependency :application, as: :app`.
|
|
148
|
+
- Skip memoization: `dependency :request, memoize: false`.
|
|
149
|
+
- Scoped dependencies: `dependency 'config.network.default_server`.
|
|
150
|
+
- Method dependencies: `dependency '#restart', scope: :process_manager`.
|
|
151
|
+
|
|
152
|
+
Let's solve a specific problem for our query class - testing. We can already declare a different `repository` value in our test environment by assigning it to the provider in our test setup. But what if we want to pass in a `repository` value directly? That would allow us to write tests with a repository scoped to the individual test and with predefined seed data.
|
|
153
|
+
|
|
154
|
+
Plumbum provides a module to enable this specific functionality. Include `Plumbum::Parameters` in your class and you can pass in dependency values to the constructor.
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
Library::Queries::FindBooksByAuthor.include(Plumbum::Parameters)
|
|
158
|
+
|
|
159
|
+
repository = MockRepository.new(authors: [], books: [])
|
|
160
|
+
query = Library::Queries::FindBooksByAuthor.new(repository:)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
For a full list of features, see the <a href="https://www.sleepingkingstudios.com/plumbum" target="_blank">Documentation</a>.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'sleeping_king_studios/tools'
|
|
4
|
+
|
|
5
|
+
require 'plumbum'
|
|
6
|
+
require 'plumbum/consumers/class_methods'
|
|
7
|
+
require 'plumbum/consumers/instance_methods'
|
|
8
|
+
|
|
9
|
+
module Plumbum
|
|
10
|
+
# Provides methods for defining and accessing injected dependencies.
|
|
11
|
+
#
|
|
12
|
+
# @example Define a basic dependency.
|
|
13
|
+
# class Orchestrator
|
|
14
|
+
# include Plumbum::Consumer
|
|
15
|
+
# include ApplicationProvider
|
|
16
|
+
#
|
|
17
|
+
# dependency :application
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# Orchestrator.new.application
|
|
21
|
+
# #=> returns the value of ApplicationProvider.value
|
|
22
|
+
#
|
|
23
|
+
# @example Define an aliased dependency.
|
|
24
|
+
# class Orchestrator
|
|
25
|
+
# include Plumbum::Consumer
|
|
26
|
+
# include ApplicationProvider
|
|
27
|
+
#
|
|
28
|
+
# dependency :application, as: :app
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# Orchestrator.new.app
|
|
32
|
+
# #=> returns the value of ApplicationProvider.value
|
|
33
|
+
#
|
|
34
|
+
# @example Define an optional dependency.
|
|
35
|
+
# class BillCustomer
|
|
36
|
+
# include Plumbum::Consumer
|
|
37
|
+
#
|
|
38
|
+
# dependency :rewards
|
|
39
|
+
# end
|
|
40
|
+
#
|
|
41
|
+
# class BillRewardsCustomer < BillCustomer
|
|
42
|
+
# include RewardsProvider
|
|
43
|
+
# end
|
|
44
|
+
#
|
|
45
|
+
# BillCustomer.new.rewards
|
|
46
|
+
# #=> returns nil
|
|
47
|
+
#
|
|
48
|
+
# BillRewardsCustomer.new.rewards
|
|
49
|
+
# #=> returns RewardsProvider.value
|
|
50
|
+
#
|
|
51
|
+
# @example Define an unmemoized dependency.
|
|
52
|
+
# class Action
|
|
53
|
+
# include Plumbum::Consumer
|
|
54
|
+
# include RequestProvider
|
|
55
|
+
#
|
|
56
|
+
# dependency :request, memoize: false
|
|
57
|
+
# end
|
|
58
|
+
#
|
|
59
|
+
# action = Action.new
|
|
60
|
+
# action.request
|
|
61
|
+
# #=> returns the value of RequestProvider.value
|
|
62
|
+
#
|
|
63
|
+
# request = Request.new
|
|
64
|
+
# RequestProvider.value = request
|
|
65
|
+
# action.request
|
|
66
|
+
# #=> returns the new request
|
|
67
|
+
#
|
|
68
|
+
# @example Define a dependency with scoped key.
|
|
69
|
+
# class Client
|
|
70
|
+
# include Plumbum::Consumer
|
|
71
|
+
# include ConfigurationProvider
|
|
72
|
+
#
|
|
73
|
+
# dependency 'config.network.default_server'
|
|
74
|
+
# end
|
|
75
|
+
#
|
|
76
|
+
# client = Client.new
|
|
77
|
+
# client.default_server
|
|
78
|
+
# #=> returns the value of ConfigurationProvider.values[:config][:network][:default_server]
|
|
79
|
+
#
|
|
80
|
+
# @example Define multiple dependencies.
|
|
81
|
+
# class Rocket
|
|
82
|
+
# include Plumbum::Consumer
|
|
83
|
+
# include RocketPartsProvider
|
|
84
|
+
#
|
|
85
|
+
# dependency :engine, :fusilage, :payload
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# rocket = Rocket.new
|
|
89
|
+
# rocket.engine
|
|
90
|
+
# #=> returns the value of RocketPartsProvider.values[:engine]
|
|
91
|
+
#
|
|
92
|
+
# @example Define multiple dependencies with scoped keys.
|
|
93
|
+
# class Server
|
|
94
|
+
# include Plumbum::Consumer
|
|
95
|
+
# include ConfigurationProvider
|
|
96
|
+
#
|
|
97
|
+
# dependency :port, :protocol, :timeout, scope: 'config.network'
|
|
98
|
+
# end
|
|
99
|
+
#
|
|
100
|
+
# server = Server.new
|
|
101
|
+
# server.port
|
|
102
|
+
# #=> returns the value of ConfigurationProvider.values[:config][:network][:port]
|
|
103
|
+
#
|
|
104
|
+
# @example Define a method depndency.
|
|
105
|
+
# class Application
|
|
106
|
+
# include Plumbum::Consumer
|
|
107
|
+
# include ProcessManagerProvider
|
|
108
|
+
#
|
|
109
|
+
# dependency '#restart', scope: :process_manager
|
|
110
|
+
# end
|
|
111
|
+
#
|
|
112
|
+
# application = Application.new
|
|
113
|
+
# application.restart(force: true)
|
|
114
|
+
# #=> calls ProcessManagerProvider.values[:process_manager].restart(force: true)
|
|
115
|
+
module Consumer
|
|
116
|
+
include Plumbum::Consumers::InstanceMethods
|
|
117
|
+
|
|
118
|
+
# Class methods to extend when including Plumbum::Consumer.
|
|
119
|
+
module ClassMethods
|
|
120
|
+
include Plumbum::Consumers::ClassMethods
|
|
121
|
+
|
|
122
|
+
alias dependency plumbum_dependency
|
|
123
|
+
|
|
124
|
+
alias dependency_keys plumbum_dependency_keys
|
|
125
|
+
|
|
126
|
+
alias provider plumbum_provider
|
|
127
|
+
|
|
128
|
+
# Callback invoked when Consumer is included in another module or class.
|
|
129
|
+
#
|
|
130
|
+
# This ensures that the Consumer methods propagate correctly across a
|
|
131
|
+
# chain of included modules.
|
|
132
|
+
def included(other)
|
|
133
|
+
super
|
|
134
|
+
|
|
135
|
+
other.extend(ClassMethods)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
class << self
|
|
140
|
+
# Callback invoked when Consumer is included in another module or class.
|
|
141
|
+
def included(other)
|
|
142
|
+
super
|
|
143
|
+
|
|
144
|
+
other.extend(ClassMethods)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|