Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aldesantis/adaptor
No-nonsense, no-dependency Ruby implementation of the Adapter pattern.
https://github.com/aldesantis/adaptor
adapter best-practices design-patterns
Last synced: 21 days ago
JSON representation
No-nonsense, no-dependency Ruby implementation of the Adapter pattern.
- Host: GitHub
- URL: https://github.com/aldesantis/adaptor
- Owner: aldesantis
- License: mit
- Created: 2018-02-12T20:58:30.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-26T04:37:37.000Z (over 3 years ago)
- Last Synced: 2024-12-26T02:21:36.700Z (23 days ago)
- Topics: adapter, best-practices, design-patterns
- Language: Ruby
- Homepage:
- Size: 41 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Adaptor
[![Build Status](https://travis-ci.org/aldesantis/adaptor.svg?branch=master)](https://travis-ci.org/aldesantis/adaptor)
[![Coverage Status](https://coveralls.io/repos/github/aldesantis/adaptor/badge.svg?branch=master)](https://coveralls.io/github/aldesantis/adaptor?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/aldesantis/adaptor/maintainability)Adaptor makes it easy to implement the [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) in Ruby.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'adaptor'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install adaptor
## Usage
### Single adaptor mode
You can use the library in single-adaptor mode:
```ruby
module DocumentProcessor
class Pdf
include Adaptordef self.supports?(document)
document.mime_type == 'application/pdf'
enddef initialize(document)
@document = document
enddef build_thumbnail
# something with @document
end
endclass Word
include Adaptordef self.supports?(document)
document.mime_type == 'application/msword'
enddef initialize(document)
@document = document
enddef build_thumbnail
# something with @document
end
end
endmodule DocumentProcessor
include Adaptor::Loader
register Pdf, Word
end# You can use #load_adaptor! if you want to raise an
# Adaptor::NoAdaptorError when no adaptor is found.
thumbnail = DocumentProcessor.load_adaptor(document).build_thumbnail
```### Multiple adaptor mode
If it suits your use case, you can use multiple-adaptor mode:
```ruby
module NotificationProcessor
class Email
include Adaptordef self.supports?(notification)
notification.user.email.present?
enddef initialize(notification)
@notification = notification
enddef deliver
# ...
end
endclass Sms
include Adaptordef self.supports?(notification)
notification.user.phone.present?
enddef initialize(notification)
@notification = notification
enddef deliver
# ...
end
end
endmodule MultipleAdaptorLoader
include Adaptor::Loader
register Email, Sms
end# You can use #load_adaptors! if you want to raise an
# Adaptor::NoAdaptorError when no adaptors are found.
NotificationProcessor.load_adaptors(notification).each(&:deliver)
```### Multiple arguments
Note that there is no limit to the number of arguments you can pass to the adaptors' `.supports?`
and `.new` methods. Here's an example that checks support only with one argument, but initializes
with multiple:```ruby
module DocumentProcessor
class Pdf
include Adaptordef self.supports?(document)
document.mime_type == 'application/pdf'
enddef initialize(document, options)
@document = document
@options = options
enddef build_thumbnail
# something with @document and @options
end
endclass Word
include Adaptordef self.supports?(document)
document.mime_type == 'application/msword'
enddef initialize(document, options)
@document = document
@options = options
enddef build_thumbnail
# something with @document and @options
end
end
endmodule DocumentProcessor
include Adaptor::Loader
register Pdf, Word
end# You can use #load_adaptor! if you want to raise an
# Adaptor::NoAdaptorError when no adaptor is found.
thumbnail = DocumentProcessor.load_adaptor(document, stamp: true).build_thumbnail
```As you can see, whatever you pass to `.load_adaptor` or `.load_adaptors` will be forwarded to
`.supports?` and `.new`, according to the methods' respective arity.## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/adaptor. This
project is intended to be a safe, welcoming space for collaboration, and contributors are expected
to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Adaptor project’s codebases, issue trackers, chat rooms and mailing
lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/adaptor/blob/master/CODE_OF_CONDUCT.md).