Class: Langis::Middleware::Parameterizer

Inherits:
Object
  • Object
show all
Defined in:
lib/langis/middleware.rb

Overview

Middleware that adds an Array of values to the Rackish Environment input. This array of values is created by calling callables using the said Rackish Environment as input, and from static strings.

The following example creates an Array of size two, and places it into the Rackish Environment key identified by ‘my_key’. The first item in the Array is the static string, “Hello World”. The second value is whatever was in the Rackish Environment under the key, “name”.

use Parameterizer,
  'Hello World',
  lambda { |env| env['name'] },
  :env_key => 'my_key'

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ Parameterizer

Returns a new instance of Parameterizer.

Parameters:

  • app (#call)

    The next link in the Rackish Application chain.

  • *args (String, #call)

    The list of new parameters that the Parameterizer middleware creates. String values are used as is, and callable objects are executed with the input Rackish Environment as the first parameter.

  • options (Hash)

    a customizable set of options



75
76
77
78
79
80
# File 'lib/langis/middleware.rb', line 75

def initialize(app, *args)
  @app = app
  @options = args.last.kind_of?(Hash) ? args.pop : {}
  @args = args
  @env_key = @options[:env_key] || MESSAGE_KEY
end

Instance Method Details

#call(env = {}) ⇒ Object

The main method of the Parameterizer middleware.

Parameters:

  • env (Hash) (defaults to: {})

    The input Rackish Environment.



86
87
88
89
90
91
92
93
# File 'lib/langis/middleware.rb', line 86

def call(env={})
  new_args = @args.map do |value|
    value.respond_to?(:call) ? value.call(env) : value
  end
  new_env = {}.update(env)
  new_env[@env_key] = new_args
  return @app.call new_env
end