https://github.com/odlp/adapter-pattern-example
https://github.com/odlp/adapter-pattern-example
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/odlp/adapter-pattern-example
- Owner: odlp
- Created: 2017-11-29T12:47:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-19T12:29:21.000Z (over 8 years ago)
- Last Synced: 2025-02-12T17:49:54.972Z (over 1 year ago)
- Language: Ruby
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Adapter pattern
Riffing on the adapter pattern from:
[http://legacy.new-bamboo.co.uk/blog/2008/02/05/micro-patterns-in-ruby/](http://legacy.new-bamboo.co.uk/blog/2008/02/05/micro-patterns-in-ruby/)
Takes a different approach of finding constants within a module that respond to `#match?`, avoiding inheritance and class variables.
## Reference implementation
From the blog post:
```ruby
# = The superclass / factory
class ServiceParser
# We register adapters into this class variable
#
@@adapters = []
# class reader
#
def self.adapters
@@adapters
end
# Factory method here, se below...
#
end
# = An example subclass / adapter
class FlickrAdapter < ServiceParser
def initialize( url)
end
# API methods
#
def thumbnail
# do something clever here...
end
# register ourselves with the Factory
#
ServiceParser.adapters << [self, /flickr\.com/]
end
```
Differences with this implementation:
- No `@@adapters` class variable
- Adapters don't need to add themselves into the `@@adapters` array (`ServiceParser.adapters <<`)
## Getting started
```
bundle install
bundle exec rspec
```