adaptor 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -1
- data/CHANGELOG.md +29 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +23 -15
- data/README.md +94 -31
- data/lib/adaptor/loader.rb +33 -14
- data/lib/adaptor/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cec000085cd4c4fbeee7ca6dc0ba9443733f5dcd981167e948fe9f5e78a209d
|
4
|
+
data.tar.gz: 71dc57e3c8157ecd56eb8cdf43434a873ec9d4369bb6ffb437446d023a67fb53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cef2a653b2e6f619d0d792f77aaa8969c39d66adb629926a5cacec055a9c56790cdb0c89821b755120c4e6e8d1eb33891e286e479e8666b69d8967ac67a89d5a
|
7
|
+
data.tar.gz: e25535ef085921a51ed8c04c4d9fb3e0aa5af09bde3229817a1a655f7a326f7fa1332d22c8b7e521ca816a5d1323847725814ceaa9bae947eefdac4caee13703
|
data/.rubocop.yml
CHANGED
@@ -5,6 +5,7 @@ AllCops:
|
|
5
5
|
Include:
|
6
6
|
- '**/Gemfile'
|
7
7
|
- '**/Rakefile'
|
8
|
+
- '**/*.rb'
|
8
9
|
Exclude:
|
9
10
|
- 'bin/*'
|
10
11
|
- 'db/**/*'
|
@@ -52,7 +53,7 @@ Style/SignalException:
|
|
52
53
|
Style/BracesAroundHashParameters:
|
53
54
|
EnforcedStyle: context_dependent
|
54
55
|
|
55
|
-
|
56
|
+
Layout/EndAlignment:
|
56
57
|
EnforcedStyleAlignWith: variable
|
57
58
|
AutoCorrect: true
|
58
59
|
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
6
|
+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.2.1]
|
11
|
+
|
12
|
+
### Fixed
|
13
|
+
|
14
|
+
- Fixed support for multiple arguments
|
15
|
+
|
16
|
+
## [0.2.0]
|
17
|
+
|
18
|
+
### Added
|
19
|
+
|
20
|
+
- Added support for multiple arguments
|
21
|
+
|
22
|
+
## [0.1.0]
|
23
|
+
|
24
|
+
Initial release.
|
25
|
+
|
26
|
+
[Unreleased]: https://github.com/aldesantis/adaptor/compare/v0.2.0...HEAD
|
27
|
+
[0.2.1]: https://github.com/aldesantis/adaptor/compare/v0.2.0...v0.2.1
|
28
|
+
[0.2.0]: https://github.com/aldesantis/adaptor/compare/v0.1.0...v0.2.0
|
29
|
+
[0.1.0]: https://github.com/aldesantis/adaptor/tree/v0.1.0
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,25 +1,31 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
adaptor (0.1
|
4
|
+
adaptor (0.2.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ast (2.4.0)
|
10
|
-
|
10
|
+
coderay (1.1.2)
|
11
|
+
coveralls (0.8.22)
|
11
12
|
json (>= 1.8, < 3)
|
12
|
-
simplecov (~> 0.
|
13
|
+
simplecov (~> 0.16.1)
|
13
14
|
term-ansicolor (~> 1.3)
|
14
15
|
thor (~> 0.19.4)
|
15
16
|
tins (~> 1.6)
|
16
17
|
diff-lcs (1.3)
|
17
|
-
docile (1.1
|
18
|
+
docile (1.3.1)
|
19
|
+
jaro_winkler (1.5.1)
|
18
20
|
json (2.1.0)
|
21
|
+
method_source (0.9.0)
|
19
22
|
parallel (1.12.1)
|
20
|
-
parser (2.
|
21
|
-
ast (~> 2.
|
22
|
-
powerpack (0.1.
|
23
|
+
parser (2.5.1.2)
|
24
|
+
ast (~> 2.4.0)
|
25
|
+
powerpack (0.1.2)
|
26
|
+
pry (0.11.3)
|
27
|
+
coderay (~> 1.1.0)
|
28
|
+
method_source (~> 0.9.0)
|
23
29
|
rainbow (3.0.0)
|
24
30
|
rake (10.5.0)
|
25
31
|
rspec (3.7.0)
|
@@ -35,18 +41,19 @@ GEM
|
|
35
41
|
diff-lcs (>= 1.2.0, < 2.0)
|
36
42
|
rspec-support (~> 3.7.0)
|
37
43
|
rspec-support (3.7.1)
|
38
|
-
rubocop (0.
|
44
|
+
rubocop (0.58.1)
|
45
|
+
jaro_winkler (~> 1.5.1)
|
39
46
|
parallel (~> 1.10)
|
40
|
-
parser (>= 2.
|
47
|
+
parser (>= 2.5, != 2.5.1.1)
|
41
48
|
powerpack (~> 0.1)
|
42
49
|
rainbow (>= 2.2.2, < 4.0)
|
43
50
|
ruby-progressbar (~> 1.7)
|
44
51
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
45
|
-
rubocop-rspec (1.
|
46
|
-
rubocop (>= 0.
|
52
|
+
rubocop-rspec (1.27.0)
|
53
|
+
rubocop (>= 0.56.0)
|
47
54
|
ruby-progressbar (1.9.0)
|
48
|
-
simplecov (0.
|
49
|
-
docile (~> 1.1
|
55
|
+
simplecov (0.16.1)
|
56
|
+
docile (~> 1.1)
|
50
57
|
json (>= 1.8, < 3)
|
51
58
|
simplecov-html (~> 0.10.0)
|
52
59
|
simplecov-html (0.10.2)
|
@@ -54,7 +61,7 @@ GEM
|
|
54
61
|
tins (~> 1.0)
|
55
62
|
thor (0.19.4)
|
56
63
|
tins (1.16.3)
|
57
|
-
unicode-display_width (1.
|
64
|
+
unicode-display_width (1.4.0)
|
58
65
|
|
59
66
|
PLATFORMS
|
60
67
|
ruby
|
@@ -63,10 +70,11 @@ DEPENDENCIES
|
|
63
70
|
adaptor!
|
64
71
|
bundler (~> 1.16)
|
65
72
|
coveralls (~> 0.8)
|
73
|
+
pry
|
66
74
|
rake (~> 10.0)
|
67
75
|
rspec (~> 3.0)
|
68
76
|
rubocop (~> 0.52)
|
69
77
|
rubocop-rspec (~> 1.22)
|
70
78
|
|
71
79
|
BUNDLED WITH
|
72
|
-
1.16.
|
80
|
+
1.16.2
|
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# Adaptor
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/aldesantis/adaptor
|
4
|
-
[![
|
5
|
-
[![
|
6
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/aldesantis/adaptor-rb/maintainability)
|
3
|
+
[![Build Status](https://travis-ci.org/aldesantis/adaptor.svg?branch=master)](https://travis-ci.org/aldesantis/adaptor)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/aldesantis/adaptor/badge.svg?branch=master)](https://coveralls.io/github/aldesantis/adaptor?branch=master)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/aldesantis/adaptor/maintainability)
|
7
6
|
|
8
7
|
Adaptor makes it easy to implement the [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) in Ruby.
|
9
8
|
|
@@ -25,7 +24,7 @@ Or install it yourself as:
|
|
25
24
|
|
26
25
|
## Usage
|
27
26
|
|
28
|
-
### Single
|
27
|
+
### Single adaptor mode
|
29
28
|
|
30
29
|
You can use the library in single-adaptor mode:
|
31
30
|
|
@@ -33,25 +32,33 @@ You can use the library in single-adaptor mode:
|
|
33
32
|
module DocumentProcessor
|
34
33
|
class Pdf
|
35
34
|
include Adaptor
|
36
|
-
|
35
|
+
|
37
36
|
def self.supports?(document)
|
38
37
|
document.mime_type == 'application/pdf'
|
39
38
|
end
|
40
|
-
|
39
|
+
|
40
|
+
def initialize(document)
|
41
|
+
@document = document
|
42
|
+
end
|
43
|
+
|
41
44
|
def build_thumbnail
|
42
|
-
#
|
45
|
+
# something with @document
|
43
46
|
end
|
44
47
|
end
|
45
|
-
|
46
|
-
class Word
|
48
|
+
|
49
|
+
class Word
|
47
50
|
include Adaptor
|
48
|
-
|
51
|
+
|
49
52
|
def self.supports?(document)
|
50
53
|
document.mime_type == 'application/msword'
|
51
54
|
end
|
52
|
-
|
55
|
+
|
56
|
+
def initialize(document)
|
57
|
+
@document = document
|
58
|
+
end
|
59
|
+
|
53
60
|
def build_thumbnail
|
54
|
-
#
|
61
|
+
# something with @document
|
55
62
|
end
|
56
63
|
end
|
57
64
|
end
|
@@ -61,12 +68,12 @@ module DocumentProcessor
|
|
61
68
|
register Pdf, Word
|
62
69
|
end
|
63
70
|
|
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(
|
71
|
+
# You can use #load_adaptor! if you want to raise an
|
72
|
+
# Adaptor::NoAdaptorError when no adaptor is found.
|
73
|
+
thumbnail = DocumentProcessor.load_adaptor(document).build_thumbnail
|
67
74
|
```
|
68
75
|
|
69
|
-
### Multiple
|
76
|
+
### Multiple adaptor mode
|
70
77
|
|
71
78
|
If it suits your use case, you can use multiple-adaptor mode:
|
72
79
|
|
@@ -74,31 +81,31 @@ If it suits your use case, you can use multiple-adaptor mode:
|
|
74
81
|
module NotificationProcessor
|
75
82
|
class Email
|
76
83
|
include Adaptor
|
77
|
-
|
84
|
+
|
78
85
|
def self.supports?(notification)
|
79
86
|
notification.user.email.present?
|
80
87
|
end
|
81
|
-
|
88
|
+
|
82
89
|
def initialize(notification)
|
83
90
|
@notification = notification
|
84
91
|
end
|
85
|
-
|
92
|
+
|
86
93
|
def deliver
|
87
94
|
# ...
|
88
95
|
end
|
89
96
|
end
|
90
|
-
|
91
|
-
class Sms
|
97
|
+
|
98
|
+
class Sms
|
92
99
|
include Adaptor
|
93
|
-
|
100
|
+
|
94
101
|
def self.supports?(notification)
|
95
102
|
notification.user.phone.present?
|
96
103
|
end
|
97
|
-
|
104
|
+
|
98
105
|
def initialize(notification)
|
99
106
|
@notification = notification
|
100
107
|
end
|
101
|
-
|
108
|
+
|
102
109
|
def deliver
|
103
110
|
# ...
|
104
111
|
end
|
@@ -110,15 +117,71 @@ module MultipleAdaptorLoader
|
|
110
117
|
register Email, Sms
|
111
118
|
end
|
112
119
|
|
113
|
-
# You can use #load_adaptors! if you want to raise an
|
114
|
-
# Adaptor::NoAdaptorError when no adaptors are found.
|
115
|
-
NotificationProcessor.load_adaptors(
|
120
|
+
# You can use #load_adaptors! if you want to raise an
|
121
|
+
# Adaptor::NoAdaptorError when no adaptors are found.
|
122
|
+
NotificationProcessor.load_adaptors(notification).each(&:deliver)
|
123
|
+
```
|
124
|
+
|
125
|
+
### Multiple arguments
|
126
|
+
|
127
|
+
Note that there is no limit to the number of arguments you can pass to the adaptors' `.supports?`
|
128
|
+
and `.new` methods. Here's an example that checks support only with one argument, but initializes
|
129
|
+
with multiple:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
module DocumentProcessor
|
133
|
+
class Pdf
|
134
|
+
include Adaptor
|
135
|
+
|
136
|
+
def self.supports?(document)
|
137
|
+
document.mime_type == 'application/pdf'
|
138
|
+
end
|
139
|
+
|
140
|
+
def initialize(document, options)
|
141
|
+
@document = document
|
142
|
+
@options = options
|
143
|
+
end
|
144
|
+
|
145
|
+
def build_thumbnail
|
146
|
+
# something with @document and @options
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
class Word
|
151
|
+
include Adaptor
|
152
|
+
|
153
|
+
def self.supports?(document)
|
154
|
+
document.mime_type == 'application/msword'
|
155
|
+
end
|
156
|
+
|
157
|
+
def initialize(document, options)
|
158
|
+
@document = document
|
159
|
+
@options = options
|
160
|
+
end
|
161
|
+
|
162
|
+
def build_thumbnail
|
163
|
+
# something with @document and @options
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
module DocumentProcessor
|
169
|
+
include Adaptor::Loader
|
170
|
+
register Pdf, Word
|
171
|
+
end
|
172
|
+
|
173
|
+
# You can use #load_adaptor! if you want to raise an
|
174
|
+
# Adaptor::NoAdaptorError when no adaptor is found.
|
175
|
+
thumbnail = DocumentProcessor.load_adaptor(document, stamp: true).build_thumbnail
|
116
176
|
```
|
117
177
|
|
178
|
+
As you can see, whatever you pass to `.load_adaptor` or `.load_adaptors` will be forwarded to
|
179
|
+
`.supports?` and `.new`, according to the methods' respective arity.
|
180
|
+
|
118
181
|
## Contributing
|
119
182
|
|
120
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/adaptor
|
121
|
-
project is intended to be a safe, welcoming space for collaboration, and contributors are expected
|
183
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/adaptor. This
|
184
|
+
project is intended to be a safe, welcoming space for collaboration, and contributors are expected
|
122
185
|
to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
123
186
|
|
124
187
|
## License
|
@@ -127,5 +190,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
127
190
|
|
128
191
|
## Code of Conduct
|
129
192
|
|
130
|
-
Everyone interacting in the Adaptor project’s codebases, issue trackers, chat rooms and mailing
|
193
|
+
Everyone interacting in the Adaptor project’s codebases, issue trackers, chat rooms and mailing
|
131
194
|
lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/adaptor/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/adaptor/loader.rb
CHANGED
@@ -32,46 +32,65 @@ module Adaptor
|
|
32
32
|
|
33
33
|
# Loads the first available adaptor for the given object.
|
34
34
|
#
|
35
|
-
# @param [
|
35
|
+
# @param [Array] args any arguments to pass to the adaptor's +.supports?+ and +.new+
|
36
36
|
#
|
37
37
|
# @return [Object|NilClass] an instance of the adpator or nil if no adaptor was found
|
38
|
-
def load_adaptor(
|
39
|
-
adaptors.find
|
38
|
+
def load_adaptor(*args)
|
39
|
+
adaptor_klass = adaptors.find do |klass|
|
40
|
+
adaptor_supports?(klass, args)
|
41
|
+
end
|
42
|
+
|
43
|
+
instantiate_adaptor(adaptor_klass, args) if adaptor_klass
|
40
44
|
end
|
41
45
|
|
42
46
|
# Loads the first available adaptor for the given object.
|
43
47
|
#
|
44
|
-
# @param [
|
48
|
+
# @param [Array] args any arguments to pass to the adaptor's +.supports?+ and +.new+
|
45
49
|
#
|
46
50
|
# @return [Object] an instance of the adaptor
|
47
51
|
#
|
48
52
|
# @raise [NoAdaptorError] when no adaptor was found
|
49
|
-
def load_adaptor!(
|
50
|
-
load_adaptor(
|
53
|
+
def load_adaptor!(*args)
|
54
|
+
load_adaptor(*args) || fail(NoAdaptorError, "No adaptor found for #{args.inspect}")
|
51
55
|
end
|
52
56
|
|
53
57
|
# Loads all the adaptors which support the given object.
|
54
58
|
#
|
55
|
-
# @param [
|
59
|
+
# @param [Array] args any arguments to pass to the adaptors' +.supports?+ and +.new+
|
56
60
|
#
|
57
61
|
# @return [Array] instances of compatible adaptors
|
58
|
-
def load_adaptors(
|
62
|
+
def load_adaptors(*args)
|
59
63
|
adaptors.map do |adaptor_klass|
|
60
|
-
next unless
|
61
|
-
adaptor_klass
|
64
|
+
next unless adaptor_supports?(adaptor_klass, args)
|
65
|
+
instantiate_adaptor(adaptor_klass, args)
|
62
66
|
end.compact
|
63
67
|
end
|
64
68
|
|
65
69
|
# Loads all the adaptors which support the given object.
|
66
70
|
#
|
67
|
-
# @param [
|
71
|
+
# @param [Array] args any arguments to pass to the adaptors' +.supports?+ and +.new+
|
68
72
|
#
|
69
73
|
# @return [Array] instances of compatible adaptors
|
70
74
|
#
|
71
75
|
# @raise [NoAdaptorError] when no adaptors are found
|
72
|
-
def load_adaptors!(
|
73
|
-
adaptors = load_adaptors(
|
74
|
-
adaptors.any? ? adaptors : fail(NoAdaptorError, "No adaptors found for #{
|
76
|
+
def load_adaptors!(*args)
|
77
|
+
adaptors = load_adaptors(*args)
|
78
|
+
adaptors.any? ? adaptors : fail(NoAdaptorError, "No adaptors found for #{args.inspect}")
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def adaptor_supports?(adaptor, args)
|
84
|
+
adaptor.supports?(*cut_args(args, method: adaptor.method(:supports?)))
|
85
|
+
end
|
86
|
+
|
87
|
+
def instantiate_adaptor(adaptor, args)
|
88
|
+
adaptor.new(*cut_args(args, method: adaptor.method(:new)))
|
89
|
+
end
|
90
|
+
|
91
|
+
def cut_args(args, method:)
|
92
|
+
arity = method.arity
|
93
|
+
arity.negative? ? args : args[0..(arity - 1)]
|
75
94
|
end
|
76
95
|
end
|
77
96
|
end
|
data/lib/adaptor/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adaptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Desantis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- ".rspec"
|
106
106
|
- ".rubocop.yml"
|
107
107
|
- ".travis.yml"
|
108
|
+
- CHANGELOG.md
|
108
109
|
- CODE_OF_CONDUCT.md
|
109
110
|
- Gemfile
|
110
111
|
- Gemfile.lock
|
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
139
|
version: '0'
|
139
140
|
requirements: []
|
140
141
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.7.
|
142
|
+
rubygems_version: 2.7.7
|
142
143
|
signing_key:
|
143
144
|
specification_version: 4
|
144
145
|
summary: Adaptor makes it easy to implement the Adapter pattern in Ruby.
|