Skip to content

Instantly share code, notes, and snippets.

@tosch
Created March 3, 2010 07:19
Show Gist options
  • Select an option

  • Save tosch/320403 to your computer and use it in GitHub Desktop.

Select an option

Save tosch/320403 to your computer and use it in GitHub Desktop.
module RuoteKit
# RuoteKit configuration handling
class Configuration
class ParticipantRegistrationProxy
def participant(*args)
RuoteKit.engine.register_participant(*args)
end
def catchall
'.+'
end
end
# Working directory for the engine (if using file system persistence)
attr_accessor :work_directory
# Number of workers to spawn (1 by default)
attr_accessor :workers
# Whether to run the engine or not (defaults to true)
attr_accessor :run_engine
# Whether to run a single worker or not (defaults to false)
attr_accessor :run_worker
def initialize
self.work_directory = File.join( Dir.pwd, "work_#{RuoteKit.env}" )
self.run_engine = true
self.run_worker = false
end
# Return the selected ruote-kit mode
def mode
@mode ||= :file_system
end
# Set the ruote-kit mode
def mode=( mode )
raise ArgumentError, "Unsupported mode (#{mode})" unless [ :transient, :file_system ].include?( mode )
@mode = mode
end
# Sets a custom storage
def set_storage( klass, *args )
@storage = [ klass, args ]
@mode = :custom
end
# Return the best suited storage class for the current mode
def storage_instance
if @storage
klass, args = @storage
return klass.new( *args )
end
case mode
when :transient
require 'ruote/storage/hash_storage'
Ruote::HashStorage.new
when :file_system
require 'ruote/storage/fs_storage'
Ruote::FsStorage.new( self.work_directory )
end
end
def catchall_participant
require 'ruote/part/storage_participant'
Ruote::StorageParticipant
end
def register &block
p = ParticipantRegistrationProxy.new
p.instance_eval(&block)
end
end
end
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
undef :context if defined?(context)
class MyCustomStorage
attr_reader :opts
def initialize (opts)
@opts = opts
end
end
class MyParticipant
end
describe RuoteKit do
describe 'configure' do
before(:each) do
# nothing
end
it 'should default to :transient' do
RuoteKit.configuration.mode.should == :transient
end
end
describe 'configure with a custom storage' do
before(:each) do
RuoteKit.configure do |conf|
#require 'path/to/my_custom_storage'
conf.set_storage( MyCustomStorage, :a => 'A', :b => 'B' )
conf.run_engine = false
end
end
after(:each) do
RuoteKit.reset_configuration!
end
it 'should advertise mode as :custom' do
RuoteKit.configuration.mode.should == :custom
end
it 'should return an instance of the customer storage' do
si = RuoteKit.configuration.storage_instance
si.class.should == MyCustomStorage
si.opts.should == { :a => 'A', :b => 'B' }
end
end
describe 'register participants' do
require 'ruote/participant'
describe 'custom participant' do
RuoteKit.configure do |conf|
conf.register do
participant 'al', MyParticipant
end
end
RuoteKit.engine.context.plist.names.should == ['al']
end
describe 'catchall participant' do
RuoteKit.configure do |conf|
conf.register do
participant catchall, MyParticipant
end
end
RuoteKit.engine.context.plist.names.should == [ '.+' ]
end
after do
RuoteKit.reset_configuration!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment