uses 1.0.0.pre.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +35 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +5 -0
- data/Gemfile +6 -0
- data/LICENSE.md +33 -0
- data/README.md +368 -0
- data/Rakefile +2 -0
- data/bin/ci +10 -0
- data/bin/console +14 -0
- data/bin/rspec +29 -0
- data/bin/setup +6 -0
- data/lib/uses/circular_dependency/analyzer.rb +60 -0
- data/lib/uses/circular_dependency/base_notifier.rb +18 -0
- data/lib/uses/circular_dependency/error.rb +7 -0
- data/lib/uses/circular_dependency/ignore_notifier.rb +10 -0
- data/lib/uses/circular_dependency/log_notifier.rb +10 -0
- data/lib/uses/circular_dependency/raise_error_notifier.rb +11 -0
- data/lib/uses/config.rb +38 -0
- data/lib/uses/error.rb +4 -0
- data/lib/uses/initializer/base_initializer.rb +19 -0
- data/lib/uses/initializer/from_initializers.rb +9 -0
- data/lib/uses/initializer/new_no_args.rb +11 -0
- data/lib/uses/initializer/proc_based.rb +7 -0
- data/lib/uses/initializer.rb +35 -0
- data/lib/uses/inject_double.rb +76 -0
- data/lib/uses/invalid_method_name.rb +5 -0
- data/lib/uses/method.rb +87 -0
- data/lib/uses/method_name.rb +23 -0
- data/lib/uses/uses_method_args.rb +24 -0
- data/lib/uses/version.rb +3 -0
- data/lib/uses.rb +49 -0
- data/uses.gemspec +29 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 88067fa2445e42cb0c82777e973ea4f99e8875d5aa9b5e57f2cf6e5c41bf7fdd
|
4
|
+
data.tar.gz: 8c81ad281c316cc68f09f9d6f24fc4df3b7f9871fc0db567326a3bafa3d3efc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3aa03891e384844990ffeb3ae2e1d1763bcd0ed4debfe169427d07814ea88339cbd9dbaa8aa6c9690de13a1c2796fb4d7a1063b47c289cc552e6e376b3ef0f90
|
7
|
+
data.tar.gz: 613162f4b7710cb491bffccdd5eef58a0895c5e96e36e8fe4132c4454e10a96cbfc6f78144563f2ea449e23c1a9952cf9cb0b3fc34470b7ac12027dd6d90d07c
|
@@ -0,0 +1,35 @@
|
|
1
|
+
version: 2.1
|
2
|
+
orbs:
|
3
|
+
# See https://circleci.com/developer/orbs/orb/circleci/ruby
|
4
|
+
ruby: circleci/ruby@1.2.0
|
5
|
+
jobs: # keyword
|
6
|
+
test: # my name for the job
|
7
|
+
parameters: # keyword
|
8
|
+
ruby-version: # my parameter name
|
9
|
+
type: string # type is a keyword
|
10
|
+
docker: # keyword
|
11
|
+
- image: cimg/base:stable
|
12
|
+
steps: # keyword
|
13
|
+
- checkout # magic name
|
14
|
+
- ruby/install: # ruby/ is from the orb name, install is a command in that orb
|
15
|
+
version: << parameters.ruby-version >> # magic nonsense for param subst
|
16
|
+
- run:
|
17
|
+
command: "bin/setup"
|
18
|
+
- run:
|
19
|
+
name: "Create the test results directory because you can't just store_test_results with a file and if you do you do not get any sort of error because wtf is with this platform?"
|
20
|
+
command: mkdir -p /tmp/test-results
|
21
|
+
- run:
|
22
|
+
command: bin/ci /tmp/test-results/rspec_results.xml
|
23
|
+
- store_test_results: # store_test_results is magic from circle
|
24
|
+
path: /tmp/test-results # path is a param to store_test_results and it must be a directory not a file
|
25
|
+
- store_artifacts: # store_artifacts is magic from circle
|
26
|
+
path: /tmp/test-results # path is the param to store_artifacts
|
27
|
+
workflows: # keyword
|
28
|
+
all-rubies: # my name for the workflow
|
29
|
+
jobs: # keyword
|
30
|
+
- test: # my name for the job
|
31
|
+
matrix: # keyword
|
32
|
+
parameters: # keyword
|
33
|
+
# All rubies being maintained per this page:
|
34
|
+
# https://www.ruby-lang.org/en/downloads/branches/
|
35
|
+
ruby-version: [ "2.5", "2.6", "2.7", "3.0" ]
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
9
|
+
level of experience, education, socio-economic status, nationality, personal
|
10
|
+
appearance, race, religion, or sexual identity and orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at davetron5000 (at) gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
72
|
+
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
74
|
+
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
* Code changes for style, cleanliness, eleganance, or other aesthetic stuff will not be accepted.
|
4
|
+
* All proposed changes must have a clear problem statement and be based on a real-world scenario. Imagined problems will not be solved.
|
5
|
+
* Tests are appreciated with all pull requests.
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
[uses] Copyright (2021) (David Copeland)(“Licensor”)
|
2
|
+
|
3
|
+
Hippocratic License Version Number: 2.1.
|
4
|
+
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
|
6
|
+
|
7
|
+
Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
|
8
|
+
|
9
|
+
* Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
|
10
|
+
|
11
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
|
12
|
+
|
13
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
14
|
+
|
15
|
+
1. Human Rights Principles.
|
16
|
+
|
17
|
+
(a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
|
18
|
+
|
19
|
+
(b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
|
20
|
+
|
21
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
|
22
|
+
|
23
|
+
2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
|
24
|
+
|
25
|
+
3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
|
26
|
+
|
27
|
+
* Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
|
28
|
+
|
29
|
+
* Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
|
30
|
+
|
31
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
32
|
+
|
33
|
+
This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
|
data/README.md
ADDED
@@ -0,0 +1,368 @@
|
|
1
|
+
# uses - declare dependencies between classes
|
2
|
+
|
3
|
+
[![<sustainable-rails>](https://circleci.com/gh/sustainable-rails/uses.svg?style=shield)](https://app.circleci.com/pipelines/github/sustainable-rails/uses)
|
4
|
+
|
5
|
+
Your *service layer* is a web of classes that depend on each other to get the job done. While you can manage that directly with code and test them
|
6
|
+
directly using mocks or integration tests, declaring dependencies directly can reduce errors, increase consistency, and make your code a bit
|
7
|
+
simpler.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
Add to your `Gemfile`
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "uses"
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Suppose you have a `PaymentsController` that uses a class called `PaymentProcessor`. Suppose that `PaymentProcessor` uses `Braintree::Gateway`
|
20
|
+
under the covers:
|
21
|
+
|
22
|
+
```
|
23
|
+
+--------------------+
|
24
|
+
| |
|
25
|
+
| PaymentsController |
|
26
|
+
| |
|
27
|
+
+---+----------------+
|
28
|
+
|
|
29
|
+
| +------------------+
|
30
|
+
\-«uses»--->| |
|
31
|
+
| PaymentProcessor |
|
32
|
+
| |
|
33
|
+
+------------------+
|
34
|
+
|
|
35
|
+
| +--------------------+
|
36
|
+
\-«uses»--->| |
|
37
|
+
| Braintree::Gateway |
|
38
|
+
| |
|
39
|
+
+--------------------+
|
40
|
+
```
|
41
|
+
|
42
|
+
You might implement this like so:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class PaymentsController < ApplicationController
|
46
|
+
def create
|
47
|
+
payment_processor.collect_payment(...)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def payment_processor
|
53
|
+
@payment_processor ||= PaymentProcessor.new
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class PaymentProcessor
|
58
|
+
def collect_payment
|
59
|
+
braintree_gateway.sale(...)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def braintree_gateway
|
65
|
+
@braintree_gateway ||= Braintree::Gateway.new
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
This is fine, but with Uses you can declare these dependencies explicitly:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class PaymentsController < ApplicationController
|
74
|
+
uses PaymentProcessor # <--------
|
75
|
+
|
76
|
+
def create
|
77
|
+
payment_processor.collect_payment(...)
|
78
|
+
# ^
|
79
|
+
# |
|
80
|
+
# |
|
81
|
+
# Method dynamically defined when `uses` is called
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class PaymentProcessor
|
87
|
+
uses Braintree::Gateway # <--------
|
88
|
+
|
89
|
+
def collect_payment
|
90
|
+
braintree_gateway.sale(...)
|
91
|
+
# ^
|
92
|
+
# |
|
93
|
+
# |
|
94
|
+
# Method dynamically defined when `uses` is called
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
## Why Would You Want This?
|
100
|
+
|
101
|
+
By declaring dependencies like this, you:
|
102
|
+
|
103
|
+
* save some code, which adds up as your application grows.
|
104
|
+
* can detect circular dependencies, which is often a sign of trouble or confusion.
|
105
|
+
* can ensure that if you mock a dependency, you class really does depend on it.
|
106
|
+
* make dependants clear at the top of the class, but without adopting a complicated dependency-injection pattern.
|
107
|
+
|
108
|
+
## Set Up
|
109
|
+
|
110
|
+
Strictly speaking, to access `uses`, you should include `Uses::Method` into any class that needs it. Practically, this is what you should
|
111
|
+
do:
|
112
|
+
|
113
|
+
* Add it to `ApplicationController`:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
class ApplicationController < ActionController::Base
|
117
|
+
include Uses::Method
|
118
|
+
|
119
|
+
# whatever else is in your ApplicationController
|
120
|
+
end
|
121
|
+
```
|
122
|
+
* Create `app/sevices/application_service.rb` like so:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class ApplicationService
|
126
|
+
include Uses::Method
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
and have your service layer classes inherit from this
|
131
|
+
* Add `Uses::Method` to any other base class where your service layer logic is initiated.
|
132
|
+
|
133
|
+
## Testing Support
|
134
|
+
|
135
|
+
You might test `PaymentProcessor` by mocking `Braintree::Gateway`, like so:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
RSpec.describe PaymentProcessor do
|
139
|
+
subject(:payment_processor) { described_class.new }
|
140
|
+
|
141
|
+
let(:braintree_gateway) { instance_double(Braintree::Gateway) }
|
142
|
+
|
143
|
+
before do
|
144
|
+
allow(Braintree::Gateway).to receive(:new).and_return(braintree_gateway)
|
145
|
+
allow(braintree_gateway).to receive(:sale)
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "#collect_payment" do
|
149
|
+
it "calls braintree" do
|
150
|
+
payment_processor.collect_payment
|
151
|
+
|
152
|
+
expect(braintree_gateway).to have_received(:sale)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
When building out your service layer, having to repeatedly mock the call to `new` can be tedious. If your class changes to need the class you are
|
159
|
+
mocking, you don't have a good way to know that. Instead, `inject_rspec_double` can solve both issues:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
RSpec.describe PaymentProcessor do
|
163
|
+
subject(:payment_processor) { described_class.new }
|
164
|
+
|
165
|
+
# vvvvvv
|
166
|
+
let(:braintree_gateway) { inject_rspec_double(payment_processor,Braintree::Gateway) }
|
167
|
+
# ^^^^^^
|
168
|
+
|
169
|
+
before do
|
170
|
+
# No need to mock :new
|
171
|
+
allow(braintree_gateway).to receive(:sale)
|
172
|
+
end
|
173
|
+
|
174
|
+
describe "#collect_payment" do
|
175
|
+
it "calls braintree" do
|
176
|
+
payment_processor.collect_payment
|
177
|
+
|
178
|
+
expect(braintree_gateway).to have_received(:sale)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
```
|
183
|
+
|
184
|
+
This saves a few lines of code (which, again, will add up over time), but will also fail is `PaymentProcessor` does not depend on
|
185
|
+
`Braintree::Gateway` via `uses`.
|
186
|
+
|
187
|
+
If you aren't using RSpec or want to control how the mock gets created, you can also use `inject_double`:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
RSpec.describe PaymentProcessor do
|
191
|
+
subject(:payment_processor) { described_class.new }
|
192
|
+
|
193
|
+
# vvvvvv
|
194
|
+
let(:braintree_gateway) { inject_double(payment_processor,Braintree::Gateway: double("BT Gateway")) }
|
195
|
+
# ^^^^^^
|
196
|
+
|
197
|
+
before do
|
198
|
+
# No need to mock :new
|
199
|
+
allow(braintree_gateway).to receive(:sale)
|
200
|
+
end
|
201
|
+
|
202
|
+
describe "#collect_payment" do
|
203
|
+
it "calls braintree" do
|
204
|
+
payment_processor.collect_payment
|
205
|
+
|
206
|
+
expect(braintree_gateway).to have_received(:sale)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
## Set Up for Testing
|
213
|
+
|
214
|
+
You need to `require("uses/inject_double")` and then `include Uses::InjectDouble` to make `inject_rspec_double` and
|
215
|
+
`inject_double` available. Practically speaking, you should do this in your base test case or RSpec configuration:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
# spec/spec_helper.rb
|
219
|
+
require "uses/inject_double"
|
220
|
+
|
221
|
+
RSpec.configure do |config|
|
222
|
+
config.include Uses::InjectDouble
|
223
|
+
|
224
|
+
# ... remainder of your configuration
|
225
|
+
end
|
226
|
+
```
|
227
|
+
|
228
|
+
|
229
|
+
## Reference
|
230
|
+
|
231
|
+
### `uses` macro
|
232
|
+
|
233
|
+
The `uses` macro does two things:
|
234
|
+
|
235
|
+
* Creates a private method that returns a memoized instance of the dependent class
|
236
|
+
* Instantiates that class
|
237
|
+
|
238
|
+
You can control both the name of the method and how the class is instantiated if needed, but the default behavior should work for most cases.
|
239
|
+
|
240
|
+
The full signature of `uses` is:
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
uses SomeClass, as: «method name»,
|
244
|
+
initialize: «initialization_strategy»
|
245
|
+
```
|
246
|
+
|
247
|
+
By default, the `«method name»` will be the underscorized version of the class, with slashes replaced by underscores, so for example `Braintree::Gateway` becomes `braintree_gateway`.
|
248
|
+
|
249
|
+
By default `«initialization_strategy»` is `:new_no_args`, which instructs `uses` to simply call `new` without any args, e.g.
|
250
|
+
`Braintree::Gateway.new`.
|
251
|
+
|
252
|
+
Thus the method that gets defined would look like this:
|
253
|
+
|
254
|
+
```ruby
|
255
|
+
def braintree_gateway
|
256
|
+
@braintree_gateway ||= Braintree::Gateway.new
|
257
|
+
end
|
258
|
+
private :braintree_gateway
|
259
|
+
```
|
260
|
+
|
261
|
+
#### `as:` option
|
262
|
+
|
263
|
+
By setting `as:` you can control the name of the private method if you don't like the default. Generally, you should not do this, but it can be
|
264
|
+
handy if you are migrating existing code to this gem and don't want to change variable names:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
uses Braintree::Gateway as: :braintree
|
268
|
+
|
269
|
+
# creates this method:
|
270
|
+
def braintree
|
271
|
+
@braintree ||= Braintree::Gateway.new
|
272
|
+
end
|
273
|
+
private :braintree
|
274
|
+
```
|
275
|
+
|
276
|
+
`as:` must be a value that can be used as a method in Ruby
|
277
|
+
|
278
|
+
#### `initialize:` option
|
279
|
+
|
280
|
+
Not all objects can be created with `.new`. Here are the possible values for `initialize:`
|
281
|
+
|
282
|
+
* `:new_no_args` - This is the default and creates an instance by calling `new` without any args. This should be how most of the classes you create are initialized.
|
283
|
+
* `:config_initializers` - This says that an instance has been pre-configured by a file in `config/initializers` that calls into Uses' configuration API like so:
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
# config/initializers/braintree.rb
|
287
|
+
Uses.initializers do |initializers|
|
288
|
+
initializers[Braintree::Gateway] = ->(*) {
|
289
|
+
Braintree::Gateway.new(
|
290
|
+
:environment => :sandbox,
|
291
|
+
:merchant_id => "your_merchant_id",
|
292
|
+
:public_key => "your_public_key",
|
293
|
+
:private_key => "your_private_key",
|
294
|
+
)
|
295
|
+
}
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
299
|
+
`initializers` is a hash that has the class as a key and a `Proc` as the value. That `Proc` will be used each time the class must be
|
300
|
+
instantiated. This is the second preferred method for creating instances since it is desirable to have a single location for how an instance is
|
301
|
+
created if it must be created in a complex way.
|
302
|
+
|
303
|
+
Note that it doesn't have to be in a file in `config/initializers`, it just has to be loaded before any class that uses `uses` is loaded.
|
304
|
+
* A `Proc` - If you set `initialize:` to a `Proc`, that `Proc` will be called to get the instance of the object. This is useful if the way in which the object is created differs based on what class is including it. This should be used rarely. Example:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
class PaymentProcessor
|
308
|
+
uses Braintree::Gateway, initialize: ->(*) {
|
309
|
+
Braintree::Gateway.new(
|
310
|
+
:environment => :sandbox,
|
311
|
+
:merchant_id => "your_merchant_id",
|
312
|
+
:public_key => "your_public_key",
|
313
|
+
:private_key => "your_private_key",
|
314
|
+
)
|
315
|
+
}
|
316
|
+
end
|
317
|
+
```
|
318
|
+
|
319
|
+
### `inject_rspec_double`
|
320
|
+
|
321
|
+
The signature of this method is:
|
322
|
+
|
323
|
+
```ruby
|
324
|
+
inject_rspec_double(«object_with_dependency»,
|
325
|
+
«class that it depends on»)
|
326
|
+
```
|
327
|
+
|
328
|
+
`inject_rspec_double` returns an instance created via RSpec's `instance_double` method. It will raise an exception if `«object_with_dependency»`'s
|
329
|
+
class does use call `uses «class that it depends on»`. This way, if you refactor your class to no longer need this dependency, your test will
|
330
|
+
fail.
|
331
|
+
|
332
|
+
### `inject_double`
|
333
|
+
|
334
|
+
The signature of this method is:
|
335
|
+
|
336
|
+
```ruby
|
337
|
+
inject_double(«object_with_dependency»,
|
338
|
+
«class that it depends on» => «mocked instance»)
|
339
|
+
```
|
340
|
+
|
341
|
+
This is useful if you need to control how the mocked instance is created or if you are not using RSpec.
|
342
|
+
|
343
|
+
### `Uses.config`
|
344
|
+
|
345
|
+
There is currently one configuration option.
|
346
|
+
|
347
|
+
#### `:on_circular_dependency` option
|
348
|
+
|
349
|
+
Because the proliferation of `uses` creates a structured representation of your service layer's dependencies, Uses can check for circular
|
350
|
+
dependencies. The reason this is important is that your code's behavior can become confusing if a dependency depends on another class that depends
|
351
|
+
on it.
|
352
|
+
|
353
|
+
By default, if a circular dependency is detected, Uses will emit a warning. You can change this behavior by setting a config value,
|
354
|
+
which you can do by creating `config/initializers/uses.rb`:
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
# config/initializers/uses.rb
|
358
|
+
Uses.config do |config|
|
359
|
+
config.on_circular_dependency = # one of :warn, :ignore, or :raise_error
|
360
|
+
end
|
361
|
+
```
|
362
|
+
|
363
|
+
Valid values are:
|
364
|
+
|
365
|
+
* `:warn` - this is the default and will emit a warning if a circular dependency is found.
|
366
|
+
* `:ignore` - this will log a warning a debug level, effectively squelching the message. Don't use this unless you are migrating a service layer that has a lot of circular dependencies.
|
367
|
+
* `:raise_error` - this will raise an `Uses::CircularDependency::Error` when it encounters a circular dependency.
|
368
|
+
|
data/Rakefile
ADDED
data/bin/ci
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "with_clues"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rspec-core", "rspec")
|