Created
November 14, 2011 12:46
-
-
Save icebreaker/1363883 to your computer and use it in GitHub Desktop.
Simple service driven architecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Service | |
| class ODT < Service::Base | |
| def hello(str) | |
| super | |
| puts 'ODT is here ...' | |
| end | |
| end | |
| end | |
| Service.register :odt do | |
| Service::ODT.new('ODT') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Service | |
| class PDF < Service::Base | |
| def hello(str) | |
| super | |
| puts 'PDF is here ...' | |
| end | |
| end | |
| end | |
| Service.register :pdf do | |
| Service::PDF.new('PDF') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Service | |
| class Sucks < Service::Base | |
| def hello(str) | |
| super | |
| puts 'Sucks is here ...' | |
| end | |
| end | |
| end | |
| Service.register :sucks do | |
| Service::Sucks.new('SUCKS') | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Service | |
| class << self | |
| def services | |
| @services ||= {} | |
| end | |
| def services_for(*names) | |
| services.each do |name, service| | |
| yield(service) if names.include? name | |
| end | |
| end | |
| def upload(options) | |
| filename = options[:filename] or return | |
| ext = File.extname(filename)[1..-1] or return | |
| services_for :sucks, ext.to_sym do |service| | |
| service.hello('uploading') | |
| end | |
| end | |
| def register(name) | |
| services[name] = yield | |
| end | |
| end | |
| class Base | |
| attr_reader :name | |
| attr_accessor :xyz | |
| def initialize(name) | |
| @name = name | |
| @xyz = nil | |
| end | |
| def hello(str) | |
| puts "#{@name} said #{str} ..." | |
| end | |
| end | |
| end | |
| require 'service-pdf' | |
| require 'service-odt' | |
| require 'service-sucks' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'service' | |
| Service.services_for :pdf, :odt do |service| | |
| service.hello('boo foo') | |
| end | |
| Service.upload :filename => 'myfile.pdf' | |
| Service.upload :filename => 'myfile.odt' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment