lib/ruote/context.rb in ruote-2.1.6 vs lib/ruote/context.rb in ruote-2.1.7
- old
+ new
@@ -32,18 +32,19 @@
# and the engine can access subservices like parser, treechecker,
# wfid_generator and so on.
#
class Context
+ SERVICE_PREFIX = /^s\_/
+
attr_reader :storage
attr_accessor :worker
attr_accessor :engine
def initialize (storage, worker_or_engine)
@storage = storage
- @conf = default_conf.merge(@storage.get_configuration('engine') || {})
@worker, @engine = if worker_or_engine.kind_of?(Ruote::Engine)
[ worker_or_engine.worker, worker_or_engine ]
else
[ worker_or_engine, nil ]
@@ -52,71 +53,86 @@
initialize_services
end
def engine_id
- @conf['engine_id'] || 'engine'
+ get_conf['engine_id'] || 'engine'
end
def [] (key)
- @conf[key]
+ SERVICE_PREFIX.match(key) ? @services[key] : get_conf[key]
end
def []= (key, value)
- @conf[key] = value
+ raise(
+ ArgumentError.new('use context#add_service to register services')
+ ) if SERVICE_PREFIX.match(key)
+
+ cf = get_conf
+ cf[key] = value
+ @storage.put(cf)
+
+ value
end
def keys
- @conf.keys
+ get_conf.keys
end
def add_service (key, *args)
path, klass, opts = args
- key = "s_#{key}" unless key.match(/^s\_/)
+ key = "s_#{key}" unless SERVICE_PREFIX.match(key)
service = if klass
require(path)
aa = [ self ]
aa << opts if opts
- @conf[key] = Ruote.constantize(klass).new(*aa)
+ @services[key] = Ruote.constantize(klass).new(*aa)
else
- @conf[key] = path
+ @services[key] = path
end
- self.class.class_eval %{ def #{key[2..-1]}; @conf['#{key}']; end }
+ self.class.class_eval %{ def #{key[2..-1]}; @services['#{key}']; end }
service
end
def shutdown
@storage.shutdown if @storage.respond_to?(:shutdown)
@worker.shutdown if @worker
- @conf.values.each do |s|
+ @services.values.each do |s|
s.shutdown if s.respond_to?(:shutdown)
end
end
protected
+ def get_conf
+
+ @storage.get_configuration('engine') || {}
+ end
+
def initialize_services
- @conf.keys.each do |key|
+ @services = {}
- next unless key.match(/^s\_/)
+ default_conf.merge(get_conf).each do |key, value|
- add_service(key, *@conf[key])
+ next unless SERVICE_PREFIX.match(key)
+
+ add_service(key, *value)
end
end
def default_conf