solid-adapters 1.0.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.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +3 -0
  3. data/.standard.yml +5 -0
  4. data/CHANGELOG.md +10 -0
  5. data/CODE_OF_CONDUCT.md +132 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +418 -0
  8. data/Rakefile +14 -0
  9. data/examples/README.md +15 -0
  10. data/examples/anti_corruption_layer/README.md +217 -0
  11. data/examples/anti_corruption_layer/Rakefile +30 -0
  12. data/examples/anti_corruption_layer/app/models/payment/charge_credit_card.rb +36 -0
  13. data/examples/anti_corruption_layer/config.rb +19 -0
  14. data/examples/anti_corruption_layer/lib/payment_gateways/adapters/circle_up.rb +19 -0
  15. data/examples/anti_corruption_layer/lib/payment_gateways/adapters/pay_friend.rb +19 -0
  16. data/examples/anti_corruption_layer/lib/payment_gateways/contract.rb +15 -0
  17. data/examples/anti_corruption_layer/lib/payment_gateways/response.rb +5 -0
  18. data/examples/anti_corruption_layer/lib/payment_gateways.rb +11 -0
  19. data/examples/anti_corruption_layer/vendor/circle_up/client.rb +11 -0
  20. data/examples/anti_corruption_layer/vendor/pay_friend/client.rb +11 -0
  21. data/examples/ports_and_adapters/README.md +157 -0
  22. data/examples/ports_and_adapters/Rakefile +66 -0
  23. data/examples/ports_and_adapters/app/models/user/record/repository.rb +13 -0
  24. data/examples/ports_and_adapters/app/models/user/record.rb +7 -0
  25. data/examples/ports_and_adapters/config.rb +32 -0
  26. data/examples/ports_and_adapters/db/setup.rb +16 -0
  27. data/examples/ports_and_adapters/lib/user/creation.rb +19 -0
  28. data/examples/ports_and_adapters/lib/user/data.rb +5 -0
  29. data/examples/ports_and_adapters/lib/user/repository.rb +14 -0
  30. data/examples/ports_and_adapters/test/user_test/repository.rb +21 -0
  31. data/lib/solid/adapters/configurable/options.rb +44 -0
  32. data/lib/solid/adapters/configurable.rb +19 -0
  33. data/lib/solid/adapters/core/config.rb +35 -0
  34. data/lib/solid/adapters/core/proxy.rb +25 -0
  35. data/lib/solid/adapters/interface.rb +57 -0
  36. data/lib/solid/adapters/proxy.rb +11 -0
  37. data/lib/solid/adapters/version.rb +7 -0
  38. data/lib/solid/adapters.rb +28 -0
  39. metadata +85 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98a6d85923f9d8cfab76c09d6a9c0427bf842cb5f8853625a03bcbc19a5f92d9
4
+ data.tar.gz: 523975d5b3b31dc52ba2717c5e5da46c999ce8d5f4649f91758d6082af92ca25
5
+ SHA512:
6
+ metadata.gz: f4926ca375fdcbda5043eed9bd2712a9896261d9ed5a8f94ffde607c90f14cc1b2dcf0e45388f537cd56ba8f34ce160689b9a9935371390681b29c0d2903ed89
7
+ data.tar.gz: 521b034fc62de1250380685e59a0852938435a64c1e2f8e67ebdca4301b2017ca568df6a818b72bc5f0d2e93ff90258289578fa3d44b18fff064f2a844d601bc
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ inherit_gem:
2
+ standard:
3
+ - config/ruby-2.7.yml
data/.standard.yml ADDED
@@ -0,0 +1,5 @@
1
+ ruby_version: 2.7
2
+ ignore:
3
+ - ./examples/**/*
4
+ - ./**/*.gemfile.lock
5
+ - Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2024-06-23
4
+
5
+ ### Added
6
+
7
+ - Add `Solid::Adapters::Interface`
8
+ - Add `Solid::Adapters::Proxy`
9
+ - Add `Solid::Adapters::Configurable`
10
+ - Add `Solid::Adapters.config`, `Solid::Adapters.configuration` (alias `.configure`) to toggle the gem's features.
@@ -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, caste, color, religion, or sexual
10
+ identity 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 overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ 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 address,
35
+ 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 email 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
+ [INSERT CONTACT METHOD].
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 of
86
+ 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 permanent
93
+ 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 the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
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 at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/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.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Rodrigo Serradura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,418 @@
1
+ <p align="center">
2
+ <h1 align="center" id="-solidadapters">🧩 Solid::Adapters</h1>
3
+ <p align="center"><i>Write interface contracts using pure Ruby.</i></p>
4
+ <p align="center">
5
+ <a href="https://codeclimate.com/github/solid-process/solid-adapters/maintainability"><img src="https://api.codeclimate.com/v1/badges/b94564ac2686bc8d5feb/maintainability" /></a>
6
+ <a href="https://codeclimate.com/github/solid-process/solid-adapters/test_coverage"><img src="https://api.codeclimate.com/v1/badges/b94564ac2686bc8d5feb/test_coverage" /></a>
7
+ <img src="https://img.shields.io/badge/Ruby%20%3E%3D%202.7%2C%20%3C%3D%20Head-ruby.svg?colorA=444&colorB=333" alt="Ruby">
8
+ </p>
9
+ </p>
10
+
11
+ ## 📚 Table of Contents <!-- omit from toc -->
12
+
13
+ - [Supported Ruby](#supported-ruby)
14
+ - [Examples](#examples)
15
+ - [Installation](#installation)
16
+ - [Usage](#usage)
17
+ - [`Solid::Adapters::Interface`](#solidadaptersinterface)
18
+ - [Dynamic proxies](#dynamic-proxies)
19
+ - [`Solid::Adapters::Proxy`](#solidadaptersproxy)
20
+ - [Configuration](#configuration)
21
+ - [Non-toggleable features](#non-toggleable-features)
22
+ - [Solid::Adapters.configuration(freeze: false)](#solidadaptersconfigurationfreeze-false)
23
+ - [Solid::Adapters.config](#solidadaptersconfig)
24
+ - [`Solid::Adapters::Interface` versus `Solid::Adapters::Proxy`](#solidadaptersinterface-versus-solidadaptersproxy)
25
+ - [`Solid::Adapters::Configurable`](#solidadaptersconfigurable)
26
+ - [Configuration](#configuration-1)
27
+ - [About](#about)
28
+ - [Development](#development)
29
+ - [Contributing](#contributing)
30
+ - [License](#license)
31
+ - [Code of Conduct](#code-of-conduct)
32
+
33
+ ## Supported Ruby
34
+
35
+ This library is tested against:
36
+
37
+ Coverage | 2.7 | 3.0 | 3.1 | 3.2 | 3.3 | Head
38
+ ---- | --- | --- | --- | --- | --- | ---
39
+ 100% | ✅ | ✅ | ✅ | ✅ | ✅ | ✅
40
+
41
+ ## Examples
42
+
43
+ Check the [examples](examples) directory to see different applications of `solid-adapters`.
44
+
45
+ > **Attention:** Each example has its own **README** with more details.
46
+
47
+ 1. [Ports and Adapters](examples/ports_and_adapters) - Implements the Ports and Adapters pattern. It uses [**`Solid::Adapters::Interface`**](#solidadaptersinterface) to provide an interface from the application's core to other layers.
48
+
49
+ 2. [Anti-Corruption Layer](examples/anti_corruption_layer) - Implements the Anti-Corruption Layer pattern. It uses the [**`Solid::Adapters::Proxy`**](#solidadapterstproxy) to define an interface for a set of adapters, which will translate an external interface (`vendors`) to the application's core interface.
50
+
51
+ 3. [Solid::Rails::App](https://github.com/solid-process/solid-rails-app/tree/solid-process-4?tab=readme-ov-file#-solid-rails-app-) - A Rails application (Web and REST API) made with `solid-adapters` + [`solid-process`](https://github.com/solid-process/solid-process) that uses the Ports and Adapters (Hexagonal) architectural pattern to decouple the application's core from the framework.
52
+
53
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
54
+
55
+ ## Installation
56
+
57
+ Install the gem and add to the application's Gemfile by executing:
58
+
59
+ $ bundle add solid-adapters
60
+
61
+ If bundler is not being used to manage dependencies, install the gem by executing:
62
+
63
+ $ gem install solid-adapters
64
+
65
+ And require it in your code:
66
+
67
+ require 'solid/adapters'
68
+
69
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
70
+
71
+ ## Usage
72
+
73
+ ### `Solid::Adapters::Interface`
74
+
75
+ This feature allows the creation of a module that will be used as an interface.
76
+
77
+ It will check if the class that includes it or the object that extends it implements all the expected methods.
78
+
79
+ ```ruby
80
+ module User::Repository
81
+ include ::Solid::Adapters::Interface
82
+
83
+ module Methods
84
+ def create(name:, email:)
85
+ name => String
86
+ email => String
87
+
88
+ super.tap { _1 => ::User::Data[id: Integer, name: String, email: String] }
89
+ end
90
+ end
91
+ end
92
+ ```
93
+
94
+ Let's break down the example above.
95
+
96
+ 1. The `User::Repository` module includes `Solid::Adapters::Interface`.
97
+ 2. Defines the `Methods` module. It is mandatory, as these will be the methods to be implemented.
98
+ 3. The `create` method is defined inside the `Method`s' module.
99
+ 1. This method receives two arguments: `name` and `email`.
100
+ 2. The arguments are checked using the `=>` pattern matching operator.
101
+ 3. `super` is called to invoke the `create` method of the superclass. Which will be the class/object that includes/extends the `User::Repository` module.
102
+ 4. The `super` output is checked using pattern matching under the `tap` method.
103
+
104
+ Now, let's see how to use it in a class.
105
+
106
+ ```ruby
107
+ class User::Record::Repository
108
+ include User::Repository
109
+
110
+ def create(name:, email:)
111
+ record = Record.create(name:, email:)
112
+
113
+ ::User::Data.new(id: record.id, name: record.name, email: record.email)
114
+ end
115
+ end
116
+ ```
117
+
118
+ And how to use it in a module with singleton methods.
119
+
120
+ ```ruby
121
+ module User::Record::Repository
122
+ extend User::Repository
123
+
124
+ def self.create(name:, email:)
125
+ record = Record.create(name:, email:)
126
+
127
+ ::User::Data.new(id: record.id, name: record.name, email: record.email)
128
+ end
129
+ end
130
+ ```
131
+
132
+ **What happend when an interface module is included/extended?**
133
+
134
+ 1. An instance of the class will be a `User::Repository`.
135
+ 2. The module, class, object, that extended the interface will be a `User::Repository`.
136
+
137
+ ```ruby
138
+ class User::Record::Repository
139
+ include User::Repository
140
+ end
141
+
142
+ module UserTest::RepositoryInMemory
143
+ extend User::Repository
144
+ # ...
145
+ end
146
+
147
+ User::Record::Repository.new.is_a?(User::Repository) # true
148
+
149
+ UserTest::RepositoryInMemory.is_a?(User::Repository) # true
150
+ ```
151
+
152
+ **Why this is useful?**
153
+
154
+ You can use `=>` pattern matching or `is_a?` to ensure that the class/object implements the expected methods as it includes/extends the interface.
155
+
156
+ ```ruby
157
+ class User::Creation
158
+ def initialize(repository)
159
+ repository => User::Repository
160
+
161
+ @repository = repository
162
+ end
163
+
164
+ # ...
165
+ end
166
+ ```
167
+
168
+ > Access the [**Ports and Adapters example**](examples/ports_and_adapters) to see, test, and run something that uses the `Solid::Adapters::Interface`
169
+
170
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
171
+
172
+ #### Dynamic proxies
173
+
174
+ The `Solid::Adapters::Interface` can be used to create dynamic proxies. To do this, you must use the `.[]` method to wrap an object in a proxy that will check if the object implements the interface methods.
175
+
176
+ The advantage of dynamic proxies is that you can create a proxy for any object. Therefore, you don't need to include/extend the interface module to perform the checkings.
177
+
178
+ ```ruby
179
+ class User::Repository
180
+ include ::Solid::Adapters::Interface
181
+
182
+ module Methods
183
+ def create(name:, email:)
184
+ name => String
185
+ email => String
186
+
187
+ super.tap { _1 => ::User::Data[id: Integer, name: String, email: String] }
188
+ end
189
+ end
190
+ end
191
+
192
+ ## Real object example
193
+
194
+ class User::Record::Repository
195
+ def create(name:, email:)
196
+ ::User::Data.new(id: 1, name: name, email: email)
197
+ end
198
+ end
199
+
200
+ repository = User::Repository[User::Record::Repository.new]
201
+
202
+ ## Mock example
203
+
204
+ mock_repository = double
205
+
206
+ allow(mock_repository)
207
+ .to receive(:create)
208
+ .with(name: 'John', email: 'john@email.com')
209
+ .and_return(::User::Data.new(id: 1, name: 'John', email: 'john@email.com'))
210
+
211
+ repository = User::Repository[mock_repository]
212
+ ```
213
+
214
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
215
+
216
+ ### `Solid::Adapters::Proxy`
217
+
218
+ This feature allows the creation of a class that will be used as a proxy for another objects.
219
+
220
+ The idea is to define an interface for the object that will be proxied.
221
+
222
+ Let's implement the example from the [previous section](#solidadaptersinterface) using a proxy.
223
+
224
+ ```ruby
225
+ class User::Repository < Solid::Adapters::Proxy
226
+ def create(name:, email:)
227
+ name => String
228
+ email => String
229
+
230
+ object.create(name:, email:).tap do
231
+ _1 => ::User::Data[id: Integer, name: String, email: String]
232
+ end
233
+ end
234
+ end
235
+ ```
236
+
237
+ **How to use it?**
238
+
239
+ Inside the proxy you will use `object` to access the proxied object. This means the proxy must be initialized with an object. And the object must implement the methods defined in the proxy.
240
+
241
+ ```ruby
242
+ class User::Record::Repository
243
+ # ...
244
+ end
245
+
246
+ module UserTest::RepositoryInMemory
247
+ extend self
248
+ # ...
249
+ end
250
+
251
+ # The proxy must be initialized with an object that implements the expected methods
252
+
253
+ memory_repository = User::Repository.new(UserTest::RepositoryInMemory)
254
+
255
+ record_repository = User::Repository.new(User::Record::Repository.new)
256
+ ```
257
+
258
+ > Access the [**Anti-Corruption Layer**](examples/anti_corruption_layer) to see, test, and run something that uses the `Solid::Adapters::Proxy`
259
+
260
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
261
+
262
+ ## Configuration
263
+
264
+ By default, the `Solid::Adapters` enables all its features. You can disable them by setting the configuration.
265
+
266
+ ```ruby
267
+ Solid::Adapters.configuration do |config|
268
+ dev_or_test = ::Rails.env.local?
269
+
270
+ config.proxy_enabled = dev_or_test
271
+ config.interface_enabled = dev_or_test
272
+ end
273
+
274
+ # PS: You can use .configure is an alias for .configuration
275
+ ```
276
+
277
+ In the example above, the `Solid::Adapters::Proxy`, `Solid::Adapters::Interface` will be disabled in production.
278
+
279
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
280
+
281
+ ### Non-toggleable features
282
+
283
+ The following variants are always enabled. You cannot disable them through the configuration.
284
+
285
+ #### `Solid::Adapters::Proxy::AlwaysEnabled` <!-- omit from toc -->
286
+
287
+ ```ruby
288
+ class User::Repository
289
+ include ::Solid::Adapters::Interface::AlwaysEnabled
290
+
291
+ module Methods
292
+ # ...
293
+ end
294
+ end
295
+ ```
296
+
297
+ #### `Solid::Adapters::Interface::AlwaysEnabled` <!-- omit from toc -->
298
+
299
+ ```ruby
300
+ class User::Repository < Solid::Adapters::Proxy::AlwaysEnabled
301
+ # ...
302
+ end
303
+ ```
304
+
305
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
306
+
307
+ ### Solid::Adapters.configuration(freeze: false)
308
+
309
+ By default, the configuration is frozen after the block is executed. This means you cannot change the configuration after the application boot. If you need to change the configuration after the application boot, you can set the `freeze` option to `false`.
310
+
311
+ ```ruby
312
+ Solid::Adapters.configuration(freeze: false) do |config|
313
+ config.proxy_enabled = false
314
+ config.interface_enabled = ::Rails.env.local?
315
+ end
316
+ ```
317
+
318
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
319
+
320
+ ### Solid::Adapters.config
321
+
322
+ You can access or change (if the configuration is not frozen) the configuration by using the `Solid::Adapters.config` method.
323
+
324
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
325
+
326
+ ### `Solid::Adapters::Interface` versus `Solid::Adapters::Proxy`
327
+
328
+ The main difference between the interface and the proxy is when the settings take effect.
329
+
330
+ `Solid::Adapters::Interface` modules are applied with the application boot. So, you must ensure that the `Solid::Adapters.configuration` runs before loading the code. On the other hand, proxies dynamically check the configuration every time a proxy instance is generated, allowing for the possibility of turning `Solid::Adapters::Proxy` post-application boot.
331
+
332
+ I recommend using interfaces, as they can be included/extended directly and because they dynamically produce proxies. In other words, they are more versatile. But please remember you have different feature toggles in the configuration for using both adapters based on your application needs.
333
+
334
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
335
+
336
+ ### `Solid::Adapters::Configurable`
337
+
338
+ The `Solid::Adapters::Configurable` module can be included in a class to provide a configuration block. This is useful when you want to inject/define dependencies into a namespace dynamically.
339
+
340
+ First you need to include the module in the class. And define the configurations that you want to expose.
341
+
342
+ ```ruby
343
+ module User::Adapters
344
+ extend Solid::Adapters::Configurable
345
+
346
+ config.repository = nil
347
+ end
348
+ ```
349
+
350
+ Then you can use the `configure` method to set the configurations. Lets use a Rails initializer to set the repository.
351
+
352
+ ```ruby
353
+ # config/initializers/user_adapters.rb
354
+
355
+ User::Adapters.configuration do |config|
356
+ config.repository = User::Record::Repository.new
357
+ end
358
+ ```
359
+
360
+ So you can access the repository in some place like this:
361
+
362
+ ```ruby
363
+ class User::Creation
364
+ def initialize
365
+ @repository = User::Adapters.config.repository
366
+ end
367
+
368
+ def create(name:, email:)
369
+ @repository.create(name: name, email: email)
370
+ end
371
+ end
372
+ ```
373
+
374
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
375
+
376
+ #### Configuration
377
+
378
+ First, the `Solid::Adapters.configuration` does not affect the `Solid::Adapters::Configurable` configurations. This means you can use both features together.
379
+
380
+ Second, as the `Solid::Adapters.configuration` method, the `Solid::Adapters::Configurable` configurations are frozen by default. You can change this behavior by setting the `freeze` option to `false`.
381
+
382
+ ```ruby
383
+ # config/initializers/user_adapters.rb
384
+
385
+ User::Adapters.configuration(freeze: false) do |config|
386
+ config.repository = User::Record::Repository.new
387
+ end
388
+
389
+ # PS: You can use .configure is an alias for .configuration
390
+ ```
391
+
392
+ > Access the [Solid::Rails::App](https://github.com/solid-process/solid-rails-app/tree/solid-process-4?tab=readme-ov-file#-solid-rails-app-) versions 3 and 4 to see, test, and run something that uses the `Solid::Adapters::Configurable`.
393
+
394
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
395
+
396
+ ## About
397
+
398
+ [Rodrigo Serradura](https://github.com/serradura) created this project. He is the Solid Process creator and has already made similar gems like the [u-case](https://github.com/serradura/u-case) and [kind](https://github.com/serradura/kind/blob/main/lib/kind/result.rb). This gem can be used independently, but it also contains essential features that facilitate the adoption of Solid Process (the method) in code.
399
+
400
+ <p align="right"><a href="#-solidadapters">⬆️ &nbsp;back to top</a></p>
401
+
402
+ ## Development
403
+
404
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
405
+
406
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
407
+
408
+ ## Contributing
409
+
410
+ Bug reports and pull requests are welcome on GitHub at https://github.com/solid-process/solid-adapters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/solid-process/solid-adapters/blob/master/CODE_OF_CONDUCT.md).
411
+
412
+ ## License
413
+
414
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
415
+
416
+ ## Code of Conduct
417
+
418
+ Everyone interacting in the Solid::Adapters project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/solid-adapters/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs += %w[lib test]
8
+
9
+ t.test_files = FileList.new("test/**/*_test.rb")
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,15 @@
1
+ <small>
2
+
3
+ > `MENU` [README](../README.md) | Examples
4
+
5
+ </small>
6
+
7
+ ## 🧩 `Solid::Adapters` Examples
8
+
9
+ > **Attention:** Each example has its own **README** with more details.
10
+
11
+ 1. [Ports and Adapters](./ports_and_adapters) - Implements the Ports and Adapters pattern. It uses **`Solid::Adapters::Interface`** to provide an interface from the application's core to other layers.
12
+
13
+ 2. [Anti-Corruption Layer](./anti_corruption_layer) - Implements the Anti-Corruption Layer pattern. It uses the **`Solid::Adapters::Proxy`** to define an interface for a set of adapters, which will translate an external interface (`vendors`) to the application's core interface.
14
+
15
+ 3. [Solid::Rails::App](https://github.com/solid-process/solid-rails-app/tree/solid-process-4?tab=readme-ov-file#-solid-rails-app-) - A Rails application (Web and REST API) made with `solid-adapters` + [`solid-process`](https://github.com/solid-process/solid-process) that uses the Ports and Adapters (Hexagonal) architectural pattern to decouple the application's core from the framework.