Class: Langis::Dsl::RackishConfig

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/langis/dsl.rb

Overview

A Dsl class used to define Rackish application stacks. This classes implementation is heavily inspired by Rack itself.

Example:

block = proc do
  use Middleware, arg1, arg2
  run lambda { |env| return [200, {}, env[:input1]] }
end
config = Langis::RackishConfig.new
Blockenspiel.invoke block, config 
my_app = config.to_app

env = {
  :input1 => 'Hello World'
}
results = my_app.call env

See Also:

  • #langis_plumbing

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ RackishConfig

Returns a new instance of RackishConfig.

Parameters:

  • app (#call) (defaults to: nil)

    Optional endpoint to declare up front. If nil, then a “no-op” end-point is used with a very basic return.



305
306
307
308
# File 'lib/langis/dsl.rb', line 305

def initialize(app=nil)
  @ins = []
  @app = app ? app : lambda { |env| [OK, {}, [""]] }
end

Instance Method Details

#run(app) ⇒ Object

Dsl only method that defines the end point Rack app handler.

Parameters:

  • app (#call)

    The Rackish endpoint for this app.



341
342
343
# File 'lib/langis/dsl.rb', line 341

def run(app)
  @app = app
end

#to_app(app = nil) ⇒ #call

The method that actually wires up each middleware and the end point into a real Rack stack.

Parameters:

  • app (#call) (defaults to: nil)

    Optional endpoint to use instead of the one previously defined by a ‘run`.

Returns:

  • (#call)

    The Rackish application.



317
318
319
320
# File 'lib/langis/dsl.rb', line 317

def to_app(app=nil)
  app ||= @app
  @ins.reverse.inject(app) { |a, e| e.call(a) }
end

#use(middleware, *args, &block) ⇒ Object

Dsl only method that defines a piece of Middleware to run, in order, in this Rack-lik application.

Parameters:

  • middleware (Class)

    The middleware class to instantiate.

  • *args

    The arguments to pass to the initialize method for the middleware class instantiation.

  • &block

    A code block to pass to the initialize method for the middleware class instantiation.



333
334
335
# File 'lib/langis/dsl.rb', line 333

def use(middleware, *args, &block)
  @ins << lambda { |app| middleware.new(app, *args, &block) }
end