Class: Startback::Context
- Inherits:
-
Object
- Object
- Startback::Context
- Extended by:
- HFactory
- Defined in:
- lib/startback/context.rb,
lib/startback/audit/ext/context.rb,
lib/startback/context/h_factory.rb,
lib/startback/event/ext/context.rb,
lib/startback/context/middleware.rb
Overview
Defines an execution context for Startback applications, and provides a cached factory for related abstractions (see ‘factor`), and an extensible world, statically and dynamically.
In web application, an instance of a context can be set on the Rack environment, using Context::Middleware.
This class SHOULD be subclassed for application required extensions to prevent touching the global Startback state itself.
Also, for event handling in distributed architectures, a Context should be dumpable and reloadable to JSON. An ‘h` information contract if provided for that. Subclasses may contribute to the dumping and reloading process through the `h_dump` and `h_factory` methods
module MyApp
class Context < Startback::Context
attr_accessor :foo
h_dump do |h|
h.merge!("foo" => foo)
end
h_factory do |c,h|
c.foo = h["foo"]
end
end
end
Defined Under Namespace
Modules: HFactory Classes: Middleware
Instance Attribute Summary collapse
-
#engine ⇒ Object
Returns the value of attribute engine.
-
#error_handler ⇒ Object
An error handler can be provided on the Context class.
-
#logger ⇒ Object
A logger can be provided on the context, and will be used for everything related to logging, audit trailing and robustness.
-
#original_rack_env ⇒ Object
Returns the value of attribute original_rack_env.
-
#tracer ⇒ Object
Returns the value of attribute tracer.
Class Method Summary collapse
Instance Method Summary collapse
- #dup ⇒ Object
-
#factor(clazz, *args) ⇒ Object
Factors an instance of ‘clazz`, which must be a Context-related abstraction (i.e. its constructor takes the context as last parameters).
- #fork(h = nil) ⇒ Object
-
#initialize {|_self| ... } ⇒ Context
constructor
A new instance of Context.
- #to_h ⇒ Object
- #to_json(*args, &bl) ⇒ Object
- #trace_span(attributes = {}, &block) ⇒ Object
- #with_world(world) ⇒ Object
- #world ⇒ Object
Methods included from HFactory
h, h_dump, h_dump!, h_dumpers, h_dumpers=, h_dumpers?, h_factor!, h_factories, h_factories=, h_factories?, h_factory, inherited
Constructor Details
#initialize {|_self| ... } ⇒ Context
Returns a new instance of Context.
55 56 57 58 |
# File 'lib/startback/context.rb', line 55 def initialize super yield(self) if block_given? end |
Instance Attribute Details
#engine ⇒ Object
Returns the value of attribute engine.
3 4 5 |
# File 'lib/startback/event/ext/context.rb', line 3 def engine @engine end |
#error_handler ⇒ Object
An error handler can be provided on the Context class. The latter MUST expose an API similar to ruby’s Logger class. It can be a logger instance, simply.
Fatal errors catched by Web::CatchAll are sent on ‘error_handler#fatal`
Deprecated, use the logger below instead.
44 45 46 |
# File 'lib/startback/context.rb', line 44 def error_handler @error_handler end |
#logger ⇒ Object
A logger can be provided on the context, and will be used for everything related to logging, audit trailing and robustness. The logger receives object following the log & trail conventions of Startback, and must convert them to wathever log format is necessary.
50 51 52 |
# File 'lib/startback/context.rb', line 50 def logger @logger end |
#original_rack_env ⇒ Object
Returns the value of attribute original_rack_env.
35 36 37 |
# File 'lib/startback/context.rb', line 35 def original_rack_env @original_rack_env end |
#tracer ⇒ Object
Returns the value of attribute tracer.
3 4 5 |
# File 'lib/startback/audit/ext/context.rb', line 3 def tracer @tracer end |
Class Method Details
Instance Method Details
#dup ⇒ Object
113 114 115 116 117 118 |
# File 'lib/startback/context.rb', line 113 def dup super.tap{|c| c.send(:clean_factored!) yield(c) if block_given? } end |
#factor(clazz, *args) ⇒ Object
Factors an instance of ‘clazz`, which must be a Context-related abstraction (i.e. its constructor takes the context as last parameters).
Factored abstractions are cached for a given context & arguments.
87 88 89 90 91 |
# File 'lib/startback/context.rb', line 87 def factor(clazz, *args) @factored ||= {} key = args.empty? ? clazz : [clazz] + args @factored[key] ||= clazz.new(*(args << self)) end |
#fork(h = nil) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/startback/context.rb', line 106 def fork(h = nil) dup.tap{|duped| self.class.h_factor!(duped, h) if h yield(duped) if block_given? } end |
#to_h ⇒ Object
98 99 100 |
# File 'lib/startback/context.rb', line 98 def to_h self.class.h_dump!(self) end |
#to_json(*args, &bl) ⇒ Object
102 103 104 |
# File 'lib/startback/context.rb', line 102 def to_json(*args, &bl) to_h.to_json(*args, &bl) end |
#trace_span(attributes = {}, &block) ⇒ Object
11 12 13 14 |
# File 'lib/startback/audit/ext/context.rb', line 11 def trace_span(attributes = {}, &block) @tracer = tracer.new_trace unless tracer.attached? tracer.fork(attributes, &block) end |
#with_world(world) ⇒ Object
77 78 79 80 81 |
# File 'lib/startback/context.rb', line 77 def with_world(world) dup do |ctx| ctx._world = self.world.with(world) end end |
#world ⇒ Object
73 74 75 |
# File 'lib/startback/context.rb', line 73 def world @_world ||= self.class.factor_world(self) end |