{"id":15513089,"url":"https://github.com/aldesantis/adaptor","last_synced_at":"2025-04-23T10:27:49.799Z","repository":{"id":48446307,"uuid":"121301066","full_name":"aldesantis/adaptor","owner":"aldesantis","description":"No-nonsense, no-dependency Ruby implementation of the Adapter pattern.","archived":false,"fork":false,"pushed_at":"2021-07-26T04:37:37.000Z","size":42,"stargazers_count":5,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T20:37:16.931Z","etag":null,"topics":["adapter","best-practices","design-patterns"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aldesantis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-12T20:58:30.000Z","updated_at":"2022-01-20T13:46:06.000Z","dependencies_parsed_at":"2022-08-19T16:10:21.528Z","dependency_job_id":null,"html_url":"https://github.com/aldesantis/adaptor","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fadaptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fadaptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fadaptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldesantis%2Fadaptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aldesantis","download_url":"https://codeload.github.com/aldesantis/adaptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250415105,"owners_count":21426712,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adapter","best-practices","design-patterns"],"created_at":"2024-10-02T09:54:04.980Z","updated_at":"2025-04-23T10:27:49.761Z","avatar_url":"https://github.com/aldesantis.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Adaptor\n\n[![Build Status](https://travis-ci.org/aldesantis/adaptor.svg?branch=master)](https://travis-ci.org/aldesantis/adaptor)\n[![Coverage Status](https://coveralls.io/repos/github/aldesantis/adaptor/badge.svg?branch=master)](https://coveralls.io/github/aldesantis/adaptor?branch=master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e51e8d7489eb72ab97ba/maintainability)](https://codeclimate.com/github/aldesantis/adaptor/maintainability)\n\nAdaptor makes it easy to implement the [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) in Ruby.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'adaptor'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install adaptor\n\n## Usage\n\n### Single adaptor mode\n\nYou can use the library in single-adaptor mode:\n\n```ruby\nmodule DocumentProcessor\n  class Pdf\n    include Adaptor\n\n    def self.supports?(document)\n      document.mime_type == 'application/pdf'\n    end\n\n    def initialize(document)\n      @document = document\n    end\n\n    def build_thumbnail\n      # something with @document\n    end\n  end\n\n  class Word\n    include Adaptor\n\n    def self.supports?(document)\n      document.mime_type == 'application/msword'\n    end\n\n    def initialize(document)\n      @document = document\n    end\n\n    def build_thumbnail\n      # something with @document\n    end\n  end\nend\n\nmodule DocumentProcessor\n  include Adaptor::Loader\n  register Pdf, Word\nend\n\n# You can use #load_adaptor! if you want to raise an\n# Adaptor::NoAdaptorError when no adaptor is found.\nthumbnail = DocumentProcessor.load_adaptor(document).build_thumbnail\n```\n\n### Multiple adaptor mode\n\nIf it suits your use case, you can use multiple-adaptor mode:\n\n```ruby\nmodule NotificationProcessor\n  class Email\n    include Adaptor\n\n    def self.supports?(notification)\n      notification.user.email.present?\n    end\n\n    def initialize(notification)\n      @notification = notification\n    end\n\n    def deliver\n      # ...\n    end\n  end\n\n  class Sms\n    include Adaptor\n\n    def self.supports?(notification)\n      notification.user.phone.present?\n    end\n\n    def initialize(notification)\n      @notification = notification\n    end\n\n    def deliver\n      # ...\n    end\n  end\nend\n\nmodule MultipleAdaptorLoader\n  include Adaptor::Loader\n  register Email, Sms\nend\n\n# You can use #load_adaptors! if you want to raise an\n# Adaptor::NoAdaptorError when no adaptors are found.\nNotificationProcessor.load_adaptors(notification).each(\u0026:deliver)\n```\n\n### Multiple arguments\n\nNote that there is no limit to the number of arguments you can pass to the adaptors' `.supports?`\nand `.new` methods. Here's an example that checks support only with one argument, but initializes\nwith multiple:\n\n```ruby\nmodule DocumentProcessor\n  class Pdf\n    include Adaptor\n\n    def self.supports?(document)\n      document.mime_type == 'application/pdf'\n    end\n\n    def initialize(document, options)\n      @document = document\n      @options = options\n    end\n\n    def build_thumbnail\n      # something with @document and @options\n    end\n  end\n\n  class Word\n    include Adaptor\n\n    def self.supports?(document)\n      document.mime_type == 'application/msword'\n    end\n\n    def initialize(document, options)\n      @document = document\n      @options = options\n    end\n\n    def build_thumbnail\n      # something with @document and @options\n    end\n  end\nend\n\nmodule DocumentProcessor\n  include Adaptor::Loader\n  register Pdf, Word\nend\n\n# You can use #load_adaptor! if you want to raise an\n# Adaptor::NoAdaptorError when no adaptor is found.\nthumbnail = DocumentProcessor.load_adaptor(document, stamp: true).build_thumbnail\n```\n\nAs you can see, whatever you pass to `.load_adaptor` or `.load_adaptors` will be forwarded to\n`.supports?` and `.new`, according to the methods' respective arity.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/aldesantis/adaptor. This\nproject is intended to be a safe, welcoming space for collaboration, and contributors are expected\nto adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Adaptor project’s codebases, issue trackers, chat rooms and mailing\nlists is expected to follow the [code of conduct](https://github.com/[USERNAME]/adaptor/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldesantis%2Fadaptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faldesantis%2Fadaptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldesantis%2Fadaptor/lists"}