adaptor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +102 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/adaptor.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/adaptor.rb +32 -0
- data/lib/adaptor/errors.rb +8 -0
- data/lib/adaptor/loader.rb +78 -0
- data/lib/adaptor/version.rb +5 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e0990c06482fe55cec104a9f0f0587f1b0d00a6b5187bfbb05d16e6acdff87f9
|
4
|
+
data.tar.gz: 624998b5b711ceb962a592be32c456ad0f248aa52b410e2b7672aaad1c6a07ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d58b8bfa436860609a2cdbafcdea3750caa8ce0a1b0bd152755e4f74e5292769830c46033e0c31983c444cf218a9f3cd1ac350ac4fc4cab62ea06080469ef51
|
7
|
+
data.tar.gz: 6008e017efe35b30848983df4a392825bf217230ffb1ab1e2aca0d8cf299da41ed1bd7e78533c23cfa89bd5b542d90654e880a5a7c5221684d4366eaaee696e7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.3
|
5
|
+
Include:
|
6
|
+
- '**/Gemfile'
|
7
|
+
- '**/Rakefile'
|
8
|
+
Exclude:
|
9
|
+
- 'bin/*'
|
10
|
+
- 'db/**/*'
|
11
|
+
- 'vendor/bundle/**/*'
|
12
|
+
- 'spec/spec_helper.rb'
|
13
|
+
- 'spec/rails_helper.rb'
|
14
|
+
- 'spec/support/**/*'
|
15
|
+
- 'config/**/*'
|
16
|
+
- '**/Rakefile'
|
17
|
+
- '**/Gemfile'
|
18
|
+
|
19
|
+
RSpec/DescribeClass:
|
20
|
+
Exclude:
|
21
|
+
- 'spec/requests/**/*'
|
22
|
+
|
23
|
+
Style/BlockDelimiters:
|
24
|
+
Exclude:
|
25
|
+
- 'spec/**/*'
|
26
|
+
|
27
|
+
Layout/AlignParameters:
|
28
|
+
EnforcedStyle: with_fixed_indentation
|
29
|
+
|
30
|
+
Layout/ClosingParenthesisIndentation:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Metrics/LineLength:
|
34
|
+
Max: 100
|
35
|
+
AllowURI: true
|
36
|
+
|
37
|
+
Layout/FirstParameterIndentation:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Layout/MultilineMethodCallIndentation:
|
41
|
+
EnforcedStyle: indented
|
42
|
+
|
43
|
+
Layout/IndentArray:
|
44
|
+
EnforcedStyle: consistent
|
45
|
+
|
46
|
+
Layout/IndentHash:
|
47
|
+
EnforcedStyle: consistent
|
48
|
+
|
49
|
+
Style/SignalException:
|
50
|
+
EnforcedStyle: semantic
|
51
|
+
|
52
|
+
Style/BracesAroundHashParameters:
|
53
|
+
EnforcedStyle: context_dependent
|
54
|
+
|
55
|
+
Lint/EndAlignment:
|
56
|
+
EnforcedStyleAlignWith: variable
|
57
|
+
AutoCorrect: true
|
58
|
+
|
59
|
+
Style/AndOr:
|
60
|
+
EnforcedStyle: conditionals
|
61
|
+
|
62
|
+
Style/MultilineBlockChain:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
RSpec/NamedSubject:
|
66
|
+
Enabled: false
|
67
|
+
|
68
|
+
RSpec/ExampleLength:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Layout/MultilineMethodCallBraceLayout:
|
72
|
+
Enabled: false
|
73
|
+
|
74
|
+
Metrics/MethodLength:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
Metrics/AbcSize:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
Metrics/PerceivedComplexity:
|
81
|
+
Enabled: false
|
82
|
+
|
83
|
+
Metrics/CyclomaticComplexity:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
Metrics/ClassLength:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
Metrics/BlockLength:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Style/Documentation:
|
93
|
+
Enabled: false
|
94
|
+
|
95
|
+
Naming/MethodName:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Naming/AccessorMethodName:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
RSpec/MessageSpies:
|
102
|
+
EnforcedStyle: receive
|
data/.travis.yml
ADDED
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, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
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 desa.alessandro@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 [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
adaptor (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
coveralls (0.8.21)
|
11
|
+
json (>= 1.8, < 3)
|
12
|
+
simplecov (~> 0.14.1)
|
13
|
+
term-ansicolor (~> 1.3)
|
14
|
+
thor (~> 0.19.4)
|
15
|
+
tins (~> 1.6)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
docile (1.1.5)
|
18
|
+
json (2.1.0)
|
19
|
+
parallel (1.12.1)
|
20
|
+
parser (2.4.0.2)
|
21
|
+
ast (~> 2.3)
|
22
|
+
powerpack (0.1.1)
|
23
|
+
rainbow (3.0.0)
|
24
|
+
rake (10.5.0)
|
25
|
+
rspec (3.7.0)
|
26
|
+
rspec-core (~> 3.7.0)
|
27
|
+
rspec-expectations (~> 3.7.0)
|
28
|
+
rspec-mocks (~> 3.7.0)
|
29
|
+
rspec-core (3.7.1)
|
30
|
+
rspec-support (~> 3.7.0)
|
31
|
+
rspec-expectations (3.7.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.7.0)
|
34
|
+
rspec-mocks (3.7.0)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.7.0)
|
37
|
+
rspec-support (3.7.1)
|
38
|
+
rubocop (0.52.1)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 2.4.0.2, < 3.0)
|
41
|
+
powerpack (~> 0.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
ruby-progressbar (~> 1.7)
|
44
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
45
|
+
rubocop-rspec (1.22.2)
|
46
|
+
rubocop (>= 0.52.1)
|
47
|
+
ruby-progressbar (1.9.0)
|
48
|
+
simplecov (0.14.1)
|
49
|
+
docile (~> 1.1.0)
|
50
|
+
json (>= 1.8, < 3)
|
51
|
+
simplecov-html (~> 0.10.0)
|
52
|
+
simplecov-html (0.10.2)
|
53
|
+
term-ansicolor (1.6.0)
|
54
|
+
tins (~> 1.0)
|
55
|
+
thor (0.19.4)
|
56
|
+
tins (1.16.3)
|
57
|
+
unicode-display_width (1.3.0)
|
58
|
+
|
59
|
+
PLATFORMS
|
60
|
+
ruby
|
61
|
+
|
62
|
+
DEPENDENCIES
|
63
|
+
adaptor!
|
64
|
+
bundler (~> 1.16)
|
65
|
+
coveralls (~> 0.8)
|
66
|
+
rake (~> 10.0)
|
67
|
+
rspec (~> 3.0)
|
68
|
+
rubocop (~> 0.52)
|
69
|
+
rubocop-rspec (~> 1.22)
|
70
|
+
|
71
|
+
BUNDLED WITH
|
72
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Alessandro Desantis
|
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,131 @@
|
|
1
|
+
# Adaptor
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/aldesantis/adaptor-rb.svg?branch=master)](https://travis-ci.org/aldesantis/adaptor-rb)
|
4
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/aldesantis/adaptor-rb.svg)](https://gemnasium.com/github.com/aldesantis/adaptor-rb)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/aldesantis/adaptor-rb/badge.svg?branch=master)](https://coveralls.io/github/aldesantis/adaptor-rb?branch=master)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/aldesantis/adaptor-rb/maintainability)
|
7
|
+
|
8
|
+
Adaptor makes it easy to implement the [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) in Ruby.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'adaptor'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install adaptor
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Single-Adaptor Mode
|
29
|
+
|
30
|
+
You can use the library in single-adaptor mode:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
module DocumentProcessor
|
34
|
+
class Pdf
|
35
|
+
include Adaptor
|
36
|
+
|
37
|
+
def self.supports?(document)
|
38
|
+
document.mime_type == 'application/pdf'
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_thumbnail
|
42
|
+
# ...
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Word
|
47
|
+
include Adaptor
|
48
|
+
|
49
|
+
def self.supports?(document)
|
50
|
+
document.mime_type == 'application/msword'
|
51
|
+
end
|
52
|
+
|
53
|
+
def build_thumbnail
|
54
|
+
# ...
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
module DocumentProcessor
|
60
|
+
include Adaptor::Loader
|
61
|
+
register Pdf, Word
|
62
|
+
end
|
63
|
+
|
64
|
+
# You can use #load_adaptor! if you want to raise an
|
65
|
+
# Adaptor::NoAdaptorError when no adaptor is found.
|
66
|
+
thumbnail = DocumentProcessor.load_adaptor(object).build_thumbnail
|
67
|
+
```
|
68
|
+
|
69
|
+
### Multiple-Adaptor Mode
|
70
|
+
|
71
|
+
If it suits your use case, you can use multiple-adaptor mode:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
module NotificationProcessor
|
75
|
+
class Email
|
76
|
+
include Adaptor
|
77
|
+
|
78
|
+
def self.supports?(notification)
|
79
|
+
notification.user.email.present?
|
80
|
+
end
|
81
|
+
|
82
|
+
def initialize(notification)
|
83
|
+
@notification = notification
|
84
|
+
end
|
85
|
+
|
86
|
+
def deliver
|
87
|
+
# ...
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Sms
|
92
|
+
include Adaptor
|
93
|
+
|
94
|
+
def self.supports?(notification)
|
95
|
+
notification.user.phone.present?
|
96
|
+
end
|
97
|
+
|
98
|
+
def initialize(notification)
|
99
|
+
@notification = notification
|
100
|
+
end
|
101
|
+
|
102
|
+
def deliver
|
103
|
+
# ...
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
module MultipleAdaptorLoader
|
109
|
+
include Adaptor::Loader
|
110
|
+
register Email, Sms
|
111
|
+
end
|
112
|
+
|
113
|
+
# You can use #load_adaptors! if you want to raise an
|
114
|
+
# Adaptor::NoAdaptorError when no adaptors are found.
|
115
|
+
NotificationProcessor.load_adaptors(notifications).each(&:deliver)
|
116
|
+
```
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/adaptor-rb. This
|
121
|
+
project is intended to be a safe, welcoming space for collaboration, and contributors are expected
|
122
|
+
to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
123
|
+
|
124
|
+
## License
|
125
|
+
|
126
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
127
|
+
|
128
|
+
## Code of Conduct
|
129
|
+
|
130
|
+
Everyone interacting in the Adaptor project’s codebases, issue trackers, chat rooms and mailing
|
131
|
+
lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/adaptor/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/adaptor.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'adaptor/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'adaptor'
|
9
|
+
spec.version = Adaptor::VERSION
|
10
|
+
spec.authors = ['Alessandro Desantis']
|
11
|
+
spec.email = ['desa.alessandro@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Adaptor makes it easy to implement the Adapter pattern in Ruby.'
|
14
|
+
spec.homepage = 'https://github.com/aldesantis/adaptor'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.52'
|
29
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.22'
|
30
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "adaptor"
|
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/setup
ADDED
data/lib/adaptor.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'adaptor/version'
|
4
|
+
require 'adaptor/loader'
|
5
|
+
require 'adaptor/errors'
|
6
|
+
|
7
|
+
# Adaptor makes it easy to implement the Adapter pattern in Ruby.
|
8
|
+
#
|
9
|
+
# @example Defining a new adaptor
|
10
|
+
# class EmailAdaptor
|
11
|
+
# include Adaptor
|
12
|
+
#
|
13
|
+
# def self.supports?(notification)
|
14
|
+
# notification.recipient.email.present?
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
module Adaptor
|
18
|
+
def self.included(klass)
|
19
|
+
klass.extend ClassMethods
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
# Returns whether this adaptor supports the given object.
|
24
|
+
#
|
25
|
+
# @param [Object] _object the object to test
|
26
|
+
#
|
27
|
+
# @return [Boolean] whether the adaptor supports the object
|
28
|
+
def supports?(_object)
|
29
|
+
fail NotImplementedError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Adaptor
|
4
|
+
# This error is raised when no adaptors can be found for an object and the adaptor is loaded
|
5
|
+
# through {Loader::ClassMethods#load_adaptor!} or {Loader::ClassMethods#load_adaptors!}.
|
6
|
+
class NoAdaptorError < StandardError
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Adaptor
|
4
|
+
# An adaptor loader holds and loads the adapter(s) which support an object.
|
5
|
+
#
|
6
|
+
# @example Defining a loader
|
7
|
+
# module NotificationProcessor
|
8
|
+
# include Adaptor::Loader
|
9
|
+
# register Email, Sms
|
10
|
+
# end
|
11
|
+
module Loader
|
12
|
+
def self.included(klass)
|
13
|
+
klass.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
# Registers one or more new adaptors with the loader.
|
18
|
+
#
|
19
|
+
# @param [Array<Class>] klasses the adaptors to register
|
20
|
+
def register(*klasses)
|
21
|
+
klasses.each do |klass|
|
22
|
+
adaptors << klass unless adaptors.include?(klass)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the adaptors registered with this loader.
|
27
|
+
#
|
28
|
+
# @return [Array<Class>]
|
29
|
+
def adaptors
|
30
|
+
@adaptors ||= []
|
31
|
+
end
|
32
|
+
|
33
|
+
# Loads the first available adaptor for the given object.
|
34
|
+
#
|
35
|
+
# @param [Object] object the object to load the adaptor for
|
36
|
+
#
|
37
|
+
# @return [Object|NilClass] an instance of the adpator or nil if no adaptor was found
|
38
|
+
def load_adaptor(object)
|
39
|
+
adaptors.find { |adaptor_klass| adaptor_klass.supports?(object) }&.new(object)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Loads the first available adaptor for the given object.
|
43
|
+
#
|
44
|
+
# @param [Object] object the object to load the adaptor for
|
45
|
+
#
|
46
|
+
# @return [Object] an instance of the adaptor
|
47
|
+
#
|
48
|
+
# @raise [NoAdaptorError] when no adaptor was found
|
49
|
+
def load_adaptor!(object)
|
50
|
+
load_adaptor(object) || fail(NoAdaptorError, "No adaptor found for #{object}")
|
51
|
+
end
|
52
|
+
|
53
|
+
# Loads all the adaptors which support the given object.
|
54
|
+
#
|
55
|
+
# @param [Object] object the object to load the adaptors for
|
56
|
+
#
|
57
|
+
# @return [Array] instances of compatible adaptors
|
58
|
+
def load_adaptors(object)
|
59
|
+
adaptors.map do |adaptor_klass|
|
60
|
+
next unless adaptor_klass.supports?(object)
|
61
|
+
adaptor_klass.new(object)
|
62
|
+
end.compact
|
63
|
+
end
|
64
|
+
|
65
|
+
# Loads all the adaptors which support the given object.
|
66
|
+
#
|
67
|
+
# @param [Object] object the object to load the adaptors for
|
68
|
+
#
|
69
|
+
# @return [Array] instances of compatible adaptors
|
70
|
+
#
|
71
|
+
# @raise [NoAdaptorError] when no adaptors are found
|
72
|
+
def load_adaptors!(object)
|
73
|
+
adaptors = load_adaptors(object)
|
74
|
+
adaptors.any? ? adaptors : fail(NoAdaptorError, "No adaptors found for #{object}")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adaptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alessandro Desantis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coveralls
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.52'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.52'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.22'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.22'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- desa.alessandro@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- adaptor.gemspec
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/adaptor.rb
|
118
|
+
- lib/adaptor/errors.rb
|
119
|
+
- lib/adaptor/loader.rb
|
120
|
+
- lib/adaptor/version.rb
|
121
|
+
homepage: https://github.com/aldesantis/adaptor
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.7.5
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Adaptor makes it easy to implement the Adapter pattern in Ruby.
|
145
|
+
test_files: []
|