项目作者: aldesantis

项目描述 :
No-nonsense, no-dependency Ruby implementation of the Adapter pattern.
高级语言: Ruby
项目地址: git://github.com/aldesantis/adaptor.git
创建时间: 2018-02-12T20:58:30Z
项目社区:https://github.com/aldesantis/adaptor

开源协议:MIT License

下载


Adaptor

Build Status
Coverage Status
Maintainability

Adaptor makes it easy to implement the Adapter pattern in Ruby.

Installation

Add this line to your application’s Gemfile:

  1. gem 'adaptor'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install adaptor

Usage

Single adaptor mode

You can use the library in single-adaptor mode:

  1. module DocumentProcessor
  2. class Pdf
  3. include Adaptor
  4. def self.supports?(document)
  5. document.mime_type == 'application/pdf'
  6. end
  7. def initialize(document)
  8. @document = document
  9. end
  10. def build_thumbnail
  11. # something with @document
  12. end
  13. end
  14. class Word
  15. include Adaptor
  16. def self.supports?(document)
  17. document.mime_type == 'application/msword'
  18. end
  19. def initialize(document)
  20. @document = document
  21. end
  22. def build_thumbnail
  23. # something with @document
  24. end
  25. end
  26. end
  27. module DocumentProcessor
  28. include Adaptor::Loader
  29. register Pdf, Word
  30. end
  31. # You can use #load_adaptor! if you want to raise an
  32. # Adaptor::NoAdaptorError when no adaptor is found.
  33. thumbnail = DocumentProcessor.load_adaptor(document).build_thumbnail

Multiple adaptor mode

If it suits your use case, you can use multiple-adaptor mode:

  1. module NotificationProcessor
  2. class Email
  3. include Adaptor
  4. def self.supports?(notification)
  5. notification.user.email.present?
  6. end
  7. def initialize(notification)
  8. @notification = notification
  9. end
  10. def deliver
  11. # ...
  12. end
  13. end
  14. class Sms
  15. include Adaptor
  16. def self.supports?(notification)
  17. notification.user.phone.present?
  18. end
  19. def initialize(notification)
  20. @notification = notification
  21. end
  22. def deliver
  23. # ...
  24. end
  25. end
  26. end
  27. module MultipleAdaptorLoader
  28. include Adaptor::Loader
  29. register Email, Sms
  30. end
  31. # You can use #load_adaptors! if you want to raise an
  32. # Adaptor::NoAdaptorError when no adaptors are found.
  33. 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:

  1. module DocumentProcessor
  2. class Pdf
  3. include Adaptor
  4. def self.supports?(document)
  5. document.mime_type == 'application/pdf'
  6. end
  7. def initialize(document, options)
  8. @document = document
  9. @options = options
  10. end
  11. def build_thumbnail
  12. # something with @document and @options
  13. end
  14. end
  15. class Word
  16. include Adaptor
  17. def self.supports?(document)
  18. document.mime_type == 'application/msword'
  19. end
  20. def initialize(document, options)
  21. @document = document
  22. @options = options
  23. end
  24. def build_thumbnail
  25. # something with @document and @options
  26. end
  27. end
  28. end
  29. module DocumentProcessor
  30. include Adaptor::Loader
  31. register Pdf, Word
  32. end
  33. # You can use #load_adaptor! if you want to raise an
  34. # Adaptor::NoAdaptorError when no adaptor is found.
  35. 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 code of conduct.

License

The gem is available as open source under the terms of the MIT License.

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.