Skip to content

Instantly share code, notes, and snippets.

@icebreaker
Created November 14, 2011 12:46
Show Gist options
  • Select an option

  • Save icebreaker/1363883 to your computer and use it in GitHub Desktop.

Select an option

Save icebreaker/1363883 to your computer and use it in GitHub Desktop.
Simple service driven architecture
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
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
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
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'
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