lib/ruote/context.rb in ruote-2.1.9 vs lib/ruote/context.rb in ruote-2.1.10

- old
+ new

@@ -38,33 +38,57 @@ attr_reader :storage attr_accessor :worker attr_accessor :engine - def initialize (storage, worker_or_engine) + def initialize (storage, worker=nil) @storage = storage + @storage.context = self - @worker, @engine = if worker_or_engine.kind_of?(Ruote::Engine) - [ worker_or_engine.worker, worker_or_engine ] - else - [ worker_or_engine, nil ] - end + @engine = nil + @worker = worker + @services = {} + initialize_services end + # A trick : in order to avoid + # + # @context = o.respond_to?(:context) ? o.context : o + # # or + # #@context = o.is_a?(Ruote::Context) ? o : o.context + # + # simply letting a context say its context is itself. + # + def context + + self + end + + # Returns the engine_id (as set in the configuration under the key + # "engine_id"), or, by default, "engine". + # def engine_id get_conf['engine_id'] || 'engine' end + # Used for things like + # + # if @context['ruby_eval_allowed'] + # # ... + # end + # def [] (key) SERVICE_PREFIX.match(key) ? @services[key] : get_conf[key] end + # Mostly used by engine#configure + # def []= (key, value) raise( ArgumentError.new('use context#add_service to register services') ) if SERVICE_PREFIX.match(key) @@ -103,19 +127,18 @@ self.class.class_eval %{ def #{key[2..-1]}; @services['#{key}']; end } service end + # Takes care of shutting down every service registered in this context. + # def shutdown @storage.shutdown if @storage.respond_to?(:shutdown) @worker.shutdown if @worker - @services.values.each do |s| - - s.shutdown if s.respond_to?(:shutdown) - end + @services.values.each { |s| s.shutdown if s.respond_to?(:shutdown) } end protected def get_conf @@ -123,12 +146,10 @@ @storage.get_configuration('engine') || {} end def initialize_services - @services = {} - default_conf.merge(get_conf).each do |key, value| next unless SERVICE_PREFIX.match(key) add_service(key, *value) @@ -150,10 +171,12 @@ 's_plist' => [ 'ruote/part/participant_list', 'Ruote::ParticipantList' ], 's_dispatch_pool' => [ 'ruote/part/dispatch_pool', 'Ruote::DispatchPool' ], 's_logger' => [ - 'ruote/log/wait_logger', 'Ruote::WaitLogger' ] } + 'ruote/log/wait_logger', 'Ruote::WaitLogger' ], + 's_error_handler' => [ + 'ruote/error_handler', 'Ruote::ErrorHandler' ] } end end end