Class: Ruote::Context
- Inherits:
-
Object
- Object
- Ruote::Context
- Defined in:
- lib/ruote/context.rb
Overview
A sort of internal registry, via a shared instance of this class, the worker and the engine can access subservices like reader, treechecker, wfid_generator and so on.
Constant Summary collapse
- SERVICE_PREFIX =
/^s\_/
Instance Attribute Summary collapse
-
#engine ⇒ Object
Returns the value of attribute engine.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#worker ⇒ Object
Returns the value of attribute worker.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Used for things like.
-
#[]=(key, value) ⇒ Object
Mostly used by engine#configure.
- #add_service(key, *args) ⇒ Object
-
#context ⇒ Object
A trick : in order to avoid.
-
#engine_id ⇒ Object
Returns the engine_id (as set in the configuration under the key “engine_id”), or, by default, “engine”.
-
#initialize(storage, worker = nil) ⇒ Context
constructor
A new instance of Context.
- #keys ⇒ Object
-
#shutdown ⇒ Object
Takes care of shutting down every service registered in this context.
Constructor Details
#initialize(storage, worker = nil) ⇒ Context
Returns a new instance of Context.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruote/context.rb', line 43 def initialize(storage, worker=nil) @storage = storage @storage.context = self @engine = nil @worker = worker @services = {} initialize_services end |
Instance Attribute Details
#engine ⇒ Object
Returns the value of attribute engine.
41 42 43 |
# File 'lib/ruote/context.rb', line 41 def engine @engine end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
39 40 41 |
# File 'lib/ruote/context.rb', line 39 def storage @storage end |
#worker ⇒ Object
Returns the value of attribute worker.
40 41 42 |
# File 'lib/ruote/context.rb', line 40 def worker @worker end |
Instance Method Details
#[](key) ⇒ Object
Used for things like
if @context['ruby_eval_allowed']
# ...
end
83 84 85 86 |
# File 'lib/ruote/context.rb', line 83 def [](key) SERVICE_PREFIX.match(key) ? @services[key] : get_conf[key] end |
#[]=(key, value) ⇒ Object
Mostly used by engine#configure
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruote/context.rb', line 90 def []=(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 |
#add_service(key, *args) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ruote/context.rb', line 108 def add_service(key, *args) path, klass, opts = args key = "s_#{key}" unless SERVICE_PREFIX.match(key) service = if klass require(path) if path aa = [ self ] aa << opts if opts @services[key] = Ruote.constantize(klass).new(*aa) else @services[key] = path end self.class.class_eval %{ def #{key[2..-1]}; @services['#{key}']; end } # # This 'one-liner' will add an instance method to Context for this # service. # # If the service key is 's_dishwasher', then the service will be # available via Context#dishwasher. # # I.e. dishwasher = engine.context.dishwasher service end |
#context ⇒ Object
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.
64 65 66 67 |
# File 'lib/ruote/context.rb', line 64 def context self end |
#engine_id ⇒ Object
Returns the engine_id (as set in the configuration under the key “engine_id”), or, by default, “engine”.
72 73 74 75 |
# File 'lib/ruote/context.rb', line 72 def engine_id get_conf['engine_id'] || 'engine' end |
#keys ⇒ Object
103 104 105 106 |
# File 'lib/ruote/context.rb', line 103 def keys get_conf.keys end |
#shutdown ⇒ Object
Takes care of shutting down every service registered in this context.
142 143 144 145 146 147 148 |
# File 'lib/ruote/context.rb', line 142 def shutdown @worker.shutdown if @worker @storage.shutdown if @storage.respond_to?(:shutdown) @services.values.each { |s| s.shutdown if s.respond_to?(:shutdown) } end |