Sha256: 4bca01830c624532a3736a0cc11754d920793a61eb8a9a5aed85dc638c10e154
Contents?: true
Size: 1.45 KB
Versions: 27
Compression:
Stored size: 1.45 KB
Contents
module Startback class Context # # Rack middleware that installs a particular context instance # on the Rack environment. # # Examples: # # # Use the default context class # Rack::Builder.new do # use Startback::Context::Middleware # # run ->(env){ # ctx = env[Startback::Context::Middleware::RACK_ENV_KEY] # ctx.is_a?(Startback::Context) # => true # } # end # # # Use a user defined context class # Rack::Builder.new do # use Startback::Context::Middleware, context_class: MyContextClass # # run ->(env){ # ctx = env[Startback::Context::Middleware::RACK_ENV_KEY] # ctx.is_a?(MyContextClass) # => true (your subclass) # ctx.is_a?(Startback::Context) # => true (required!) # } # end # class Middleware RACK_ENV_KEY = 'SAMBACK_CONTEXT' DEFAULT_OPTIONS = { context_class: Context } def initialize(app, options = {}) @app = app @options = DEFAULT_OPTIONS.merge(options || {}) end attr_reader :options def call(env) env[RACK_ENV_KEY] ||= options[:context_class].h({}).tap{|c| c.original_rack_env = env.dup } @app.call(env) end def self.context(env) env[RACK_ENV_KEY] end end # class Middleware end # class Context end # module Startback
Version data entries
27 entries across 27 versions & 1 rubygems