Class: Startback::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/startback/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`).

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_factor do |c,h|
      c.foo = h["foo"]
    end

  end
end

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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.



43
44
45
# File 'lib/startback/context.rb', line 43

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.



49
50
51
# File 'lib/startback/context.rb', line 49

def logger
  @logger
end

#original_rack_envObject

Returns the value of attribute original_rack_env.



34
35
36
# File 'lib/startback/context.rb', line 34

def original_rack_env
  @original_rack_env
end

Class Method Details

.h(hash) ⇒ Object



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

def h(hash)
  h_factor!(self.new, hash)
end

.h_dump(&dumper) ⇒ Object



86
87
88
# File 'lib/startback/context.rb', line 86

def h_dump(&dumper)
  h_dumpers << dumper
end

.h_dump!(context, hash = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/startback/context.rb', line 75

def h_dump!(context, hash = {})
  h_dumpers.each do |d|
    context.instance_exec(hash, &d)
  end
  hash
end

.h_dumpersObject



82
83
84
# File 'lib/startback/context.rb', line 82

def h_dumpers
  @h_dumpers ||= []
end

.h_factor!(context, hash) ⇒ Object



58
59
60
61
62
63
# File 'lib/startback/context.rb', line 58

def h_factor!(context, hash)
  h_factories.each do |f|
    f.call(context, hash)
  end
  context
end

.h_factoriesObject



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

def h_factories
  @h_factories ||= []
end

.h_factory(&factory) ⇒ Object



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

def h_factory(&factory)
  h_factories << factory
end

Instance Method Details

#dupObject



115
116
117
118
119
120
# File 'lib/startback/context.rb', line 115

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.



96
97
98
99
100
# File 'lib/startback/context.rb', line 96

def factor(clazz, *args)
  @factored ||= {}
  key = args.empty? ? clazz : [clazz] + args
  @factored[key] ||= clazz.new(*(args << self))
end

#to_hObject



107
108
109
# File 'lib/startback/context.rb', line 107

def to_h
  self.class.h_dump!(self)
end

#to_json(*args, &bl) ⇒ Object



111
112
113
# File 'lib/startback/context.rb', line 111

def to_json(*args, &bl)
  to_h.to_json(*args, &bl)
end