Class: Langis::Middleware::Parameterizer
- Inherits:
-
Object
- Object
- Langis::Middleware::Parameterizer
- 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
-
#call(env = {}) ⇒ Object
The main method of the Parameterizer middleware.
-
#initialize(app, *args) ⇒ Parameterizer
constructor
A new instance of Parameterizer.
Constructor Details
#initialize(app, *args) ⇒ Parameterizer
Returns a new instance of Parameterizer.
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.
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 |