Class: Startback::Context

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
# File 'lib/startback/context.rb', line 55

def initialize
  super
  yield(self) if block_given?
end

Instance Attribute Details

#engineObject

Returns the value of attribute engine.



3
4
5
# File 'lib/startback/event/ext/context.rb', line 3

def engine
  @engine
end

#error_handlerObject

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

#loggerObject

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_envObject

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

#tracerObject

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

.factor_world(context) ⇒ Object



68
69
70
71
# File 'lib/startback/context.rb', line 68

def self.factor_world(context)
  @_world ||= Support::World.new
  @_world.with_scope(context)
end

.world(who, &block) ⇒ Object



63
64
65
66
# File 'lib/startback/context.rb', line 63

def self.world(who, &block)
  @_world ||= Support::World.new
  @_world = @_world.factory(who, &block)
end

Instance Method Details

#dupObject



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_hObject



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

#worldObject



73
74
75
# File 'lib/startback/context.rb', line 73

def world
  @_world ||= self.class.factor_world(self)
end